Search code examples
prometheuspromql

PromQL avg_over_time for non-zero values


I am trying to get avg_over_time value from gauge metric, but I would want average only from the non-zero values of the metric (or values higher than zero, to be exact).

Example:

avg_over_time(foo[2d] > 0)

But I alwas get parse error: binary expression must contain only scalar and instant vector types

I tried setting up recording rule

expr: foo > 0

But unfortunately with the same result.

Is this possible in PromQL?


Solution

  • You can use a sub-query with Prometheus version above 2.7:

    avg_over_time((foo > 0)[2d:])