建立快訊規則

本頁面說明如何根據 Google Distributed Cloud (GDC) 實體隔離方案環境的記錄和指標建立警告規則,以便主動監控及加快事件應變速度。

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

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

指標規則和記錄規則都依賴查詢語言運算式來定義觸發快訊的條件。這個運算式會篩選及分析傳入資料,評估是否符合定義的條件。

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

為了提供更多背景資訊並簡化快訊管理作業,您可以為快訊規則新增標籤和註解:

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

    • 嚴重性等級 (錯誤、嚴重、警告)
    • 快訊代碼
    • 資源名稱
  • 註解:提供額外的非個人識別資訊,以擴充快訊內容。註解可能包含下列資訊:

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

事前準備

如要管理以指標為準以記錄為準的規則,您必須具備必要權限。

### 要求 以指標為準的規則適用的 IAM 機構角色

請與機構 IAM 管理員聯絡,要求下列角色:

  • MonitoringRule PA Creator (monitoringrule-pa-creator):建立 MonitoringRule 自訂資源。
  • MonitoringRule PA 編輯者 (monitoringrule-pa-editor):編輯或修改 MonitoringRule 自訂資源。
  • MonitoringRule PA 檢視者 (monitoringrule-pa-viewer):查看 MonitoringRule 自訂資源。

    要求 以指標為準的規則適用的 IAM 專案角色

請與專案 IAM 管理員聯絡,要求對方授予下列角色:

  • 監控規則編輯器 (monitoringrule-editor):編輯或修改專案命名空間中的 MonitoringRule 自訂資源。
  • 監控規則檢視者 (monitoringrule-viewer):在專案命名空間中查看 MonitoringRule 自訂資源。

要求 以記錄為準規則的 IAM 機構角色

請與機構 IAM 管理員聯絡,要求下列角色:

  • LoggingRule PA Creator (loggingrule-pa-creator):建立LoggingRule自訂資源。
  • LoggingRule PA 編輯者 (loggingrule-pa-editor):編輯或修改 LoggingRule 自訂資源。
  • LoggingRule PA Viewer (loggingrule-pa-viewer):查看 LoggingRule 自訂資源。

要求 以記錄為準規則的 IAM 專案角色

請與專案 IAM 管理員聯絡,要求對方授予下列角色:

  • 記錄規則編輯器 (loggingrule-editor):在專案命名空間中編輯或修改 LoggingRule 自訂資源。
  • LoggingRule 檢視者 (loggingrule-viewer):在專案命名空間中查看 LoggingRule 自訂資源。

定義快訊規則

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

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

控制台

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

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

    • 如要根據指標設定快訊規則,請選取「指標」
    • 選取「記錄」,查看以記錄為依據的快訊規則。
  6. 設定警告規則群組:

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

  7. 在「快訊規則」部分中,依序按一下 「新增規則」

  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:Management 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 定義檔案的名稱。