【问题标题】:Prometheus: How to calculate proportion of single value over timePrometheus:如何计算单个值随时间的比例
【发布时间】:2017-07-20 11:44:22
【问题描述】:

我想计算给定指标在时间范围内非零的时间百分比。我知道我可以使用

获得该时间范围内的值的数量

count_over_time(my_metric[1m])

但我想要的是类似

count_over_time(my_metric[1m] != 0) / count_over_time(my_metric)

我不能这样做,因为binary expression must contain only scalar and instant vector types

有没有办法做我正在尝试的事情?

【问题讨论】:

    标签: prometheus


    【解决方案1】:

    如果值只能是0或1,那么可以使用avg_over_time

    如果它可以有其他值,则需要通过记录规则将其转换为0或1:

    my_metric_nonzero = my_metric != bool 0
    

    然后你就可以avg_over_time(my_metric_nonzero[1m])

    另见https://www.robustperception.io/composing-range-vector-functions-in-promql/

    【讨论】:

    • 谢谢,效果很好。出于兴趣,为什么我可以获取这个记录的指标范围但不能在查询中内联:avg_over_time((my_metric != bool 0)[1m])
    • my_metric != bool 0 根据当前数据返回一个即时向量,[1m] 范围不能应用于该向量。
    • @brian-brazil 你的意思可能是avg_over_time(my_metric_nonzero[1m]) ?
    【解决方案2】:

    从 Prometheus 2.7 开始,you can do this using a subquery

    具体来说,以下查询将使用过去 1 小时的平均值告诉您查询非零的时间百分比。

    avg_over_time((my_metric[1m] != bool 0)[1h:])

    请注意,链接教程建议您仍然使用记录规则“以避免在每个评估间隔重新计算整个小时的子查询输出。”

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2022-09-26
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2020-09-11
      相关资源
      最近更新 更多