Measuring Service-Wide p99 With Prometheus Histograms
One checkout replica serves 9,900 requests at 80 ms. Another serves 100 requests at 1,000 ms during the same window. The mean of their two p99 values is 540 ms. For the combined traffic, the 9,900th request in sorted order takes 80 ms, so the empirical p99 using nearest rank is 80 ms.
Measure the same interval on every replica
Decide where the clock starts and stops before choosing a query. A gateway timer may include waiting for an upstream, while a handler timer can start after the request has cleared a queue. For a customer-facing latency objective, pick a boundary that can also observe timeouts the customer experiences. If a fast error would count as good under a latency-only rule, decide whether to track availability separately or define good requests as successful and within the threshold. Use the same eligibility rules for the numerator and denominator of the chosen indicator.
Record duration in seconds, once per eligible request. Label the metric with bounded dimensions, such as a route template or a small set of response classes. A raw URL containing order IDs can create a separate time series per order for every bucket. Keep request-specific detail in traces or logs and use the histogram for the distribution.
Aggregate bucket rates before calculating p99
A classic Prometheus histogram exports cumulative counts as _bucket series. Each le value is an inclusive upper bound. A request taking 80 ms increments the 100 ms bucket and all larger buckets. The +Inf bucket includes every observation and matches the total observation count. Because counts at the same boundaries can be added across replicas, the query can reconstruct the fleet’s bucketed distribution.
histogram_quantile(
0.99,
sum by (job, le) (
rate(http_request_duration_seconds_bucket{job="checkout"}[5m])
)
)
rate() runs on each underlying counter before aggregation, so it can recognize resets when an individual replica restarts. The aggregation drops replica labels and preserves le. The outer function estimates the percentile once for the combined distribution. Keep a per-replica view for diagnosing the case where one slow replica receives little traffic. Summary quantiles computed separately by each process lose the bucket counts needed for the fleet calculation.
Classic histograms estimate values within a bucket by linear interpolation. If your p99 falls in the interval from 200 to 500 ms, the bucket counts alone cannot distinguish a spike at 210 ms from a spike at 490 ms. Place several boundaries near the range where operational decisions change, and include a sufficiently high finite boundary to see the slow tail. For classic histograms, a percentile in the +Inf bucket returns the highest finite boundary. Check the fraction above that boundary when p99 appears pinned there.
Use a bucket boundary to measure a latency objective
Suppose the objective is for at least 99% of eligible checkout requests to finish in at most 300 ms. Add a classic bucket at 0.3 seconds and query the fraction directly.
sum by (job) (
rate(http_request_duration_seconds_bucket{job="checkout", le="0.3"}[5m])
)
/
sum by (job) (
rate(http_request_duration_seconds_count{job="checkout"}[5m])
)
This returns a ratio from zero to one when both sets of series cover the same requests and there is traffic. The bucket supplies the count at the exact threshold, so the ratio avoids estimating how observations are distributed inside a bucket. With the illustrative traffic above, it returns about 0.99 given steady accumulation through the scrape window. The five-minute view shows recent behavior. For a monthly objective use matching good and total counts across the full evaluation period, preserving the relative weight of busy and quiet intervals.
During a quiet interval inspect request volume alongside p99. In ten requests, a single outlier can dominate the tail. A zero denominator yields no usable ratio, and a missing bucket can leave no result at all. Keep those cases distinguishable from a healthy observation. An alert can require a minimum request volume when paging on a short window, with a separate check for a target that stops reporting.
Check the dashboard during a rollout
Confirm that every replica exports the same classic bucket boundaries. During a rolling change, a bucket present on half the replicas can undercount the numerator while the denominator still includes the fleet. If layouts or measurement boundaries must change, publish a separate metric while migrating and switch the dashboard after coverage is complete. Track the metric’s series count as you add labels or buckets.
For a dashboard review, start with a traceable test workload and known durations on two replicas with uneven traffic. Check the merged distribution, the 300 ms fraction, a single replica restart, and an interval with no requests. Compare the query with recorded counts at the same boundary and allow for scrape timing. Once the figures agree, the fleet view can answer both questions during an incident. How slow is the tail, and what fraction of requests crosses the objective?