从 Prometheus 迁移提醒规则和接收器

本页面介绍了如何将 Prometheus 提醒规则迁移到具有基于 PromQL 的条件的 Cloud Monitoring 提醒政策。如果您在 Google Cloud 之外的环境中使用 PromQL,则迁移过程非常有用。通过将 Prometheus 提醒规则迁移到 Cloud Monitoring,您可以在一个环境中管理所有提醒政策。

如需了解有关使用基于 PromQL 条件的提醒政策的一般信息,请参阅使用 PromQL 提醒政策

您还可以导入 Grafana 信息中心。如需了解详情,请参阅将 Grafana 信息中心导入 Cloud Monitoring

迁移的工作原理

在 gcloud CLI 中,您可以迁移现有的提醒规则和接收器,以创建 Cloud Monitoring 提醒政策和通知渠道。gcloud CLI 可以执行以下类型的迁移:

  • 如果您为 Prometheus 配置文件提供提醒规则,则 gcloud CLI 会创建一个具有基于 PromQL 条件的提醒政策。

    您可以同时提供多个 Prometheus 配置文件,以创建具有基于 PromQL 的条件的多个提醒政策。

  • 如果您向 Prometheus Alertmanager 文件提供至少一个接收器,则 gcloud CLI 会将每个接收器转换为 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

迁移后的 Prometheus 提醒规则中的 Google Cloud 文档变量

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 提醒规则迁移到具有基于 PromQL 的条件的 Cloud Monitoring 提醒政策,请使用 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 利用率保持超过 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%"

此外,您还拥有一个包含电子邮件、PagerDuty 和 webhook 接收器的 Prometheus AlertManager 文件。

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 配置代管式规则评估

如需将这些资源迁移到具有基于 PromQL 的条件的 Google Cloud 提醒政策,请执行以下操作:

  1. 将自定义资源的 groups 部分复制到新的 Prometheus 规则文件中。

  2. 使用 gcloud CLI 迁移规则文件。

例如,您具有以下规则自定义资源:

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

迁移多个规则自定义资源

如果要迁移多个规则、CustomRule 或 GlobalRule,我们建议您为每个资源构建并迁移单独的规则文件。如需了解详情,请参阅 Prometheus 文档

管理过滤条件和聚合表达式

规则和 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 进行比较。

下表显示了规则和 ClusterRules 的提醒表达式过滤条件的评估顺序:

规则资源类型 过滤标签
规则
  1. project_id
  2. cluster
  3. namespace
ClusterRule
  1. project_id
  2. cluster

如需详细了解 Prometheus 表达式中的过滤行为,请参阅即时矢量选择器