Search code examples
prometheuspromql

In Prometheus,how to use comparison operators with range vectors?


prometheus has a function count_over_time() to get "the count of all values in the specified interval".

but I want some of the values and not all of them.

like below:

count_over_time(up)[1h] == 0

prometheus does not support this and will report an error: Error executing query: invalid parameter "query": 1:20: parse error: ranges only allowed for vector selectors.

Or, is there a way to implement the count_ne_over_time() function in MetricsQL in promQL?


Solution

  • Contrary to VictoriaMetrics, Prometheus doesn't provide count_ne_over_time function. But you can use the following trick if you need calculating the number of raw samples with zero values over the previous hour for up metric in Prometheus:

    (1 - avg_over_time(up[1h]))
      *
    count_over_time(up[1h])
    

    This is equivalent to the following MetricsQL query:

    count_eq_over_time(up[1h], 0)
    

    Note that this trick works only if the up metric contains 1 and 0 discrete values. If the up metric contains other values than 0 and 1, then the PromQL trick above won't return the expected results.