Lesson 3 of 9 · 24m
Rates, Aggregations, and Histogram Quantiles
How to turn counters into rates, aggregate without destroying dimensions, and compute latency percentiles that can actually be trusted.
A counter is almost never interesting by itself. Nobody pages on "there have been 18 million requests since boot." We care about requests per second, error ratio, saturation, and latency distribution. Those are computed values, and each computation imposes rules.
Rate before aggregation
sum by (service, status) (
rate(http_requests_total{service=~"checkout|payments"}[5m])
)For counters, rate must be closest to the raw series. It needs per-series counter history to detect resets. If you sum first, Prometheus cannot infer resets across mixed series and the result can become nonsense.
irate uses only the last two samples and is jumpy; useful for fast-moving troubleshooting, rarely good for alerts. increase extrapolates the counter increase over a window; useful for totals, not a smoother alert input by default.
Aggregation is destructive unless you keep dimensions
These two queries answer different questions:
sum(rate(http_requests_total[5m]))
sum by (service, env) (rate(http_requests_total[5m]))The first is one line: total traffic. The second preserves service and environment so you can see who is responsible. Most dashboard mistakes are either over-aggregating into one meaningless line or failing to aggregate and producing an unreadable wall of series.
Use without() when you want to drop known noise while retaining everything else:
sum without(instance, pod) (rate(app_errors_total[5m]))Histogram quantiles are interpolation across buckets
A Prometheus client histogram looks like:
http_request_duration_seconds_bucket{le="0.1"} 8120
http_request_duration_seconds_bucket{le="0.5"} 10430
http_request_duration_seconds_bucket{le="+Inf"} 10900The percentile query is:
histogram_quantile(0.95,
sum by (le, service) (
rate(http_request_duration_seconds_bucket[5m])
)
)le must survive aggregation because bucket boundaries are the coordinate system. Drop it and percentile math is impossible. The result is an interpolation inside a bucket, not an exact measurement. If your p95 lands inside a bucket spanning 1s to 10s, "p95 is 3.7s" is an estimate with very broad assumptions.
Error ratios beat raw error counts
sum by (service) (rate(http_requests_total{status=~"5.."}[5m]))
/
sum by (service) (rate(http_requests_total[5m]))A traffic spike makes raw errors look terrifying. A ratio normalizes them. It also introduces a small-number problem: a service with two requests and one error has a 50% error ratio. Guard low-volume services rather than paging the on-call because a canary had three requests.
Record the expensive stable parts
groups:
- name: checkout.rules
rules:
- record: job_service:http_requests:rate5m
expr: sum by (job, service) (rate(http_requests_total[5m]))Recording rules precompute common expressions and give dashboards a stable vocabulary. They are not junk drawers. Name them deliberately with the level:metric:operations convention and document what dimensions they preserve.
Field notes
sum(rate(...))is correct;rate(sum(...))is usually a category error.- Histogram quantiles can aggregate across instances; summary quantiles cannot. Prefer histograms for distributed services.
- Choose buckets around meaningful latency budgets, not powers of ten copied from a blog post.
- If a ratio explodes at zero traffic, add a volume guard or clamp; do not normalize away real outages either.
Was this lesson helpful?