建立快訊規則

本頁面說明如何根據 Google Distributed Cloud (GDC) 氣隙環境的記錄和指標建立快訊規則,以便主動監控及加快事件回應速度。

您可以根據專案的指標或記錄檔,在 GDC 中定義快訊規則。當符合特定條件時,這些規則會自動觸發快訊。您可以定義下列類型的快訊規則:

  • 以指標為準的規則:根據從應用程式或基礎架構收集的數值資料觸發快訊。舉例來說,您可以建立規則,在 CPU 使用率超過 80% 時觸發快訊。使用 GDC 控制台或MonitoringRule自訂資源定義建立以指標為依據的規則。
  • 以記錄檔為基礎的規則:根據記錄檔資料分析結果觸發快訊。您可以透過這些快訊,找出並回應記錄中的特定事件或模式,例如錯誤訊息或異常活動。使用 GDC 控制台或 LoggingRule 自訂資源定義,建立以記錄為依據的規則。

不論是根據指標或記錄檔的規則,都會使用查詢語言運算式定義觸發快訊的條件。這個運算式會篩選及分析傳入資料,評估是否符合定義的條件。

第一次符合條件時,快訊會轉換為「待處理」狀態。 如果條件在您定義的後續時間範圍內為 True,快訊就會進入「開啟」狀態。系統會在該時間點傳送快訊。

為了提供更多背景資訊,並方便您有效管理快訊,您可以為快訊規則新增標籤和註解:

  • 標籤:用於分類及識別快訊的鍵/值組合。標籤可用於下列資訊:

    • 嚴重程度 (錯誤、嚴重、警告)
    • 快訊代碼
    • 資源名稱
  • 註解:提供額外的非識別資訊,以豐富快訊內容。註解可能包含下列資訊:

    • 詳細錯誤訊息
    • 相關運算式
    • 執行手冊或疑難排解指南的連結

事前準備

如要取得管理以指標為準規則所需的權限,請要求機構 IAM 管理員或專案 IAM 管理員授予您相關的MonitoringRule資源角色。

另一方面,如要取得管理以記錄為準規則所需的權限,請要求機構 IAM 管理員或專案 IAM 管理員授予您相關聯的 LoggingRule 資源角色。

視存取層級和所需權限而定,您可能會在機構或專案中取得這些資源的建立者、編輯者或檢視者角色。詳情請參閱「準備 IAM 權限」。

定義快訊規則

您可以使用 GDC 控制台 (建議) 或監控及記錄 API,在專案命名空間中定義快訊規則,套用自訂資源。

選取下列其中一種方法,根據指標或記錄定義快訊規則:

主控台

在 GDC 控制台的規則群組中建立快訊規則:

  1. 在 GDC 控制台中選取專案。
  2. 在導覽選單中,依序選取「Operations」>「Alerting」
  3. 按一下「快訊政策」分頁標籤。
  4. 按一下「建立規則群組」
  5. 選擇快訊規則類型:

    • 如要根據指標設定快訊規則,請選取「指標」
    • 根據記錄檔選取快訊規則的「記錄檔」
  6. 設定快訊規則群組:

    • 在「快訊規則群組名稱」欄位中,輸入規則群組的名稱。
    • 在「規則評估間隔」欄位中,輸入每個間隔的秒數。
    • 在「限制」欄位中,輸入快訊數量上限。

  7. 在「Alert rules」(快訊規則) 區段中,按一下 「Add Rule」(新增規則)

  8. 在「建立快訊規則」視窗中,輸入下列詳細資料:

    • 快訊規則的名稱。
    • 快訊規則的運算式 (以記錄為基礎的規則使用 LogQL,以指標為基礎的規則使用 PromQL)。

    • 警報從「待處理」轉換為「開啟」前的時間長度 (以秒為單位)。

    • 嚴重性等級,例如「Error」或「Warning」

    • 用來識別相關資源的簡短名稱。

    • 用來識別快訊的快訊代碼。

    • 執行手冊網址或疑難排解資訊。

    • 快訊訊息或說明。

    • 選用:以鍵/值組合形式新增標籤註解

  9. 按一下 [儲存] 即可建立規則。

  10. 按一下「建立」即可建立規則群組。

規則群組會顯示在「快訊規則群組」清單中。您可以在這個規則群組中,將更多快訊規則分組。

API

