從 Prometheus 遷移快訊規則和接收器

本頁面說明如何將 Prometheus 快訊規則遷移至 Cloud Monitoring 中的 PromQL 快訊政策。如果您在 Google Cloud以外的環境中使用 PromQL,遷移程序就會派上用場。將 Prometheus 快訊規則遷移至 Cloud Monitoring,即可在單一環境中管理所有快訊政策。

如要進一步瞭解以 PromQL 為基礎的快訊政策,請參閱「PromQL 快訊簡介」。

您也可以匯入 Grafana 資訊主頁。詳情請參閱「將 Grafana 資訊主頁匯入 Cloud Monitoring」一文。

遷移作業的運作方式

您可以在 gcloud 指令列介面中,遷移現有的快訊規則和接收器,以建立 Cloud Monitoring 快訊政策和通知管道。gcloud CLI 可執行下列類型的遷移作業:

  • 如果您提供含有警示規則的 Prometheus 設定檔,gcloud CLI 就會建立以 PromQL 為基礎的警示政策。

    您可以同時提供多個 Prometheus 設定檔,建立多個以 PromQL 為基礎的警示政策。

  • 如果您提供 Prometheus Alertmanager 檔案,且檔案至少包含一個接收器,gcloud 命令列會將每個接收器轉換為 Monitoring 中的等效通知管道。

  • 如果您同時提供 Prometheus Alertmanager 檔案和 Prometheus 設定檔,gcloud CLI 會建立快訊政策和通知管道,然後將每個新的通知管道與新的快訊政策建立關聯。

    如果您提供多個含有快訊規則的設定檔,gcloud CLI 會將 Alertmanager 檔案中的每個通知管道指派給每個新的快訊政策。

如果 Prometheus 警示規則參照自訂指標,gcloud CLI 就需要等價的指標描述元,才能建立警示政策。

Prometheus 設定檔和 Prometheus Alertmanager 檔案必須以 YAML 或 JSON 格式編寫為有效的 Prometheus 規則檔案

欄位對應

gcloud CLI 會將 Prometheus 警示規則和 Cloud Monitoring 警示政策之間的欄位對應如下:

Prometheus 快訊規則 Cloud Monitoring 快訊政策
alert conditionPrometheusQueryLanguage.alertRule
expr conditionPrometheusQueryLanguage.query
for conditionPrometheusQueryLanguage.duration
labels conditionPrometheusQueryLanguage.labels
annotations: "summary" documentation.subject
annotations: "description" documentation.content

Google Cloud 已遷移的 Prometheus 快訊規則中的說明變數

Prometheus 會使用快訊欄位範本,在快訊規則註解和標籤中設定變數。遷移含有快訊欄位範本的 Prometheus 快訊規則時,Cloud Monitoring 會將範本轉換為 Cloud Monitoring 說明文件變數,如下所示:

Prometheus 警示欄位範本 Cloud Monitoring 說明文件變數
{{$value}}

${metric.label.value}

這個變數是 ${metric.label.KEY} 的實作項目,其中 value 會保留 PromQL 查詢的值。

{{humanize $value}}

${metric.label.value}

這個變數是 ${metric.label.KEY} 的實作項目,其中 value 會保留 PromQL 查詢的值。

{{$labels}} ${metric_or_resource.labels}
{{humanize $labels}} ${metric_or_resource.labels}
{{$labels.key}}

在快訊政策說明文件設定中顯示為 ${metric_or_resource.label.KEY}

  • 如果 KEY 是有效的標籤,這個變數會在通知中顯示為 ${metric.label.KEY} 的值。
  • 如果 KEY 是有效的資源,這個變數會在通知中顯示為 ${resource.label.KEY} 的值。
  • 如果 KEY 既不是有效的標籤,也不是有效的資源,則這個變數會在通知中顯示為空字串。
{{humanize $labels.<key>}}

