🎓 A1Learner
🔍
0 of 50 lessons completed

PromQL: The Five Queries That Lie

Some PromQL queries return a confident number that is wrong. Each one below is shown broken first, then fixed.

Lie 1: rate on a gauge

The gauge reads 1, but rate says it grows at 0 per second. The rate looks fine, and it means nothing.

rate(shopsentry_in_flight{job="shopsentry"}[5m])
# Fixed: read the gauge as it is
shopsentry_in_flight{job="shopsentry"}

Lie 2: sum the counters, then rate

Summing first gives 2.02 requests per second. Rating first gives 1.66. Only the second answer is right.

rate(sum(http_requests_total{job="shopsentry"})[5m:])
# Fixed: rate each series first, then add
sum(rate(http_requests_total{job="shopsentry"}[5m]))

Lie 3: a window shorter than the scrape

A 10 second window holds fewer than two scrapes, so the query returns nothing. A 5 minute window returns the real speed.

rate(http_requests_total{job="shopsentry",route="/api/orders"}[10s])
# Fixed: a window of at least one minute
rate(http_requests_total{job="shopsentry",route="/api/orders"}[5m])

Lie 4: averaging percentiles

histogram_quantile reads a percentile out of histogram buckets. The next lesson teaches it in full. A second ShopSentry with slow responses ran beside the fast one. Averaging their p95 values gives 1.20 seconds. The true combined p95 is 1.82 seconds.

avg(histogram_quantile(0.95, sum by (le, instance) (rate(http_request_duration_seconds_bucket{job=~"shopsentry|shopsentry-slow"}[5m]))))
# Fixed: one quantile over the combined buckets
histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket{job=~"shopsentry|shopsentry-slow"}[5m])))

Lie 5: topk hides the rest

Topk keeps only the busiest route and hides the rest. The full sum shows all four routes.

topk(1, sum by (route) (http_requests_total{job="shopsentry"}))
# Fixed: show every route
sum by (route) (http_requests_total{job="shopsentry"})

Example

rate(http_requests_total{job="shopsentry",route="/api/orders"}[5m])

Key points

  • Rate on a gauge is wrong, because a gauge can fall as well as rise.
  • Summing counters before rating them gives a different number from rating first.
  • A window shorter than the scrape interval finds no samples and returns nothing.
  • Averaging percentiles across series understates the true tail latency.
  • Topk in a query can hide the series you need to see.
  • A broken query often returns a number, not an error.
  • Check every query against the raw series before you trust it.

In this example, a 5 minute window holds twenty scrapes, so the rate has enough samples to be trusted. The value 0.53 requests per second is the real speed from the live ShopSentry lab. A 10 second window on the same series returns nothing at all.

Note

Topk hides series wherever it runs, so an alert built on topk can miss the series that matters.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why is rate() on a gauge wrong?