Learn Labs
PromQL

Histograms & Percentiles

histogram_quantile, aggregating buckets, and what averages hide

Latency is the signal users feel most directly, and it is almost never normally distributed. Most requests are fast; a few are very slow. The average hides those slow ones. Percentiles show them.

Why averages lie

Suppose 99 requests take 10 ms and 1 takes 5 seconds. The average is about 60 ms, which looks fine. But one user in a hundred waited 5 seconds, and a page that makes 50 backend calls hits that slow path on most loads.

# mean latency per route
sum by (route) (rate(http_request_duration_seconds_sum[5m]))
/ sum by (route) (rate(http_request_duration_seconds_count[5m]))

# p99 latency per route
histogram_quantile(0.99,
sum by (route, le) (rate(http_request_duration_seconds_bucket[5m])))

Graph both for /api/search, which the demo app gives a long-tailed latency distribution. The mean sits low and steady; p99 is several times higher and much noisier. The Demo App · RED dashboard puts these side by side.

How histogram_quantile works

histogram_quantile(φ, buckets) needs, for each group, the set of cumulative bucket counts with their le labels. It then:

  1. Finds the total count (the +Inf bucket).
  2. Computes the target rank: φ × total. For p99 of 1000 requests, rank 990.
  3. Walks the buckets to find the first one whose cumulative count reaches that rank.
  4. Linearly interpolates within that bucket, assuming observations are spread evenly between its lower and upper bound.
le="0.05"   950      # 950 requests took <= 50ms
le="0.1"    985
le="0.25"   998      # rank 990 falls in (0.1, 0.25]
le="+Inf"  1000

# 990 is 5/13 of the way from 985 to 998, so
# p99 ≈ 0.1 + (5/13) * (0.25 - 0.1) ≈ 0.158s

The real p99 could be anywhere between 100 and 250 ms. The answer is only as precise as your buckets, which is why bucket boundaries should sit around the latencies you care about.

9009501000950le="0.05"985le="0.1"998le="0.25"1000le="+Inf"rank 990 = 0.99 × 1000990 falls in (0.1, 0.25]: p99 ≈ 0.1 + (5/13) × 0.15 ≈ 0.158 s, an estimate from bucket edges

The rule: aggregate buckets, keep le

The pattern is always the same: rate the buckets, sum them by le plus whatever you want to group by, then take the quantile.

# p99 across all instances and routes
histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))

# p99 per route
histogram_quantile(0.99, sum by (route, le) (rate(http_request_duration_seconds_bucket[5m])))

# WRONG: dropped le, so there are no buckets left to compute from
histogram_quantile(0.99, sum by (route) (rate(http_request_duration_seconds_bucket[5m])))

# WRONG: averaging per-instance p99s is not the fleet p99
avg(histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m])))

This is the real power of histograms: because buckets are counters, you can add them across any number of instances and get a correct fleet-wide percentile.

Exact SLO measurements

Percentiles are estimates. But if your threshold is a bucket boundary, "what fraction of requests were fast enough" is exact:

# share of /api/orders requests that finished within 100ms
sum(rate(http_request_duration_seconds_bucket{route="/api/orders", le="0.1"}[5m]))
/
sum(rate(http_request_duration_seconds_count{route="/api/orders"}[5m]))

This is how latency SLOs should be measured: "99% of requests under 100 ms" is a ratio of two counters, not a percentile. It also aggregates correctly over any time window, which percentiles don't.

Heatmaps

A percentile line throws away the shape of the distribution. A heatmap shows all of it: time on the x-axis, buckets on the y-axis, and colour for how many requests landed in each bucket.

# in Grafana, Format = Heatmap, panel type = Heatmap
sum by (le) (increase(http_request_duration_seconds_bucket{job="demo-app"}[$__rate_interval]))

Heatmaps make bimodal latency obvious, such as a cache-hit band at 5 ms and a cache-miss band at 200 ms. A p50 line would sit between the two bands, at a latency no request actually had.

Apdex-style scores

A single "user happiness" number from two thresholds: requests under T are satisfied, under 4T tolerated, the rest frustrated:

(
sum(rate(http_request_duration_seconds_bucket{le="0.1"}[5m]))
+ sum(rate(http_request_duration_seconds_bucket{le="0.5"}[5m]))   # 0.5 ≈ 4T bucket
) / 2 / sum(rate(http_request_duration_seconds_count[5m]))

The satisfied requests are counted in both buckets, so they count as 1 after dividing by 2; tolerated ones count once, so they're worth 0.5.

Lab

  1. Graph p50, p90, and p99 for /api/search on one panel.
  2. Inject latency: curl -X POST 'localhost:8080/admin/chaos?extra_latency_ms=300'.
  3. Watch p50 and p99 move. Then graph the mean. Which reacted most?
  4. Graph the fraction of /api/orders requests under 100 ms. It should collapse to near 0 with 300 ms of extra latency.
  5. Open the Latency distribution heatmap in Grafana and watch the band shift up.
  6. Reset: curl -X POST 'localhost:8080/admin/chaos?extra_latency_ms=0'.

On this page