max_over_time function by providing the metric and specifying a 10-minute range.
Now consider a counter metric where the goal is to compute the maximum rate over the past 10 minutes. An initial approach might be to execute the rate function over the metric and then apply max_over_time. For example:
rate function over a 10-minute period and then attempt to find the maximum value of that rate. However, this approach does not work directly because the rate function returns an instant vector, while max_over_time expects a range vector. For instance, consider the queries:
Remember: the
[10m] in the rate function is used to group data points, not to specify the time range from which data is fetched.Using Subqueries
Subqueries allow you to perform an instant query over a specified range and resolution, effectively converting an instant vector into a range vector. The general format for a subquery is:http_requests_total using a 1-minute sample range and then look at the data over the past 5 minutes with a resolution of 30 seconds, you would write:
1mis the sample range used inside theratefunction.5mis the query range, representing how far back the query retrieves data.30sis the query step, defining the interval between the returned data samples.
max_over_time. For example:
http_requests_total for the last five minutes using one-minute sampling intervals and data points every 30 seconds.
Example: Grouping Data Without Aggregation
Consider a subquery without an aggregation function. The following query demonstrates how to group CPU seconds data over a 2-minute range with 10-second intervals:2m range defines the period over which data is collected, while 10s sets the interval between each data point. This grouping is effective for performing further aggregation operations.
Subqueries convert an instant vector into a range vector, enabling aggregation functions such as
max_over_time to operate over multiple data points.Practical Example: Analyzing Network Traffic
Suppose we have a metric callednode_network_transmit_bytes_total that tracks the total transmitted bytes on a network interface. Using the rate function with a 1-minute interval gives the current transmission rate:
rate function directly in max_over_time, you will encounter an error because max_over_time expects a range vector:
max_over_time, consider the following example:
max_over_time, the subquery returns multiple data points across the specified range. In contrast, a query that fetches only the most recent data point may yield results similar to:
Subqueries are a powerful feature for converting an instant vector into a range vector, making them suitable for comprehensive, time-based aggregation scenarios.