本页面介绍了如何创建基于 PromQL 的提醒政策 使用 Cloud Monitoring API 监控条件。您可以使用 PromQL 查询,以创建复杂的条件, 例如比率、动态阈值和指标评估等功能。
有关一般信息,请参阅 使用 PromQL 设置提醒政策。
如果您在 Cloud Monitoring 之外的 Prometheus 环境中工作,并且 Prometheus 提醒规则,那么您可以使用 Google Cloud CLI 使用 PromQL 将它们迁移到 Monitoring 提醒政策 查询。如需了解详情,请参阅 从 Prometheus 迁移提醒规则和接收器。
使用 PromQL 查询创建提醒政策
您可以使用 alertPolicies.create
方法以编程方式创建提醒政策。
使用 Cloud Build 创建提醒政策
基于 PromQL 的条件
您的Condition
类型必须为 PrometheusQueryLanguageCondition
。
此条件类型允许使用 PromQL 定义提醒政策。
下面显示了用于提醒政策的 PromQL 查询
条件,使用 kube-state
导出器中的指标查找数值
在过去 30 分钟内重启容器的次数:
rate(kube_pod_container_status_restarts[30m]) * 1800 > 1
构建提醒政策
要构建包含基于 PromQL 的条件的提醒政策,请使用
AlertPolicy
条件类型PrometheusQueryLanguageCondition
。
PrometheusQueryLanguageCondition
具有以下结构:
{ "query": string, "duration": string, "evaluationInterval": string, "labels": {string: string}, "ruleGroup": string, "alertRule": string }
PrometheusQueryLanguageCondition
字段具有以下定义:
query
:要计算的 PromQL 表达式。相当于expr
字段。duration
:指定播放期间的时长 因此,查询的每次评估都必须在true
提醒政策。该值必须是分钟数, (以秒为单位);例如600s
(时长 10 分钟)。如需了解详情,请参阅 基于指标的提醒政策的行为。evaluationInterval
:PromQL 之间的时间间隔(以秒为单位) 查询评估。 默认值是 30 秒。如果 “PrometheusQueryLanguageCondition
”是通过迁移 Prometheus 创建的 则这个值来自 包含 Prometheus 提醒规则。labels
:在 PromQL 中添加或覆盖标签的可选方法 表达式结果。ruleGroup
:如果提醒政策是从 Prometheus 迁移的 配置文件,那么此字段会包含name
字段的值 来自 Prometheus 中的规则组 配置文件如果您要为广告客户创建 Cloud Monitoring API 中的 PromQL 提醒政策。alertRule
:如果提醒政策是从 Prometheus 迁移的 配置文件,那么此字段会包含alert
字段的值 来自 Prometheus 中的提醒规则, 配置文件创建 Cloud Monitoring API 中的 PromQL 提醒政策。
例如,以下条件使用 PromQL 查询来 查找过去某个容器的上次重启次数 30 分钟:
"conditionPrometheusQueryLanguage": { "query": "rate(kube_pod_container_status_restarts[30m]) * 1800 > 1", "duration": "600s", evaluationInterval: "60s", "alertRule": "ContainerRestartCount", "labels": { "action_required":"true", "severity":"critical/warning/info"} }
使用此结构作为条件中 conditionPrometheusQueryLanguage
字段的值,而该条件又嵌入到提醒政策结构中。如需详细了解这些结构,请参阅
AlertPolicy
。
下面显示了包含 PrometheusQueryLanguageCondition
的完整政策
条件:
{ "displayName": "Container Restarts", "documentation": { "content": "Pod ${resource.label.namespace_name}/${resource.label.pod_name} has restarted more than once during the last 30 minutes.", "mimeType": "text/markdown", "subject": "Container ${resource.label.container_name} in Pod ${resource.label.namespace_name}/${resource.label.pod_name} has restarted more than once during the last 30 minutes." }, "userLabels": {}, "conditions": [ { "displayName": "Container has restarted", "conditionPrometheusQueryLanguage": { "query": "rate(kubernetes_io:container_restart_count[30m]) * 1800", "duration": "600s", evaluationInterval: "60s", "alertRule": "ContainerRestart", "labels": { "action_required":"true", "severity":"critical/warning/info"} } } ], "combiner": "OR", "enabled": true }
创建提醒政策
如需创建提醒政策,请将提醒政策 JSON 放入文件中 名为 POLICY_NAME.json,然后运行以下命令:
curl -d @POLICY_NAME.json -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -X POST https://monitoring.googleapis.com/v3/projects/${PROJECT}/alertPolicies
如需详细了解用于提醒政策的 Monitoring API,请参阅使用 API 管理提醒政策。
如需详细了解如何使用 curl
,请参阅调用 curl
。
使用 Terraform
有关如何使用
Terraform,请参阅condition_prometheus_query_language
google_monitoring_alert_policy
Terraform
注册表。
如需了解有关将 Google Cloud 与 Terraform 搭配使用的一般信息,请参阅将 Terraform 与 Google Cloud 搭配使用。
调用 curl
每个 curl
调用都包含一组参数,后跟 API 资源的网址。常见参数包括 Google Cloud 项目 ID 和身份验证令牌。这些值在此处由 PROJECT_ID
和 TOKEN
环境变量表示。
您可能还需要指定其他参数,例如,指定 HTTP 请求的类型(例如 -X DELETE
)。默认请求为 GET
,因此示例未指定该请求。
每个 curl
调用具有以下一般结构:
curl --http1.1 --header "Authorization: Bearer ${TOKEN}" <other_args> https://monitoring.googleapis.com/v3/projects/${PROJECT_ID}/<request>
要使用 curl
,您必须指定项目 ID 和访问令牌。为了减少输入和错误,您可以将它们放在环境变量中,以便以这种方式将它们传递给 curl
。
要设置这些变量,请执行以下操作:
创建环境变量来保存指标范围的范围限定项目的 ID。以下步骤将调用变量
PROJECT_ID
:PROJECT_ID=a-sample-project
向 Google Cloud CLI 进行身份验证:
gcloud auth login
可选。避免为每个
gcloud
指定项目 ID 命令,请使用 gcloud CLI 将项目 ID 设置为默认值:gcloud config set project ${PROJECT_ID}
创建授权令牌并将其存储到环境变量中。 以下步骤将调用变量
TOKEN
:TOKEN=`gcloud auth print-access-token`
您必须定期刷新访问令牌。如果命令突然报告您未通过身份验证,请重新发布此命令。
要验证您是否已获得访问令牌,请回显
TOKEN
变量:echo ${TOKEN} ya29.GluiBj8o....