在快訊政策說明文件設定中顯示為 ${metric_or_resource.label.KEY}

  • 如果 KEY 是有效的標籤,這個變數會在通知中顯示為 ${metric.label.KEY} 的值。
  • 如果 KEY 是有效的資源,這個變數會在通知中顯示為 ${resource.label.KEY} 的值。
  • 如果 KEY 既不是有效的標籤,也不是有效的資源,則這個變數會在通知中顯示為空字串。

從 Prometheus 遷移快訊規則和接收器

如要將 Prometheus 快訊規則遷移至 Cloud Monitoring 的 PromQL 快訊政策,請使用 gcloud alpha monitoring policies migrate 指令。這個指令也能讓您將 Prometheus 接收器遷移至 Cloud Monitoring 通知管道。

  • 如要遷移警示規則,您必須擁有含有警示規則的 YAML 或 JSON Prometheus 規則檔案
  • 如要遷移接收器,您必須使用包含接收器的 YAML 或 JSON Prometheus Alertmanager 檔案。

Google Cloud 不支援遷移 Prometheus 錄製規則。如要使用錄製規則,建議您使用自行部署的規則評估工具

透過 Prometheus 設定檔建立快訊政策

如要透過 Prometheus 設定檔建立快訊政策,請執行下列指令。您可以提供多個檔案路徑,建立多個快訊政策:

gcloud alpha monitoring policies migrate \
--policies-from-prometheus-alert-rules-yaml=PATH_TO_FILE_1,PATH_TO_FILE_2

使用 Prometheus Alertmanager 設定檔建立通知管道

如要從 Prometheus Alertmanager 設定檔建立通知管道,請執行下列指令:

gcloud alpha monitoring policies migrate \
--channels-from-prometheus-alertmanager-yaml=PATH_TO_FILE

同時建立快訊政策和通知管道

如要從 Prometheus 設定檔建立快訊政策,並同時從 Prometheus Alertmanager 設定檔建立通知管道,請執行下列指令:

gcloud alpha monitoring policies migrate \
--policies-from-prometheus-alert-rules-yaml=PATH_TO_FILE_1,PATH_TO_FILE_2 \
--channels-from-prometheus-alertmanager-yaml=PATH_TO_FILE

範例

您有 Prometheus 設定檔,其中包含快訊規則,當 CPU 使用率在 1 分鐘內持續高於 75%,就會傳送快訊。警示規則的設定如下:

groups:
- name: Utilization_Alerts
  interval: 30s
  rules:
  - alert: CPU_Utilization_Over_75
    expr: compute_googleapis_com:instance_cpu_utilization > 75
    for: 60s
    labels:
      severity: page
    annotations:
      subject: "Subject: ${metric.label.value}"
      description: "CPU utilization is over 75%"

您也擁有 Prometheus AlertManager 檔案,其中包含電子郵件、PagerDuty 和 Webhook 接收器。

receivers:
- name: 'team-emails'
  email_configs:
  - to: EMAIL_ADDRESS
- name: 'team-pager'
  pagerduty_configs:
  - service_key: SERVICE_KEY
- name: 'team-webhook'
  webhook_configs:
  - url: WEBHOOK_ADDRESS

如要建立快訊政策和通知管道,請執行下列指令:

gcloud alpha monitoring policies migrate \
--policies-from-prometheus-alert-rules-yaml=PATH_TO_CONFIG_FILE \
--channels-from-prometheus-alertmanager-yaml=PATH_TO_ALERTMANAGER_FILE

