Search code examples
prometheuspromql

Prometheus getting an average count of datapoints


I'm trying to get the avg_over_time for a count. My basic constructor is:

avg_over_time(count(foo > 10)[1h:30s])

which returns an error:

range specification must be preceded by a metric selector, but follows a *promql.AggregateExpr instead

The count (foo > 10) returns a scalar each time it's called. The expected behavior would be to sample the data every 30s and return the average over an hours period of time. There are several examples on SO such as here and here. This second example is basically what I'm trying to do, but instead of returning the value of foo I just want to return the number of foo greater than 10. The difference being the use of count which is what I think is throwing the error.

I ran across this post with a solution - It is looking to get the count over a period of time which I think is different than getting the average count for a time period (?). Regardless, I couldn't get that construction to work either.

Asked Another Way

If the count() call is working correctly - how do i get the average count over a period of time. After working on this for another day or so, it isn't obvious to me.


Solution

  • Your Prometheus query is valid. But subqueries were introduced in Prometheus 2.7.

    a subquery is a part of a query, and allows you to do a range query within a query

    With Prometheus 2.1, you can work around that by using recording rules.