本頁說明如何在 Google Distributed Cloud (GDC) 無網路連線裝置中部署 MonitoringRule 自訂資源,從現有指標產生新指標。
記錄規則可讓您預先計算常用或運算成本高昂的運算式,進而提升資訊主頁和快訊的效能。這些規則定義於MonitoringRule自訂資源中,可從現有指標產生新的時間序列指標,提升資料可觀測性。
記錄規則會儲存預先計算的結果,因此每次需要運算式時,都不會重複計算。這個方法可大幅加快查詢速度,特別是對於需要重複評估相同運算式的複雜資訊主頁和快訊。基本上,MonitoringRule資源可讓您在記錄規則中指定必要計算,從現有指標衍生出新指標。
事前準備
如要取得管理 MonitoringRule 自訂資源所需的權限,請要求機構 IAM 管理員或專案 IAM 管理員授予您相關的 MonitoringRule 角色。
視存取層級和所需權限而定,您可能會在機構或專案中取得這項資源的建立者、編輯者或檢視者角色。詳情請參閱「準備 IAM 權限」。
建立錄製規則
如要建立記錄規則,請定義記錄指標的名稱,以及產生數值的有效運算式。這個運算式在評估時會產生新的指標。在 Management API 伺服器上,將自訂資源部署至專案命名空間,即可建立記錄規則。
如要進一步瞭解記錄規則,請參閱 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定義檔案的名稱。
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>
...