接著,gcloud CLI 會建立快訊政策,如下所示:

  {
  "name": "projects/PROJECT_ID/alertPolicies/POLICY_ID",
  "displayName": "Utilization_Alerts/CPU_Utilization_Over_75",
  "documentation": {
    "content": "CPU utilization is over 75%",
    "mimeType": "text/markdown",
    "subject": "Subject: ${metric.label.value}"
  },
  "userLabels": {},
  "conditions": [
    {
      "name": "projects/PROJECT_ID/alertPolicies/POLICY_ID/conditions/CONDITION_ID",
      "displayName": "CPU_Utilization_Over_75"
      "conditionPrometheusQueryLanguage": {
        "alertRule": "CPU_Utilization_Over_75",
        "duration": "60s",
        "evaluationInterval": "30s",
        "labels": {
          "severity": "page"
        },
        "query": "compute_googleapis_com:instance_cpu_utilization > 75",
        "ruleGroup": "Utilization_Alerts"
      }
    }
  ],
  "alertStrategy": {},
  "combiner": "OR",
  "enabled": true,
  "notificationChannels": [
    "projects/google.com:PROJECT_ID/notificationChannels/CHANNEL_ID_1",
    "projects/google.com:PROJECT_ID/notificationChannels/CHANNEL_ID_2",
    "projects/google.com:PROJECT_ID/notificationChannels/CHANNEL_ID_3"
  ]
}

從 Managed Service for Prometheus 遷移快訊規則

Google Cloud 會使用 RulesClusterRulesGlobalRules 自訂資源,為 Managed Service for Prometheus 設定代管規則評估

如要將這些資源遷移至 Google Cloud 以 PromQL 為基礎的快訊政策,請按照下列步驟操作:

  1. 將自訂資源的 groups 部分複製到新的 Prometheus 規則檔案中。

  2. 使用 gcloud CLI 遷移規則檔案。

舉例來說,您有以下 Rules 自訂資源:

apiVersion: monitoring.googleapis.com/v1
kind: Rules
metadata:
  namespace: NAMESPACE_NAME
  name: example-rules
spec:
  groups:
  - name: example
    interval: 30s
    rules:
    - alert: FewerThanTenInstancesUp
      expr: sum(up) < 10

從先前的規則自訂資源複製 groups 後建立的 Prometheus 規則檔案如下所示:

  groups:
  - name: example
    interval: 30s
    rules:
    - alert: FewerThanTenInstancesUp
      expr: sum(up) < 10

遷移多個 Rules 自訂資源

如果您想遷移多個 Rules、CustomRules 或 GlobalRules,建議您為每個資源建立及遷移個別的規則檔案。詳情請參閱 Prometheus 說明文件

管理篩選器和匯總運算式

Rules 和 ClusterRules 會根據特定標籤自動篩選 Prometheus 警示運算式。如要在已遷移的警示政策查詢中保留這項行為,您必須先在 Prometheus 規則檔案的篩選器或匯總運算式中明確加入這些標籤,再遷移檔案。

舉例來說,Managed Service for Prometheus 的代管規則評估器會執行 sum(up) < 10 運算式,如下所示:

sum(up{project_id="test-project", cluster="test-cluster", namespace="NAMESPACE_NAME"}) < 10

不過,如果警告政策包含 PromQL 查詢 sum(up) < 10,Cloud Monitoring 會以 sum(up) < 10 的形式執行查詢。這項查詢會套用至指標範圍內的每個 Google Cloud 專案、叢集和命名空間。為避免這種情況發生,建議您在查詢中依標籤匯總,確保 Cloud Monitoring 能依預期執行查詢。舉例來說,如果您要透過規則自訂資源建立規則檔案,可以將 sum(up) < 10 變更為下列運算式:

sum (up) by (project_id, cluster, namespace) < 10

上述運算式會為每個Google Cloud 專案、叢集和命名空間產生單獨的時間序列,然後將每個時間序列與 <10 的警示門檻進行比較。

下表列出 Rules 和 ClusterRules 的警示式表達式篩選器評估順序:

規則資源類型 篩選標籤
規則
  1. project_id
  2. cluster
  3. namespace
ClusterRule
  1. project_id
  2. cluster

如要進一步瞭解 Prometheus 運算式中的篩選行為,請參閱「即時向量選取器」。