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

本页面介绍了如何将 Prometheus 提醒规则迁移到 Cloud Monitoring 具有基于 PromQL 的条件的提醒政策。 迁移过程非常有用 (如果您在 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 文件必须 格式为有效的 Prometheus 规则文件 或 JSON。

字段映射

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 提醒规则迁移到 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 利用率保持超过 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 会配置 针对 Managed Service for Prometheus 的代管式规则评估 规则 ClusterRulesGlobalRules 自定义资源。

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

  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

迁移多个规则自定义资源

如果您想迁移多个 Rules、CustomRule 或 GlobalRule 那么我们建议您针对每个 资源。有关详情,请参阅 Prometheus 文档

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

规则和 ClusterRules 会自动过滤其 Prometheus 提醒 基于特定标签的表达式。保留 迁移 Deployment 的 提醒政策,因此您必须在过滤器中明确添加这些标签,或 聚合表达式 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

下表显示了 alert-expression 过滤条件的评估顺序 :

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

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