建立快訊規則

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

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

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

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

首次符合條件時,快訊會轉換為「pending」狀態。如果條件在您定義的下列時間長度內為 true,警告就會進入「open」狀態。系統就會在該時間點傳送快訊。

如要提供更多背景資訊,並有效管理警告,您可以為警告規則新增標籤和註解:

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

    • 嚴重性等級 (錯誤、重大、警告)
    • 警示代碼
    • 資源名稱
  • 註解:提供額外的非個人識別資訊,讓警告更豐富。註解可包含下列資訊:

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

事前準備

如要管理指標型或記錄檔型規則,您必須具備必要權限。

### 要求依據指標規則的 IAM 機構角色

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

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

    要求指標型規則的 IAM 專案角色

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

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

要求記錄型規則的 IAM 組織角色

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

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

要求記錄型規則的 IAM 專案角色

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

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

定義警告規則

您可以使用 GDC 控制台 (建議) 或 Monitoring 和 Logging API 套用自訂資源,在專案命名空間中定義警告規則。

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

控制台

透過 GDC 控制台,在規則群組中建立警告規則:

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

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

    • 在「Alert rule group name」(警告規則群組名稱) 欄位中,輸入規則群組的名稱。
    • 在「Rule evaluation interval」(規則評估間隔) 欄位中,輸入每個間隔的秒數。
    • 在「Limit」(上限) 欄位中,輸入快訊數量上限。

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

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

    • 警告規則的名稱。
    • 警告規則的運算式 (記錄規則使用 LogQL,指標規則使用 PromQL)。

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

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

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

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

    • Runbook 網址或疑難排解資訊。

    • 快訊訊息或說明。

    • 選用:以鍵/值組合的形式新增「Labels」(標籤) 和「Annotations」(註解)。

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

  10. 按一下「Create」(建立),建立規則群組。

規則群組會顯示在「Alert rule group」(警告規則群組) 清單中。您可以在這個規則群組中,將更多警告規則分組。

API

透過 Monitoring 或 Logging API 建立警告規則:

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

    完整的資源規格顯示指標型和記錄型規則的範例。

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

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

  4. 將資源設定套用至 Management API 伺服器,並與指標型或記錄型警告規則位於相同命名空間:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f ALERT_RULE_NAME.yaml
    

    更改下列內容:

    • KUBECONFIG_PATH:Management API 伺服器的 kubeconfig 檔案路徑。
    • ALERT_RULE_NAME:MonitoringRule 或 LoggingRule 定義檔案的名稱。

完整資源規格

本節提供 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_NAME:MonitoringRule 定義檔案的名稱。

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