Rates & Counters
rate, irate, increase, counter resets, and choosing a window
Most useful numbers in monitoring are rates: requests per second, errors
per second, bytes per second. Since everything that counts is stored as a
counter, rate() is the function you'll write more than any other.
rate()
rate(counter[window]) returns the per-second average rate of
increase over the window:
rate(http_requests_total{job="demo-app"}[5m])
# conceptually: (last sample - first sample) / (time between them),
# corrected for counter resets, then extrapolated to cover the full windowThree things rate() does for you:
- Handles counter resets. If the value drops (say 18902 to 4), it
assumes the process restarted and the counter started again from 0,
so it adds the pre-reset value back in. That's why you must only
rate()counters: a gauge that goes down would be misread as a reset. - Extrapolates to the window edges. Samples rarely sit exactly at
the window boundaries, so
rate()scales the observed increase to the full window, within limits. - Returns per-second values, whatever the window. A 5m and a 1h window both give requests per second; the longer one is just smoother.
- http_requests_total (raw counter)
- rate(http_requests_total[5m])
increase()
increase(x[1h]) is rate(x[1h]) * 3600: the total increase over the
window. Use it when "how many" reads better than "how many per second":
increase(http_requests_total{status=~"5.."}[1h]) # errors in the last hour
increase(pg_stat_database_deadlocks[1d]) # deadlocks todayincrease() often returns non-integers, like 41.7 errors, because
of extrapolation. That's expected. Prometheus is estimating the
increase over exactly one hour from samples that don't sit exactly on
the hour. If you need exact counts, you need event data, not metrics.
irate()
irate() uses only the last two samples in the window. It reacts
instantly to changes but is very noisy, and on a graph whose step is
larger than the scrape interval it skips most of the data.
rate() | irate() | |
|---|---|---|
| Uses | All samples in window | Last two |
| Shape | Smooth | Spiky |
| Good for | Alerts, recording rules, most dashboards | Zoomed-in, high-resolution graphs |
| Bad for | Spotting very short spikes | Alerts (flaps), long time ranges |
Default to rate(). Never alert on irate().
Choosing the window
The window must contain at least two samples, or rate() returns
nothing. With a 15s scrape interval, [15s] has one sample, so it's
empty and the graph shows gaps. The rule of thumb is a window of at least
4x the scrape interval, so a missed scrape or two doesn't break it:
| Scrape interval | Minimum sensible window |
|---|---|
| 5s | 20s |
| 15s | 1m |
| 30s | 2m |
| 1m | 4m |
Beyond the minimum, the window is a trade-off: short windows show spikes and noise; long windows are smooth but slow to react. Alerts usually use 5m; capacity dashboards use 1h or more.
$__rate_interval in Grafana
In Grafana, write rate(x[$__rate_interval]) instead of a fixed window.
Grafana computes it as the larger of four times the data source's scrape
interval (the timeInterval set in provisioning, 15s here) and the
graph's step plus one scrape interval. So when you zoom out to 7 days
and each pixel covers 10 minutes, the window grows to match and you don't
skip data between points.
# in a Grafana panel, always prefer:
sum by (route) (rate(http_requests_total{job="demo-app"}[$__rate_interval]))The golden rule: rate, then aggregate
Always apply rate() to the raw counters first, and sum
afterwards:
# right: rate each series (handling each one's resets), then add
sum(rate(http_requests_total[5m]))
# wrong (and invalid): you cannot rate() an aggregated instant vector
rate(sum(http_requests_total)[5m])Why it matters: if one of ten instances restarts, its counter drops to
0. Summed first, the total would drop by that instance's count, and
rate() would read it as a reset of the whole sum, producing a huge
bogus spike. Rated first, only that one series sees a reset, which
rate() handles correctly. (You can force the wrong version with a
subquery, which is exactly why it's worth knowing it's wrong.)
Ratios
Error ratio is a rate divided by a rate:
sum(rate(http_requests_total{job="demo-app", status=~"5.."}[5m]))
/
sum(rate(http_requests_total{job="demo-app"}[5m]))Both sides are aggregated with sum and no by, so each side is a
single series with no labels and they divide cleanly. Add by (route) to
both sides to get a ratio per route. Mismatched labels between
the two sides are the most common reason a division "returns no data",
covered in Aggregation & Joins.
Lab: watch a rate react
- Graph
sum by (route) (rate(http_requests_total{job="demo-app"}[1m])). You'll see the load generator's 10-minute sine wave. - Graph the error ratio query above.
- Inject errors:
curl -X POST 'localhost:8080/admin/chaos?error_rate=0.3' - Watch the ratio climb. Then change the window to
[5m]and compare: it climbs more slowly, because the window still contains the earlier healthy minutes. - Restore:
curl -X POST 'localhost:8080/admin/chaos?error_rate=0.01'. - Restart the app with
docker restart demo-appand graph the rawhttp_requests_totalnext to itsrate(). The raw counter drops to zero; the rate barely notices.