Skip to content

Prometheus Query Aggregation By Wrong Labels Fix

DodaTech Updated 2026-06-24 3 min read

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() over without() for clarity

Common Mistakes with query aggregation by

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. 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

### What's the difference between by() and without()?

by() specifies the labels to preserve — all other labels are dropped. without() specifies the labels to drop — all other labels are preserved. by(job) and without(instance, method, path) are equivalent if the full label set is {job, instance, method, path}.

Why does my aggregation still show multiple series?

Check if there are hidden labels. Some metrics have exported_job, exported_instance from federation or recording rules. Use without() to remove known high-cardinality labels, or inspect the full label set with an unfiltered query.

Can I aggregate by metric value ranges?

No, PromQL aggregation operates on labels, not values. To aggregate by value ranges, use histogram_quantile() for quantiles over bucket data, or use recording rules to create bucketed metrics.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro