普罗米修斯:如何计算单个值随时间变化的比例

the*_*use 5 prometheus

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

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.

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

bri*_*zil 6

如果该值只能为 0 或 1,则可以使用avg_over_time.

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

my_metric_nonzero = my_metric != bool 0
Run Code Online (Sandbox Code Playgroud)

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

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