Learn Labs
PromQL

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 window

Three things rate() does for you:

  1. 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.
  2. 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.
  3. Returns per-second values, whatever the window. A 5m and a 1h window both give requests per second; the longer one is just smoother.
process restartvaluetime (restart in the middle)
  • 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 today

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()
UsesAll samples in windowLast two
ShapeSmoothSpiky
Good forAlerts, recording rules, most dashboardsZoomed-in, high-resolution graphs
Bad forSpotting very short spikesAlerts (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 intervalMinimum sensible window
5s20s
15s1m
30s2m
1m4m

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

  1. Graph sum by (route) (rate(http_requests_total{job="demo-app"}[1m])). You'll see the load generator's 10-minute sine wave.
  2. Graph the error ratio query above.
  3. Inject errors: curl -X POST 'localhost:8080/admin/chaos?error_rate=0.3'
  4. 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.
  5. Restore: curl -X POST 'localhost:8080/admin/chaos?error_rate=0.01'.
  6. Restart the app with docker restart demo-app and graph the raw http_requests_total next to its rate(). The raw counter drops to zero; the rate barely notices.

On this page