PromQL avg() on a range vector

8.2k Views Asked by At

What is the meaning of applying avg() over a range vector such as:

avg(meric_name[1d]) 

As per the documentation here, it only operated on an instant vector. The metric_name[1d] returns a range vector. This query still runs over the range vector and returns a result without any complains about being wrong.

2

There are 2 best solutions below

0
On

The avg(m[d]) query doesn't work in Prometheus as Brian already noted, because this is valid MetricsQL query and is invalid PromQL query.

VictoriaMetrics automatically converts the avg(m[d]) query to avg(last_over_time(m[d])). The last_over_time(m[d]) returns the last raw values for m over d duration for every returned data point. Then the avg(...) calculates average for resulting time series.

The last_over_time() function could be useful for covering gaps in time series data if these gaps exceed step value passed to /api/v1/query_range.

See more details about last_over_time() function at MetricsQL docs.

1
On

This correctly returns a parse error:

Error executing query: invalid parameter "query": 1:5: parse error: expected type instant vector in aggregation expression, got range vector

which you can see yourself at http://demo.robustperception.io:9090/graph?g0.expr=avg(meric_name%5B1d%5D)%20&g0.tab=1&g0.stacked=0&g0.range_input=1h

So I suspect you were running a different query.