Prometheus Query Aggregation By Wrong Labels Fix
In this tutorial, you'll learn about Prometheus Query Aggregation By Wrong Labels Fix. We cover key concepts, practical examples, and best practices.
Your Prometheus query returns too many time series — an aggregation by job still shows per-instance data, or without(instance) does not remove the instance label. The by() and without() clauses control which labels are preserved after aggregation.
The Problem
// WRONG: by(job) but still shows per-instance result
sum by (job) (rate(http_requests_total[5m]))
If the query result still shows multiple series per job, the metric might have additional labels (like method, path) that make each combination unique. by() only aggregates over specified labels — all other labels are dropped.
Step-by-Step Fix
1. Use by() to specify preserved labels
// Aggregate by job only — drops all other labels
sum by (job) (rate(http_requests_total[5m]))
// Aggregate by job and method — keeps both
sum by (job, method) (rate(http_requests_total[5m]))
2. Use without() to exclude labels
// Aggregate over all labels EXCEPT instance
sum without (instance) (rate(http_requests_total[5m]))
// Equivalent to: sum by (job, method, path) (...)
// Remove multiple labels
sum without (instance, pod) (rate(http_requests_total[5m]))
3. Common aggregation patterns
// Total requests per service
sum by (service) (rate(http_requests_total[5m]))
// Error rate per service
sum by (service) (rate(http_requests_total{status=~"5.."}[5m]))
/
sum by (service) (rate(http_requests_total[5m]))
// P99 latency per endpoint
histogram_quantile(0.99,
sum by (le, endpoint) (rate(http_request_duration_seconds_bucket[5m]))
)
4. Check labels before and after aggregation
// Before aggregation — see all labels
rate(http_requests_total[5m])
// Labels: job="api", instance="pod-1", method="GET", path="/users"
// After aggregation — only preserved labels remain
sum by (job, method) (rate(http_requests_total[5m]))
// Labels: job="api", method="GET"
// Labels removed: instance, path
5. Use topk/bottomk with aggregation
// Top 5 services by request rate
topk(5, sum by (service) (rate(http_requests_total[5m])))
// Bottom 3 endpoints by latency
bottomk(3, avg by (endpoint) (rate(http_request_duration_seconds_sum[5m]) / rate(http_request_duration_seconds_count[5m])))
Expected output:
// Before aggregation: 100 time series (10 jobs × 5 instances × 2 methods)
// After sum by (job): 10 time series
// After sum by (job, method): 20 time series
Prevention Tips
- Use
by()when you know which labels to keep - Use
without()when you know which labels to drop - Check label cardinality before and after aggregation
- Use
count by (...)to verify aggregation worked - Prefer
by()overwithout()for clarity
Common Mistakes with query aggregation by
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro