'aggregate of <aggregation>_over_time() in prometheus?

Prometheus has functions to e.g. get the max of a given range of data points or min or ... See the documentation for more info.

Now when I want min(data) and max(data) and avg, ... I need to send separate requests. How can I achieve this in one query?

[ update ]

From Brian's reply I see that my question is not clear (enough), so let me rephrase:

I have a timeseries "temperature" for a day in 15sec intervals. And I want for each hour (or last hour, doesn't really matter) have min/max/avg/stddev, ...

with the *_over_time() functions I need to send a query for each of min/max/avg/... (and the correct time interval). Is there a way of doing so in one single query (so basically returning a hash with k-v-pairs containing min/max/avg/...)?



Solution 1:[1]

min(min_over_time(metric[1h])) will let you do this.

[update]

It's best to send one query per function you want to calculate. You could do it in one with trickery, but it'd be much more complicated and hard to maintain.

Solution 2:[2]

It is possible to perform multiple PromQL queries in a single query with the help of label_replace function and or operator. The label_replace function is used for giving different names for every *_over_time() results, while or operator is used for combining multiple *_over_time() results into a single response.

For example, the following query returns min, max and avg values for the temperature time series over the last hour:

label_replace(min_over_time(temperature[1h]), "__name__", "min", "", ".*")
  or
label_replace(max_over_time(temperature[1h]), "__name__", "max", "", ".*")
  or
label_replace(avg_over_time(temperature[1h]), "__name__", "avg", "", ".*")

P.S. This query can be simplified into a smaller MetricsQL query via rollup function:

rollup(temperature[1h])

If other than min, max and avg results must be calculated, then aggr_over_time function may be used. For example, the following query calculates median, min and max for temperature over the last hour:

aggr_over_time(
  ("median_over_time", "min_over_time", "max_over_time"), 
  temperature[1h]
)

Additionally, MetricsQL provides alias and label_set functions, which may be easier to use than label_replace() when a name or a label must be set to a particular value. For example, the following MetricsQL query is equivalent to the PromQL query above:

alias(min_over_time(temperature[1h]), "min")
  or
alias(max_over_time(temperature[1h]), "max")
  or
alias(avg_over_time(temperature[1h]), "avg")

MetricsQL also supports union function, which may be used instead of or operator when multiple query results must be united into a single response:

union(
  alias(min_over_time(temperature[1h]), "min"),
  alias(max_over_time(temperature[1h]), "max"),
  alias(avg_over_time(temperature[1h]), "avg"),
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2