您可以在 AlloyDB Omni Kubernetes 運算子中使用自訂指標,對資料庫執行 SQL 查詢,定義及收集應用程式專屬指標。自訂指標會使用 ObservabilityConfig 自訂資源 (CR) 定義指標收集規則。
使用 ObservabilityConfig 設定自訂指標
ObservabilityConfig CR 主要包含兩個部分:dbClusterRefs 和 customMetrics。
dbClusterRefs
這個區段包含這項設定適用的 DBCluster 資源參照清單。每個 ObservabilityConfig 都必須以相同命名空間中的一個 DBCluster 為目標。
customMetrics
本節定義自訂指標收集作業的核心設定,包括資源限制和查詢定義。
資源限制 (resourceLimits)
為保護資料庫,系統會強制執行自訂查詢限制。如未在資訊清單中指定這些限制,系統會使用下表列出的預設值。
| 參數 | 說明 | 預設 | 最大值 | 單位 |
|---|---|---|---|---|
workMemory |
指定監控代理程式用來收集這些指標的特定資料庫連線 work_mem。這項設定是指標收集程序專用,不會影響 DBCluster 規格中設定的全域 work_mem 參數。 |
4MB |
不適用 | KB、MB (預設:KB) |
maxParallelWorkers |
指定監控代理程式所用特定資料庫連線的 max_parallel_workers_per_gather。如要停用平行查詢執行作業並盡量減少 CPU 影響,請將此值設為 0。這項設定只會影響指標收集程序,不會影響全域資料庫設定。 | 0 |
不適用 | 整數 |
statementTimeout |
為監控代理程式使用的特定資料庫連線指定 statement_timeout。這會限制單一指標查詢的執行時間上限。這項設定只會影響指標收集程序,不會影響全域資料庫設定。 | 2s |
30s |
ms、s (預設:ms) |
自訂指標定義 (definitions)
definitions 清單中的每個項目都會定義查詢,並說明如何解讀結果。
metricGroup:用於命名指標的專屬名稱 (小寫、數字、底線)。database:查詢的目標資料庫名稱。監控代理程式會與這個特定資料庫建立連線來執行查詢,因此查詢的結構定義必須存在於該資料庫中。query:有效的 SQLSELECT陳述式。僅允許SELECT項查詢。metrics:將 SQL 結果資料欄對應至 Prometheus 型別的清單:usage: label:將資料欄值做為 Prometheus 標籤。usage: gauge:將值匯出為 Prometheus 計量表指標。usage: counter:將值匯出為 Prometheus 計數器指標。
安全性和權限
AlloyDB Omni 運算子會使用 alloydbmonitor 使用者收集指標。根據預設,AlloyDB Omni operator 會使用 LOGIN 屬性建立這個使用者,並在 postgres 資料庫中授予 pg_monitor 角色。
新增自訂指標時,請確認使用者具備適當的額外權限:
- 使用者責任:資料庫管理員必須手動授予
alloydbmonitor使用者SELECT權限,才能存取自訂查詢中使用的任何特定應用程式資料表、檢視區塊或結構定義。 - 寫入權限安全檢查:為確保系統完整性並防止資料遭到意外修改,AlloyDB Omni 運算子會執行安全檢查。如果系統發現
alloydbmonitor使用者在目標資料庫中擁有任何寫入權限 (例如INSERT、UPDATE和DELETE),系統就會記錄錯誤,並拒絕從該資料庫收集自訂指標。
授予權限範例
如要授予名為 warehousedb 的資料庫中 public 結構定義的所有資料表唯讀存取權,請執行下列指令:
psql -h <var>DB_CLUSTER_ENDPOINT</var> -U <var>DB_ADMIN_USER</var> -d warehousedb
warehousedb=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO alloydbmonitor;
資訊清單範例
以下範例資訊清單會將監控代理程式設為連線至 postgres 資料庫,並使用 pg_stat_database 系統檢視畫面追蹤交易統計資料。
apiVersion: alloydbomni.dbadmin.goog/v1
kind: ObservabilityConfig
metadata:
name: obs-metrics
spec:
dbClusterRefs:
- dbcluster-sample
customMetrics:
resourceLimits:
workMemory: "4MB"
maxParallelWorkers: 0
definitions:
- metricGroup: database
database: "postgres"
query: |
SELECT
curr_db, xact_commit, xact_rollback
FROM pg_stat_database WHERE datname IS NOT NULL
metrics:
- name: curr_db
desc: "Database name"
usage: label
- name: xact_commit
desc: "Transactions committed"
usage: counter
- name: xact_rollback
desc: "Transactions rolled back"
usage: counter
指標參考資料
本節會參照自訂指標功能產生的指標。
產生的指標輸出內容
這個範例資訊清單會以 Prometheus 格式匯出指標,如下所示:
# HELP alloydb_omni_custom_database_xact_commit_total Transactions committed
# TYPE alloydb_omni_custom_database_xact_commit_total counter
alloydb_omni_custom_database_xact_commit_total{database="postgres",curr_db="testdb1",dbcluster="dbcluster-sample",dbcluster_type="Primary",dbinstance="n/a",dbinstance_type="n/a",dbnamespace="mc",dbnode="76d3-dbcluster-sample",dbnode_type="Primary"} 382069 1774388549568
# HELP alloydb_omni_custom_database_xact_rollback_total Transactions rolled back
# TYPE alloydb_omni_custom_database_xact_rollback_total counter
alloydb_omni_custom_database_xact_rollback_total{database="postgres",curr_db="testdb1",dbcluster="dbcluster-sample",dbcluster_type="Primary",dbinstance="n/a",dbinstance_type="n/a",dbnamespace="mc",dbnode="76d3-dbcluster-sample",dbnode_type="Primary"} 4364 1774388549568
標準標籤
每個自訂指標都會自動納入下列標準標籤:database、dbcluster、dbcluster_type、dbinstance、dbinstance_type、dbnamespace、dbnode 和 dbnode_type。如要進一步瞭解這些標籤,請參閱「AlloyDB Omni 指標標籤」
指標收集指標
這些指標會指出每個指標收集週期的狀態。您可以在監控代理程式容器記錄中,找到詳細的錯誤訊息,包括逾時或失敗的特定查詢。
# HELP alloydb_omni_monitor_custom_metrics_errors_total Total number of errors encountered during execution of the custom query
# TYPE alloydb_omni_monitor_custom_metrics_errors_total counter
alloydb_omni_monitor_custom_metrics_errors_total{metricGroup="database",dbcluster="dbcluster-sample",dbnode="...",...} 0 1773703411350
使用自訂指標前,請先考量下列事項:
- 只能使用
SELECT陳述式。系統會拒絕任何嘗試修改資料的陳述式。手動執行並驗證查詢結果和效能,再將這些內容納入自訂指標設定。 - 設計每個 SQL 查詢時,請盡量減少傳回的結果資料列數量。建議您指定少於五列,且只有一列。這樣一來,從查詢結果衍生的指標和標籤就不會導致基數過高,進而對監控系統的效能造成負面影響。
- 最佳化查詢,確保查詢不會耗用過多資源。使用
resourceLimits保護資料庫。 - 每項查詢都必須傳回標籤值組合不重複的資料列。