Prometheus:根据标签值进行条件并添加其他结果作为信息

发布于 2025-01-16 07:03:24 字数 599 浏览 2 评论 0原文

我有一个查询来执行

sum by(proxy, code) (rate(haproxy_server_http_responses_total{code=~"5xx|2xx",proxy="api_backend"}[10m]))

此查询的结果,

{code="2xx", proxy="api_backend"} value1
{code="5xx", proxy="api_backend"} value2 

我想仅在 value2 > 时触发警报x 并添加有关 value2 的额外信息。(让 value2 因为它是无条件的)

我已尝试此代码,但它不起作用

sum by(proxy, code) (rate(haproxy_server_http_responses_total{code="5xx",proxy="api_backend"}[10m])) > x or sum by(proxy, code) (rate(haproxy_server_http_responses_total{code="2xx",proxy="api_backend"}[10m]))

i have a query to perform

sum by(proxy, code) (rate(haproxy_server_http_responses_total{code=~"5xx|2xx",proxy="api_backend"}[10m]))

the result of this query

{code="2xx", proxy="api_backend"} value1
{code="5xx", proxy="api_backend"} value2 

i want to trigger the alert only when value2 > x and add extra information about value2.(let value2 as it is without condition)

I have trying this code but it is not working

sum by(proxy, code) (rate(haproxy_server_http_responses_total{code="5xx",proxy="api_backend"}[10m])) > x or sum by(proxy, code) (rate(haproxy_server_http_responses_total{code="2xx",proxy="api_backend"}[10m]))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2025-01-23 07:03:24

尝试以下 PromQL 查询:

(
  sum by(proxy)
  (rate(haproxy_server_http_responses_total{code="2xx",proxy="api_backend"}[10m]))
)
and
(
  sum by(proxy)
  (rate(haproxy_server_http_responses_total{code="5xx",proxy="api_backend"}[10m])) > x
)

对于 5xx 响应率高于 x 的代理,它应该返回 2xx 响应率。

它是如何运作的?

  • 上面查询中的 by(proxy, code) 被替换为 by(proxy)。这是因为 Prometheus 在 ab 端使用相同的标签集对时间序列执行 a op b 查询。在原始查询中,标签集并不相同,因为它们的 code 标签值不同。请参阅Prometheus 中二元运算符的匹配规则

  • or 运算符替换为 and 运算符,并且 rate(...{code="2xx"} 放入查询的前面,因为当 b 处存在具有相同标签集的匹配时间序列时,a 和 b 返回 a。一个href="https://prometheus.io/docs/prometheus/latest/querying/operators/#ological-set-binary-operators" rel="nofollow noreferrer">这些文档了解有关 and 运算符。

Try the following PromQL query:

(
  sum by(proxy)
  (rate(haproxy_server_http_responses_total{code="2xx",proxy="api_backend"}[10m]))
)
and
(
  sum by(proxy)
  (rate(haproxy_server_http_responses_total{code="5xx",proxy="api_backend"}[10m])) > x
)

It should return 2xx response rates for proxies with 5xx response rates higher than x.

How does it work?

  • The by(proxy, code) is substituted with by(proxy) in the query above. This is because Prometheus performs a op b query over time series with identical sets of labels at a and b sides. In the original query the set of labels weren't identical, because they differ by code label value. See matching rules for binary operators in Prometheus.

  • The or operator is substituted with and operator and the rate(...{code="2xx"} is put in the front of the query, since a and b returns a when there is a matching time series with the same set of labels at b. See these docs for details about or and and operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文