Lesson 3 of 9 · 27m
PromQL, Time Windows, and Operational Truth
Reason about counters, rates, aggregation, and time windows so dashboards answer incident questions instead of displaying numerically precise nonsense.
PromQL is small enough to learn in an afternoon and subtle enough to mislead you for years. Most bad dashboards are not wrong because the query syntax failed. They are wrong because the author ignored time: counter resets, scrape intervals, window alignment, missing series, and the difference between an instant value and behavior over a period.
Rates need windows, not wishes
A counter is monotonic, so the useful question is how quickly it increased:
rate(http_requests_total{service="checkout"}[5m])The [5m] range means Prometheus looks at samples inside that window and estimates per-second increase while correcting for counter resets. If your scrape interval is 15 seconds, a one-minute window may contain only a handful of samples. Sharp one-minute rates look responsive and lie constantly during sparse scrapes.
Rule of thumb: use a window at least four times the scrape interval for routine rates. Use longer windows for alerts unless the incident truly requires minute-level response.
Aggregation removes dimensions deliberately
This query sums request rate by status:
sum by (status) (
rate(http_requests_total{service="checkout"}[5m])
)sum by is an explicit decision about which labels survive. sum without is the inverse. Do not accidentally aggregate away the label that explains the failure. During an incident, sum(rate(...)) may look flat while one route is on fire.
Compare these:
sum(rate(http_requests_total[5m]))
sum by (route, status) (rate(http_requests_total[5m]))The first answers fleet-wide demand. The second reveals shape. Both are useful; neither replaces the other.
Ratios are the language of SLOs
Error ratio is errors divided by total requests over the same window and grouping:
sum by (service) (rate(http_requests_total{status=~"5.."}[5m]))
/
sum by (service) (rate(http_requests_total[5m]))The numerator and denominator must group identically. If one keeps route and the other does not, the vector match will be empty or misleading. A zero denominator is not an error rate; it is missing demand. Use filtering and alert design to distinguish no traffic from perfect reliability.
Histograms need histogram_quantile
Latency percentile from Prometheus histogram buckets:
histogram_quantile(0.95,
sum by (le, service) (
rate(http_request_duration_seconds_bucket[5m])
)
)The le label must survive aggregation because it encodes bucket boundaries. If you drop it, quantile calculation has no distribution left. Also remember that histogram quantiles are interpolated estimates. If every observation lands in the top bucket, Prometheus cannot manufacture detail you never collected.
Instant vectors and range vectors are different objects
http_requests_total selects the latest sample in an instant vector. http_requests_total[5m] selects a range of samples. Functions expect one or the other. rate needs a range vector; sum aggregates instant vectors. Many confusing type errors are really time-model errors.
Missing data is a signal
A series that disappears can mean no events, target down, label rename, deployment migration, or instrumentation removal. These are operationally different. Pair queries with up, scrape duration, series count, and deployment annotations before declaring victory.
Useful diagnostic queries:
count by (job) (up)
scrape_samples_scraped
prometheus_tsdb_head_series
absent(up{job="checkout"})absent is powerful but easy to misuse. It proves no matching series exists at evaluation time; it does not explain why.
Field notes
irateis useful for volatile high-resolution counters but terrible for calm alerting dashboards.- A rate window does not need exact sample alignment; Prometheus interpolates counter behavior, which surprises people comparing raw scrapes.
- Never compare percentiles across services with different histogram bucket layouts.
avg(latency_seconds)across instances hides tail pain. Prefer histogram quantiles and saturation metrics.- Always inspect the raw target series when a polished panel makes no sense.
Was this lesson helpful?