Aggregation by Labels
What You'll Learn
You'll learn how to group metrics by specific labels using the by() and without() clauses in PromQL to slice and dice your aggregated data across different dimensions.
Why Dimensionality Matters
You've learned aggregation operators like sum(), avg(), and count(). By default, these collapse all series into a single result. But what if you want subtotals? Maybe you want CPU usage per environment or request counts per service, not just one global number.
This is where aggregation by labels comes in.
The by() Clause
The by() clause tells Prometheus which labels to keep when grouping:
sum by(environment) (http_requests_total)
This groups all http_requests_total metrics by their environment label. You get separate sums for environment="prod", environment="staging", etc. All other labels are discarded.
Think of it like SQL's GROUP BY — you're creating buckets based on specific dimensions.
The without() Clause
The without() clause does the opposite — it specifies which labels to discard while keeping everything else:
sum without(instance, job) (node_memory_active_bytes)
This aggregates memory across all instances and jobs but preserves other labels like region or cluster.
Use without() when you have many labels and want to remove just a few noisy ones (like per-instance identifiers).
Choosing Between by() and without()
- Use
by()when you know exactly which dimensions matter - Use
without()when you want to preserve most labels except specific ones
Key Takeaway:
Use by(label1, label2) to group aggregations by specific labels, or without(label1) to aggregate while excluding certain labels — enabling dimensional analysis of your metrics.