Aggregation Functions
This module covers PromQL aggregation functions, which allow you to summarize data across multiple time series. You will learn how to group metrics, calculate sums, averages, and other statistical functions.
Understanding Aggregation
Aggregation functions reduce multiple time series into a single time series by grouping them and applying a function (sum, average, max, etc.) to each group.
The examples in this module aggregate the container metrics that cAdvisor reports, using two conventions:
-
container!=""andcontainer!="POD"skip the series that cAdvisor also reports for whole pods, for node-level cgroups and for each pod’s pause container. Without them, the same memory or CPU is counted several times. -
Memory examples use
container_memory_working_set_bytes, the value thatoc adm top podsreports.container_cpu_usage_seconds_totalis a counter of the CPU seconds a container has used since it started, so CPU examples wrap it inrate(…[5m])to get the current usage in CPU cores. Module 4 explainsrate()in detail.
Basic Aggregation Syntax
<aggregation-function>([parameter,] <vector expression>) [without|by (<label list>)]
The by Clause
The by clause specifies which labels to keep when grouping:
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
This groups all time series by namespace and sums the memory usage for each namespace.
The without Clause
The without clause specifies which labels to remove before grouping:
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) without (pod, container, id, image, name)
This removes the pod and container labels, together with the id, image and name labels that are unique to each container, then sums the remaining groups (one per namespace and node).
Sum Aggregation
The sum() function adds up all values in each group.
Basic Sum
# Total memory usage across all containers
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
})
Average Aggregation
Min and Max Aggregations
Statistical Aggregations
PromQL provides several statistical aggregation functions.
Standard Deviation
The stddev() function calculates the standard deviation:
# Standard deviation of memory usage per namespace
stddev(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
Variance
The stdvar() function calculates the variance:
# Variance of memory usage per namespace
stdvar(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
Quantiles
The quantile() function calculates quantiles over grouped time series:
# 95th percentile of memory usage per namespace
quantile(0.95, container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
# 50th percentile (median) of CPU usage per node
quantile(0.50, rate(container_cpu_usage_seconds_total{
container!="",
container!="POD"
}[5m])) by (node)
Common quantile values:
-
0.50- Median -
0.90- 90th percentile -
0.95- 95th percentile -
0.99- 99th percentile
Top-K and Bottom-K
The topk() and bottomk() functions return the top or bottom K time series by value.
Combining Aggregations
Common Aggregation Patterns in OpenShift
Total Resource Usage per Namespace
# Total CPU usage per namespace
sum(rate(container_cpu_usage_seconds_total{
container!="",
container!="POD"
}[5m])) by (namespace)
# Total memory usage per namespace
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
Average Resource Usage per Node
# Average CPU usage per node
avg(rate(container_cpu_usage_seconds_total{
container!="",
container!="POD"
}[5m])) by (node)
# Average memory usage per node
avg(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (node)
Top Resource Consumers
# Top 10 pods by memory usage
topk(10, sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace, pod))
# Top 5 namespaces by total CPU usage
topk(5, sum(rate(container_cpu_usage_seconds_total{
container!="",
container!="POD"
}[5m])) by (namespace))
Best Practices
Choose Appropriate Aggregation
-
Use
sum()for additive metrics (counters, bytes) -
Use
avg()for metrics where you want the average -
Use
max()ormin()when you need extremes -
Use
quantile()for percentile analysis
Group by Meaningful Labels
Group by labels that make sense for your use case:
# Good: Group by namespace for resource planning
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
# Less useful: Group by cgroup path
# (too granular: one series per container instance)
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (id)
Avoid Over-Aggregation
Don’t aggregate away information you might need:
# Too aggregated: Loses pod-level detail
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
})
# Better: Keeps namespace detail
sum(container_memory_working_set_bytes{
container!="",
container!="POD"
}) by (namespace)
Summary
In this module, you learned:
-
How aggregation functions work in PromQL
-
Using
byandwithoutclauses for grouping -
Common aggregation functions:
sum(),avg(),min(),max(),count() -
Statistical functions:
stddev(),stdvar(),quantile() -
Top-K and bottom-K functions
-
Combining aggregations with rate functions
-
Common aggregation patterns for OpenShift
-
Best practices for effective aggregation
In the next module, you will learn advanced query patterns including rate functions, subqueries, and logical operators.