🎓 A1Learner
🔍
0 of 50 lessons completed

PromQL: Histograms and histogram_quantile

A histogram sorts request times into buckets. Histogram_quantile reads a percentile out of those buckets.

The median request time

Quantile 0.5 is the time half the requests beat.

histogram_quantile(0.5, sum by (le) (rate(http_request_duration_seconds_bucket{job="shopsentry"}[5m])))

The p95 request time

Quantile 0.95 is the time 95 in 100 requests beat.

histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket{job="shopsentry"}[5m])))

The p99 request time

Quantile 0.99 is the time 99 in 100 requests beat.

histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket{job="shopsentry"}[5m])))

Example

histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket{job="shopsentry"}[5m])))

Key points

  • A histogram counts how many requests fell into each time bucket.
  • The le label marks the top edge of each bucket.
  • Rate the buckets first, before any other step.
  • Sum the rated buckets by le, and by nothing else.
  • Call histogram_quantile once, on the summed buckets.
  • Use 0.5 for the median, 0.95 for p95, and 0.99 for p99.
  • A gauge cannot give a percentile, because it stores no buckets.

In this example, rate turns the buckets into per-second speeds, sum by (le) merges them into one histogram, and histogram_quantile reads the p95 latency out of it. The p95 is about 4.8 milliseconds, the real value from the live ShopSentry lab.

Note

Skip the rate step and the percentile is stale the moment the counter stops moving.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What is the first step before calling histogram_quantile?