基于指标的告警

在本教程中,我们将根据之前在 为 Go 中编写的 HTTP 服务器添加插桩 教程中插桩的 ping_request_count 指标创建告警。

在本教程中,当 ping_request_count 指标大于 5 时,我们将触发告警。请查看实际的 最佳实践 以了解更多关于告警原则的信息。

这里 下载适用于您操作系统的最新版本Alertmanager。

Alertmanager支持各种接收器,如emailwebhookpagerdutyslack等,通过这些接收器可以在警报触发时发送通知。您可以在这里找到接收器列表及其配置方法。在本教程中,我们将使用webhook作为接收器,请访问webhook.site 并复制我们将用于配置Alertmanager的webhook URL。

首先,让我们设置Alertmanager并启用webhook接收器。

alertmanager.yml

global:
  resolve_timeout: 5m
route:
  receiver: webhook_receiver
receivers:
    - name: webhook_receiver
      webhook_configs:
        - url: '<INSERT-YOUR-WEBHOOK>'
          send_resolved: false

在alertmanager.yml文件中,将<INSERT-YOUR-WEBHOOK>替换为您之前复制的webhook,并使用以下命令运行Alertmanager。

alertmanager --config.file=alertmanager.yml

Alertmanager启动并运行后,导航到https://:9093 ,您应该能够访问它。

现在我们已经配置了带有webhook接收器的Alertmanager,让我们将规则添加到Prometheus配置中。

prometheus.yml

global:
 scrape_interval: 15s
 evaluation_interval: 10s
rule_files:
  - rules.yml
alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - localhost:9093
scrape_configs:
 - job_name: prometheus
   static_configs:
       - targets: ["localhost:9090"]
 - job_name: simple_server
   static_configs:
       - targets: ["localhost:8090"]

请注意,evaluation_intervalrule_filesalerting部分已添加到Prometheus配置中。evaluation_interval定义了规则评估的时间间隔,rule_files接受一个yaml文件数组,这些文件定义了规则,而alerting部分定义了Alertmanager的配置。如本教程开头所述,我们将创建一个基本规则,当ping_request_count值大于5时触发警报。

rules.yml

groups:
 - name: Count greater than 5
   rules:
   - alert: CountGreaterThan5
     expr: ping_request_count > 5
     for: 10s

现在,让我们使用以下命令运行Prometheus。

prometheus --config.file=./prometheus.yml

在浏览器中打开https://:9090/rules 以查看规则。接下来,运行已 instrumented的ping服务器并访问https://:8090/ping 端点,并至少刷新页面6次。您可以通过访问https://:8090/metrics 端点来检查ping计数。要查看警报状态,请访问https://:9090/alerts 。一旦条件ping_request_count > 5持续超过10秒,state将变为FIRING。现在,如果您返回到您的webhook.site URL,您将看到警报消息。

类似地,Alertmanager也可以配置其他接收器,以便在警报触发时发送通知。

本页内容