本頁面說明如何在 Google Distributed Cloud (GDC) Air-gapped 中部署 MonitoringRule
自訂資源,從現有指標產生新指標。
記錄規則可讓您預先計算常用或運算成本高昂的運算式,進而提升資訊主頁和快訊的效能。這些規則定義於MonitoringRule
自訂資源中,可從現有指標產生新的時間序列指標,提升資料可觀測性。
記錄規則會儲存預先計算的結果,因此每次需要運算式時,都不會重複計算。這個方法可大幅加快查詢速度,特別是對於需要重複評估相同運算式的複雜資訊主頁和快訊。基本上,MonitoringRule
資源可讓您在記錄規則中指定必要計算,從現有指標衍生出新指標。
事前準備
如要取得管理 MonitoringRule
自訂資源所需的權限,請要求機構 IAM 管理員或專案 IAM 管理員授予您相關的 MonitoringRule
角色。
視存取層級和所需權限而定,您可能會在機構或專案中取得這項資源的建立者、編輯者或檢視者角色。詳情請參閱「準備 IAM 權限」。
建立錄製規則
如要建立記錄規則,請為記錄的指標定義名稱,並提供可產生數值的有效運算式。這個運算式在評估時會產生新的指標。在 Management API 伺服器上,於專案命名空間中部署 MonitoringRule
自訂資源,以啟用記錄規則。
如要進一步瞭解記錄規則,請參閱 https://grafana.com/docs/loki/latest/rules/。
請按照下列步驟,在專案命名空間中建立記錄規則:
- 找出要建立錄音規則的 GDC 專案。
建立定義
MonitoringRule
自訂資源的 YAML 檔案。完整的
MonitoringRule
規格會顯示以指標為準的記錄規則範例。詳情請參閱 API 參考說明文件。根據需求替換 YAML 檔案中的下列值:
欄位名稱 說明 namespace
專案命名空間。 name
規則設定的名稱。 interval
規則評估間隔的持續時間 (以秒為單位)。 limit
選填。快訊數量上限。如要設定無限次數的快訊,請將值設為 0
。recordRules
計算新指標的定義。 recordRules.record
新指標的記錄名稱,這個值必須是有效的指標名稱,用來定義儲存結果的時間序列。 recordRules.expr
指標規則的 PromQL 運算式,必須評估為數值。 recordRules.labels
選填。要新增至新指標或覆寫新指標的標籤鍵/值組合。 儲存 YAML 檔案。
在與記錄規則相同的命名空間中,將
MonitoringRule
設定套用至 Management API 伺服器:kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_RULE_NAME.yaml
更改下列內容:
KUBECONFIG_PATH
:管理 API 伺服器的 kubeconfig 檔案路徑。MONITORING_RULE_NAME
:MonitoringRule
定義檔案的名稱。
完成MonitoringRule
規格
MonitoringRule
自訂資源包含記錄規則,用於說明根據現有指標建立新指標的條件,以利觀測。
下列 YAML 檔案顯示 MonitoringRule
自訂資源的範本。詳情請參閱 API 參考說明文件。
# Configures either an alert or a target record for precomputation.
apiVersion: monitoring.gdc.goog/v1
kind: MonitoringRule
metadata:
# Choose a namespace that matches the project namespace.
# The alert or record is produced in the same namespace.
namespace: PROJECT_NAMESPACE
name: MONITORING_RULE_NAME
spec:
# Rule evaluation interval.
interval: 60s
# Configure the limit for the number of alerts.
# A value of '0' means no limit.
# Optional.
# Default value: '0'
limit: 0
# Configure recording rules to generate new metrics based on existing metrics.
# Recording rules precompute expressions that are frequently needed or computationally expensive.
# Results are saved as a new set of time series.
recordRules:
# Define the time series where you want to write the recording rule.
# The value must be a valid metric name.
- record: MyMetricsName
# Define the PromQL expression to evaluate for this rule.
expr: rate({service_name="bob-service"} [1m])
# Define labels to add or overwrite.
# Map of key-value pairs.
# Optional.
labels:
verb: read
更改下列內容:
PROJECT_NAMESPACE
:您的專案命名空間。MONITORING_RULE_NAME
:MonitoringRule
定義檔案的名稱。