Prometheus / Intermediate: PromQL and rule contracts / Lesson 14
Latency threshold ratios from classic histograms
Calculate a bounded latency objective ratio from classic histogram buckets and interpret low-traffic results.
On this page
Lesson goals
- Select a bucket matching an explicit latency threshold.
- Aggregate bucket and count rates with aligned labels.
- Test low traffic, missing buckets and alert behavior.
Define the measurement objective
A latency objective might ask what fraction of requests completed within a chosen threshold over a window. A classic histogram exposes cumulative bucket counters and a count counter. The threshold and window must come from the service’s own objective and instrumentation, not an example copied from a lesson. Define which requests, services and tenants belong in numerator and denominator before writing the expression.
Count observations within the boundary
The first PromQL fragment calculates per-service rates for a synthetic bucket at le="0.3". It assumes the instrumented classic histogram actually exposes that boundary in seconds and that the metric and service labels exist. Apply rate to each counter series before aggregation so individual resets are handled by the function. An unavailable bucket produces no matching series; it is not a measured zero.
sum by (service) (rate(lab_request_duration_seconds_bucket{le="0.3"}[5m]))
The metric and 0.3-second boundary are illustrative and must exist in the actual instrumentation.
Align numerator and denominator
The second fragment divides that bucket rate by the corresponding count rate, aggregated using the same service dimension. It represents an illustrative fraction, not a universal objective or alert. Confirm that both counters describe the same population and that units and label filters match. Classic buckets are cumulative, so the selected bucket already includes observations in lower buckets; do not add those buckets again.
sum by (service) (rate(lab_request_duration_seconds_bucket{le="0.3"}[5m]))
/
sum by (service) (rate(lab_request_duration_seconds_count[5m]))
The ratio assumes matching populations and service labels. Handle missing or zero denominators according to the application policy.
Interpret sparse or absent data
A zero denominator, missing bucket or target gap needs an explicit operational policy; it is not automatically evidence that latency is healthy. Inspect the source series, traffic and target health before displaying or alerting on the ratio. Low volume can make a short-window fraction volatile. Consider a minimum traffic condition and a longer window only after reviewing the service objective and the effect of delayed detection.
Validate the whole rule
Use synthetic histogram samples with observations below and above the selected boundary, a counter reset, no traffic and a missing bucket. Evaluate at fixed times with the installed tool and inspect output values and labels. Test any alert’s pending and recovery states separately and verify authorization and receiver routing. These PromQL fragments are illustrative; no Prometheus or promtool execution is claimed here.
Common errors and fixes
The numerator and denominator describe different populations
- Why it happens
- Filters or aggregation labels were inconsistent.
- How to fix it
- Align metric scope, labels and time windows.
- Prevent it next time
- Test two synthetic services and inspect both operands.
Lower buckets are added twice
- Why it happens
- Classic histogram buckets were treated as independent ranges.
- How to fix it
- Use the selected cumulative bucket directly.
- Prevent it next time
- Check synthetic observations against expected counts.
No traffic appears as healthy latency
- Why it happens
- A missing or zero denominator was treated as success.
- How to fix it
- Define an explicit no-data and low-traffic policy.
- Prevent it next time
- Test zero traffic, missing targets and recovery.
2 practice exercises
1. A service has 8 requests within the boundary and 10 total in a controlled interval. What fraction is intended?
Show worked solution
The intended fraction is 8/10, or 0.8, provided the counters and filters describe the same requests. The displayed rate calculation must be evaluated with representative samples and the installed Prometheus implementation.
2. Why should a missing le="0.3" bucket not be treated as a measured zero?
Show worked solution
The instrumentation or target may not provide that boundary at all. Inspect the exported series and target health, then choose a supported boundary and an explicit no-data policy.
Key takeaways
- A classic bucket is cumulative and must match the objective’s units and boundary.
- Align both operands and explicitly handle sparse or absent traffic.