Histogram and Quantile Queries
What you'll learn: How to calculate percentiles from Prometheus histogram metrics to analyze latency distributions and identify performance outliers.
Histograms in Prometheus collect observations (like request durations) and bucket them into predefined ranges. But the real power comes when you extract quantiles (percentiles) from these buckets using the histogram_quantile() function.
Why Percentiles Matter
Imagine you're measuring API response times. An average might say "200ms," but that hides the truth: 99% of requests might be fast, while 1% take 5 seconds. Percentiles reveal this distribution. The 95th percentile (p95) tells you: "95% of requests were faster than this value."
The histogram_quantile() Function
The histogram_quantile() function estimates percentiles from histogram buckets:
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))
This calculates the 95th percentile of HTTP request duration over the last 5 minutes.
Breaking it down:
0.95= the quantile (95th percentile)rate(...)= calculates the per-second rate for each bucket- Must use
_bucketsuffix (histogram metrics expose_bucket,_sum, and_count)
Practical Example
To see p50, p95, and p99 latencies by endpoint:
histogram_quantile(0.99, sum by(le, endpoint) (rate(http_request_duration_seconds_bucket[5m])))
The le label (less-than-or-equal) is required for histogram aggregation—it defines bucket boundaries.
Common Pattern
Compare multiple percentiles side-by-side to understand your latency distribution fully.
Key Takeaway: Use histogram_quantile() with histogram metrics to calculate percentiles like p95 or p99, revealing how your system performs for the majority and the outliers—essential for SLO-based latency analysis.