註記驅動的監控——一個註記,多個元件
此範例把監控 Feature 附加到巢狀拓撲中的六個服務,而不需撰寫任何一個個別元件的 features: 區塊。頂層邊界上由操作員撰寫的一個註記,經兩個子樹覆寫細化後,透過 Manifest 的 features.annotations 繫結展開為每個所含元件上的 core/prometheus-exporter 附加項目,並且各自渲染出自己的 Prometheus ServiceMonitor。
這就是註記驅動的 Feature 展開介面:把重複的各元件 Feature 附加項目,壓縮為單一個沿邊界樹向下串接的註記的模式。
形狀
- **開發者意圖(
aggregate.yaml)**只宣告六個example/compute/service元件——gateway,以及api/worker/ledger/etl/reconcile。沒有任何元件帶有監控的features:區塊。 - **Manifest(
manifest.yaml)**把註記繫結到 Feature:features:type:core/prometheus-exporter: helm/prometheus-exporter:1.0.0 # the FeatureRecipe version bindingannotations:feature.aggrega.ai/monitoring:feature: core/prometheus-exportermaps:infra: [interval, port, path] # structured-value subkeys → the feature's infra - **Release(
release.yaml)**在platform邊界上設定一次註記,並依子樹細化:platform:annotations:feature.aggrega.ai/monitoring: {interval: 30s, port: metrics, path: /metrics} # gateway inherits thisboundaries:payments:annotations:feature.aggrega.ai/monitoring: {interval: 15s} # api/worker/ledgerbatch:annotations:feature.aggrega.ai/monitoring: {interval: 120s} # etl/reconcile
core/prometheus-exporter 及其元件範圍的 ServiceMonitor FeatureRecipe 取自標準目錄(此範例未宣告本機副本)。該配方的 chart 為每個已附加的元件發出一個 monitoring.coreos.com/v1 ServiceMonitor,並依解析後的註記,以範本帶入 interval/port/path——三者都是功能 infra 中由操作員設定的抓取參數。(這是 FeatureRecipe 資源產生——由 helm 提供者渲染的 source: + valuesMapping:——而不是 Wasm 變異器或管線步驟。)
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example annotation-driven-monitoring它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd annotation-driven-monitoring建置
pull 會解析鎖定檔,而建置會讀取它:
aggrega pull --release release.yamlaggrega build --release release.yaml搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自行建立任何資料夾。若工作區資料夾中還沒有 `annotation-driven-monitoring` 資料夾,請呼叫 `draft` 工具一次,將 `example` 設為 `annotation-driven-monitoring`,且不指定 `dir`:它會把範例寫入新的 `annotation-driven-monitoring` 資料夾。接著呼叫 `build` 工具,將 `workspace_root` 設為 `annotation-driven-monitoring`、`release` 設為 `release.yaml`、`auto_pull` 設為 true、`write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `annotation-driven-monitoring/dist/` 之下。渲染輸出證明了什麼
解析鎖定檔、建置,然後檢視 dist/<boundary>/helm/<component>--feature-zdaggrega-zdai-zsmonitoring--<component>-servicemonitor-<component>-scrape.yaml:
aggrega pull --release release.yamlaggrega build --release release.yaml這個檔名值得解讀,因為這裡沒有任何人輸入過它。 此目標只讀取直接位於目錄中的檔案,因此附加項目的檔案會落在元件自己的檔案旁邊,並在檔名中而非目錄片段中帶有它們的兩個座標:Feature 所附加到的元件,然後是它被附加時所用的名稱。這裡第二個座標不是任何人寫下的名稱——它由註記鑄造,就是註記鍵本身 feature.aggrega.ai/monitoring。點與斜線無法照原樣放進檔名,所以各自變成 -z 加一個字母:點是 -zd,斜線是 -zs。對於 payments 邊界中的 api 元件,建置會寫出
dist/payments/helm/api--feature-zdaggrega-zdai-zsmonitoring--api-servicemonitor-api-scrape.yaml此編碼就是該鍵中的斜線不會開啟自己目錄的原因——這正是重點,因為深入一層的檔案,是讀取邊界目錄的抓取設定使用端不會取用的檔案。
此範例從標準程式庫取用 core/prometheus-exporter 及其 ServiceMonitor FeatureRecipe(以及 helm 提供者)。example/compute/service 示範元件及其配方位於共用目錄中的 example/ 發布者之下,與那些標準程式庫實體並列。其 .aggrega-config.yaml 宣告 hub.aggrega.ai 為預設登錄庫,因此實體從 hub 解析——兩個命令都不需要 --catalog 旗標。
| Component | 邊界 | 間隔 | 連接埠 | 路徑 |
|---|---|---|---|---|
gateway | platform | 30s(平台預設) | metrics | /metrics |
api / worker / ledger | payments | 15s(覆寫) | metrics | /metrics |
etl / reconcile | batch | 120s(覆寫) | metrics | /metrics |
這張表具體呈現三個事實:
- 註記驅動,而非作者宣告。 Aggregate 在任何地方都沒有監控
features:,然而卻渲染出六個 ServiceMonitor。它們存在,只是因為註記把core/prometheus-exporter展開到每個元件上(建置圖中附加項目的origin是annotation,而不是explicit)。 - 逐子鍵深層合併。
payments與batch只設定interval,其 ServiceMonitor 卻帶有port: metrics與path: /metrics——繼承自 platform 註記的結構化值,逐子鍵合併。子樹覆寫一個欄位,同時繼承其同層的其他欄位。 - 扇出。 六個元件由一個 platform 註記加上兩個單行覆寫來監控——不必在每個元件上重複
features:區塊。
ServiceMonitor 抓取 Service 的具名 metrics 連接埠(合成的 Service 公開 metrics: 9090),因此繫結的 port: metrics 會解析到實際的端點。
編譯時期與執行階段
這是 Kubernetes/Helm 範例;證明是 dist/ 中渲染出的資訊清單。ServiceMonitor 的 port 參照 Service 的具名連接埠,因此選擇器與端點依構造相符——不需要叢集來驗證連接。即時的 Prometheus Operator 會在部署時取用渲染出的 ServiceMonitor;編譯器的工作(註記 → 各元件的 ServiceMonitor,附帶串接解析後的設定檔)已完成,並且可於事前檢視。
建置是確定性的——重新執行,輸出逐位元組相同。