透過監控或記錄 API 建立快訊規則:

  1. 在 YAML 檔案中定義 MonitoringRule (以指標為基礎的規則) 或 LoggingRule (以記錄為基礎的規則) 自訂資源。

    完整資源規格會顯示以指標和記錄為依據的規則範例。

  2. 根據需求替換 YAML 檔案中的下列值:

    欄位 說明
    namespace 專案命名空間。
    name 快訊規則設定的名稱。
    source 快訊規則的記錄來源。有效選項為 operationalaudit。僅適用於 LoggingRule 資源。
    interval 規則評估間隔的持續時間 (以秒為單位)。
    limit (選用) 快訊數量上限。如要設定無限次數的快訊,請將值設為 0
    alertRules 建立警告規則的定義。
    alertRules.alert 快訊名稱。
    alertRules.expr 記錄規則的 LogQL 運算式,或指標規則的 PromQL 運算式。運算式必須評估為 true 或 false 值,才能判斷快訊是否要轉換為待處理狀態。
    alertRules.for (選用) 警報從「待處理」轉換為「開啟」前的時間長度 (以秒為單位)。預設值為 0 秒 (立即觸發)。
    alertRules.labels 用來分類及識別快訊的鍵/值組合。需要以下標籤:severitycoderesource
    alertRules.annotations (選用) 以鍵/值組合形式提供警報的非識別中繼資料。
  3. 儲存 YAML 檔案。

  4. 在與指標或記錄警報規則相同的命名空間中,將資源設定套用至 Management API 伺服器:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f ALERT_RULE_NAME.yaml
    

    更改下列內容:

    • KUBECONFIG_PATH:管理 API 伺服器的 kubeconfig 檔案路徑。
    • ALERT_RULE_NAMEMonitoringRuleLoggingRule 定義檔案的名稱。

完整資源規格

本節提供 YAML 範本,您可套用自訂資源,建立以指標為基礎和以記錄為基礎的警告規則。如果您是透過 GDC 控制台建立快訊,可以略過這個部分。

在下列自訂資源中定義快訊規則:

  • MonitoringRule:以指標為準的規則。
  • LoggingRule:以記錄為準的規則。

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 metric-based alert rules.
  alertRules:
    # Define an alert name.
  - alert: my-metric-based-alert

    # Define the PromQL expression to evaluate for this rule.
    expr: rate({service_name="bob-service"} [1m])

    # The duration in seconds before an alert transitions from pending to open.
    # Optional.
    # Default value: '0s'
    for: 0s

    # Define labels to add or overwrite.
    # Map of key-value pairs.
    # Required labels:
    #     severity: [error, critical, warning, info]
    #     code:
    #     resource: component/service/hardware related to the alert
    # Additional labels are optional.
    labels:
      severity: error
      code: 202
      resource: AIS
      another-label: another-value

    # Define annotations to add.
    # Map of key-value pairs.
    # Optional.
    # Recommended annotations:
    #     message: value of the Message field in the user interface.
    #     expression: value of the Rule field in the user interface.
    #     runbookurl: URL of the Actions to take field in the user interface.
    annotations:
      message: my-alert-message

更改下列內容:

  • PROJECT_NAMESPACE:您的專案命名空間。
  • MONITORING_RULE_NAMEMonitoringRule 定義檔案的名稱。

LoggingRule

下列 YAML 檔案顯示 LoggingRule 自訂資源的範本。詳情請參閱 API 參考說明文件

# Configures either an alert or a target record for precomputation.
apiVersion: logging.gdc.goog/v1
kind: LoggingRule
metadata:
  # Choose a namespace that matches the project namespace.
  # The alert or record is produced in the same namespace.
  namespace: PROJECT_NAMESPACE
  name: LOGGING_RULE_NAME
spec:
  # Choose the log source to base alerts on (operational or audit logs).
  # Optional.
  # Valid options: 'operational' and 'audit'
  # Default value: 'operational'
  source: operational

  # 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 log-based alert rules.
  alertRules:
    # Define an alert name.
  - alert: my-log-based-alert

    # Define the LogQL expression to evaluate for this rule.
    expr: rate({service_name="bob-service"} [1m])

    # The duration in seconds before an alert transitions from pending to open.
    # Optional.
    # Default value: '0s'
    for: 0s

    # Define labels to add or overwrite.
    # Map of key-value pairs.
    # Required labels:
    #     severity: [error, critical, warning, info]
    #     code:
    #     resource: component/service/hardware related to the alert
    # Additional labels are optional.
    labels:
      severity: warning
      code: 202
      resource: AIS
      another-label: another-value

    # Define annotations to add.
    # Map of key-value pairs.
    # Optional.
    # Recommended annotations:
    #     message: value of the Message field in the user interface.
    #     expression: value of the Rule field in the user interface.
    #     runbookurl: URL of the Actions to take field in the user interface.
    annotations:
      message: my-alert-message

更改下列內容:

  • PROJECT_NAMESPACE:您的專案命名空間。
  • LOGGING_RULE_NAMELoggingRule 定義檔案的名稱。