gpt4 book ai didi

prometheus - 如何优雅地避免在 Prometheus 中被零除

转载 作者:行者123 更新时间:2023-12-03 22:49:07 54 4
gpt4 key购买 nike

有时您需要将一个指标除以另一个指标。

例如,我想计算这样的平均延迟:

rate({__name__="hystrix_command_latency_total_seconds_sum"}[60s])
/
rate({__name__="hystrix_command_latency_total_seconds_count"}[60s])

如果在指定时间段内没有事件,则 rate()在分隔线中变成 0和除法的结果变成 NaN .
如果我对结果( avg()sum() 或其他)进行一些聚合,则整个聚合结果变为 NaN .

所以我在分频器中添加了一个零检查:
rate({__name__="hystrix_command_latency_total_seconds_sum"}[60s])
/
(rate({__name__="hystrix_command_latency_total_seconds_count"}[60s]) > 0)

这将删除 NaN s 来自结果向量。并且还将图表上的线条撕成碎片。

让我们用 0 标记不事件的时期使图形再次连续的值:
rate({__name__="hystrix_command_latency_total_seconds_sum"}[60s])
/
(rate({__name__="hystrix_command_latency_total_seconds_count"}[60s]) > 0)
or
rate({__name__="hystrix_command_latency_total_seconds_count"}[60s]) > bool 0

这有效地取代了 NaN s 与 0 , 图是连续的,聚合工作正常。

但是结果查询有点麻烦,尤其是当您需要进行更多标签过滤并对结果进行一些聚合时。类似的东西:
avg(
1000 * increase({__name__=~".*_hystrix_command_latency_total_seconds_sum", command_group=~"$commandGroup", command_name=~"$commandName", job=~"$service", instance=~"$instance"}[60s])
/
(increase({__name__=~".*_hystrix_command_latency_total_seconds_count", command_group=~"$commandGroup", command_name=~"$commandName", job=~"$service", instance=~"$instance"}[60s]) > 0)
or
increase({__name__=~".*_hystrix_command_latency_total_seconds_count", command_group=~"$commandGroup", command_name=~"$commandName", job=~"$service", instance=~"$instance"}[60s]) > bool 0
) by (command_group, command_name)

长话短说:有没有更简单的方法来处理除法器中的零?或者有什么常见的做法?

最佳答案

If there is no activity during the specified time period, the rate() in the divider becomes 0 and the result of division becomes NaN.



这是正确的行为, NaN 是您想要的结果。

aggregations work OK.



您无法汇总比率。您需要分别聚合分子和分母,然后进行除法。

所以:
   sum by (command_group, command_name)(rate(hystrix_command_latency_total_seconds_sum[5m]))
/
sum by (command_group, command_name)(rate(hystrix_command_latency_total_seconds_count[5m]))

关于prometheus - 如何优雅地避免在 Prometheus 中被零除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056557/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com