rate, irate and increase for counters

Prometheus · Practical · 12 min

Nearly every useful graph and alert on counters, from request rates to error ratios, starts with rate().

A counter's raw value says little; what matters is how fast it grows.

  • rate(c[5m]): the per-second average rate over the last 5 minutes. It automatically handles counter resets (a process restart drops the value to 0) and extrapolates to the window edges. Use it for graphs and alerts.
  • irate(c[5m]): the per-second rate between the last two samples in the window. More responsive, but spiky; useful only for fast-moving, zoomed-in graphs.
  • increase(c[1h]): the total increase over the window, equal to rate(c[1h]) * 3600. Because of extrapolation the result can be a non-integer even for integer counters.

Choosing the range: it must contain at least two samples, and should be at least four times the scrape interval to tolerate a missed scrape. With 15s scrapes, [1m] is the minimum and [5m] is common. In Grafana, $__rate_interval picks a safe value automatically.

The golden rule: rate first, then aggregate. sum(rate(x[5m])) is correct; rate(sum(x)[5m:]) is wrong because summing counters from instances that restart at different times hides resets.

An error ratio is just two rates divided: errors per second divided by requests per second. Never use rate() on a gauge; use deriv() or delta() for gauges instead.

Example

# Requests per second per series, averaged over 5 minutes
rate(http_requests_total{job="api"}[5m])

# Total requests per second for the whole job (rate first, then sum)
sum(rate(http_requests_total{job="api"}[5m]))

# Errors in the last hour (may be fractional because of extrapolation)
increase(http_requests_total{job="api", status=~"5.."}[1h])

# Error ratio: 5xx per second / all requests per second
sum(rate(http_requests_total{status=~"5.."}[5m]))
  /
sum(rate(http_requests_total[5m]))
# e.g. 0.0042  (0.42 % of requests fail)

Remember

  • rate() = per-second average increase, reset-safe
  • Use a range of at least 4x the scrape interval
  • Always rate first, then sum

Watch out for

  • Applying rate() to a gauge such as memory usage; it's meant for counters only.

Check yourself

  1. Why is sum(rate(x[5m])) preferred over rate of a summed counter?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: prometheus.io/docs/prometheus/latest/querying/functions/#rate