計算指標是指根據現有記錄或指標,為資料觀測能力產生新指標。如要在 GDC 中計算指標,請在MonitoringRule
自訂資源中設定記錄規則。記錄規則會預先計算經常需要或運算成本高昂的運算式。這些規則會將結果儲存為新的時間序列集。查詢預先計算的結果,比在需要時執行原始運算式更快。這項預先計算功能特別適合用於資訊主頁和複雜快訊,因為這類項目每次重新整理或評估時,都必須重複查詢相同的運算式。
您可以在 MonitoringRule
自訂資源中設定記錄規則,根據其他系統監控指標計算指標。MonitoringRule
自訂資源包含記錄規則,說明根據現有指標計算新指標的條件。
事前準備
如要取得從指標計算指標所需的權限,請要求專案 IAM 管理員授予下列其中一個角色:
- 監控規則編輯器:編輯或修改
MonitoringRule
自訂資源。要求「Monitoring 規則編輯者」(monitoringrule-editor
) 角色。 - 監控規則檢視者:可檢視
MonitoringRule
自訂資源。要求 Monitoring Rule Viewer (monitoringrule-viewer
) 角色。
建立錄製規則
定義記錄名稱和評估規則的有效運算式。運算式必須解析為數值,才能記錄為新指標。在管理員叢集的專案命名空間中部署自訂資源,建立記錄規則。
如要進一步瞭解記錄規則,請參閱 https://grafana.com/docs/loki/latest/rules/
請按照下列步驟,在專案的命名空間中建立記錄規則並計算指標:
- 使用
MonitoringRule
CR 範本開啟或建立 CR 的 YAML 檔案,以監控記錄規則。 - 在 CR 的
namespace
欄位中,輸入專案的命名空間。 - 在
name
欄位中,輸入規則設定的名稱。 - 選用:您可以在
source
欄位中選擇記錄來源。有效選項為operational
和audit
。 - 在
interval
欄位中,輸入規則評估間隔的秒數。 - 選用:在「」
limit
欄位中,輸入快訊數量上限。如要設定無限次數的快訊,請輸入0
。 在「
recordRules
」欄位中輸入下列資訊,即可計算指標:- 在
record
欄位中輸入記錄名稱。這個值會定義要寫入記錄規則的時間序列,且必須是有效的指標名稱。 在
expr
欄位中,輸入指標規則的 PromQL 運算式。這個運算式必須解析為數值,才能記錄為新指標。
選用:在
labels
欄位中,以鍵/值組合的形式定義要新增或覆寫的標籤。
- 在
儲存 CR 的 YAML 檔案。
在管理員叢集的專案命名空間中部署 CR,建立記錄規則。
MonitoringRule
記錄規則
MonitoringRule
自訂資源包含記錄規則,可根據預先存在的系統監控指標,說明計算新指標的條件。
以下 YAML 檔案顯示 MonitoringRule
CR 的 recordRules
欄位範本。
# Configures either an alert or a target record for precomputation
apiVersion: monitoring.gdc.goog/v1alpha1
kind: MonitoringRule
metadata:
# Choose namespace that matches the project's namespace
# Note: The alert or record will be produced in the same namespace
namespace: PROJECT_NAMESPACE
name: alerting-config
spec:
# Rule evaluation interval
interval: <duration>
# Configure limit for number of alerts (0: no limit)
# Optional, Default: 0 (no limit)
limit: <int>
# Configure record rules to generate new metrics based on pre-existing metrics.
# Record rules precompute expressions that are frequently needed or computationally expensive.
# These rules save their result as a new set of time series.
recordRules:
# Define which timeseries to write to (must be a valid metric name)
- record: <string>
# Define PromQL expression to evaluate for this rule
expr: <string>
# Define labels to add or overwrite
# Optional, Map of {key, value} pairs
labels:
<labelname>: <labelvalue>
...
將 PROJECT_NAMESPACE 替換為專案的命名空間。
LoggingRule
記錄規則
LoggingRule
CR 包含記錄規則,可說明根據現有記錄計算新指標的條件。
以下 YAML 檔案顯示 LoggingRule
CR 的 recordRules
欄位範本。
# Configures either an alert or a target record for precomputation
apiVersion: logging.gdc.goog/v1alpha1
kind: LoggingRule
metadata:
# Choose namespace that matches the project's namespace
# Note: The alert or record will be produced in the same namespace
namespace: g-fleetns-a
name: alerting-config
spec:
# Choose which log source to base alerts on (Operational/Audit/Security Logs)
# Optional, Default: Operational
source: <string>
# Rule evaluation interval
interval: <duration>
# Configure limit for number of alerts (0: no limit)
# Optional, Default: 0 (no limit)
limit: <int>
# Configure record rules to generate new metrics based on pre-existing logs.
# Record rules generate metrics based on logs.
# Use record rules for complex alerts, which query the same expression repeatedly every time they are evaluated.
recordRules:
# Define which timeseries to write to (must be a valid metric name)
- record: <string>
# Define LogQL expression to evaluate for this rule
# https://grafana.com/docs/loki/latest/rules/
expr: <string>
# Define labels to add or overwrite
# Optional, Map of {key, value} pairs
labels:
<labelname>: <labelvalue>
...