首页 > 解决方案 > Prometheus - 使用 telegraf http_response 插件在 http 错误的情况下创建警报

问题描述

我正在使用 Telegraf 和 Prometheus 来监控我的本地服务,例如 OpenHab 和我的 Grafana 实例。

http_response 插件可能会产生以下结果:

http_response_http_response_code{host="master-pi",instance="192.168.2.15:9126",job="telegraf-master-pi",method="GET",result="success",result_type="success",server="http://www.grafana.local",status_code="200"}    200
http_response_http_response_code{host="master-pi",instance="192.168.2.15:9126",job="telegraf-master-pi",method="GET",result="success",result_type="success",server="http://www.grafana.local",status_code="502"}    502
http_response_http_response_code{host="master-pi",instance="192.168.2.15:9126",job="telegraf-master-pi",method="GET",result="success",result_type="success",server="http://www.thuis.local/start/index",status_code="200"} 200

现在我想要一个警报,只要过去 30 分钟的 !200 status_code 计数高于 200 status_code 计数,它就会通知我。

我开始很简单:

alert: service_down_external
expr: http_response_http_response_code{status_code!~"200|302"}
for: 35m
labels:
  severity: high

这很好用,但问题是这不适用于我不是每 10 秒而是每 5 到 30 分钟监控一次的服务(因为我想减少某些 API 的负载)。

所以我想,让我们尝试另一种方式:

expr: count_over_time(http_response_http_response_code{status_code!~"200|302"}[30m]) > on(job, instance, method, server) count_over_time(http_response_http_response_code{status_code=~"200|302"}[30m])

这看起来很有希望,但不幸的是,如果根本没有 200/302 响应,则将无法正常工作,在这种情况下会返回“无数据”。

所以我不过,让我们把它除以总量:

count_over_time(http_response_http_response_code{status_code!~"200|302"}[300m]) > on(job, instance, method, server) count_over_time(http_response_http_response_code[300m])

但是,这导致:

Error executing query: found duplicate series for the match group {instance="192.168.2.15:9126", job="telegraf-master-pi", method="GET", server="http://www.grafana.local/series"} on the right hand-side of the operation: [{host="master-pi", instance="192.168.2.15:9126", job="telegraf-master-pi", method="GET", result="success", result_type="success", server="http://www.grafana.local/series", status_code="502"}, {host="master-pi", instance="192.168.2.15:9126", job="telegraf-master-pi", method="GET", result="success", result_type="success", server="http://www.grafana.local/series", status_code="200"}];many-to-many matching not allowed: matching labels must be unique on one side

同样在尝试忽略时:

count_over_time(http_response_http_response_code{status_code!~"200|302"}[30m]) >ignoring(status_code) count_over_time(http_response_http_response_code[30m])

发生同样的错误。

每当 http 响应在过去 30 分钟内仅返回 5xx 错误时,是否有其他方法可以提醒我?

标签: prometheustelegrafprometheus-alertmanagertelegraf-inputs-plugin

解决方案


6 个月后,又一次尝试解决这个问题,我终于想出了一个查询,它给了我预期的结果:

count_over_time(http_response_result_code{result!~"success"}[2h]) / on(job, instance, method, server, type) group_left() sum by(job, instance, method, server, type) (count_over_time(http_response_result_code[2h])) >= 0.5

部分求和解决了“找到匹配组的重复序列”,因为它将对所有重复序列求和(例如,所有带有“response_string_mismatch”和“success”的结果)

group_left 选择查询的左侧部分,因此我仍然可以在警报中使用 result_type 标签。右侧部分仅包含 sum by 中提到的 5 个字段。

最后,查询将给我在过去 2 小时内未成功的呼叫百分比,这正是我所需要的。


推荐阅读