rate(), irate() & increase()
Counters only go up, so their raw value is uninteresting. What you want is "how fast is it going up?". Three functions answer that, and they are the most used functions in PromQL.
| Function | Returns | Use for |
|---|---|---|
rate(c[5m]) |
average per-second increase over the window | dashboards, alerts, recording rules: almost everything |
irate(c[5m]) |
per-second increase between the last two samples in the window | spotting short spikes on a zoomed-in graph |
increase(c[1h]) |
total increase over the window (= rate × window seconds) |
"how many in the last hour?" |
All three handle counter resets: when a value is lower than the previous one, the process restarted, and the function counts the new value as growth from zero instead of a huge negative jump.
On real data
The session backfills two counters. http_requests_total on api-2 grows by 2 per second, but the process restarts at 00:15:15 (the counter drops to 0). linkly_db_errors_total grows by 1 every now and then:
python3 - > data.om <<'PY'
start = 1767225600
print("# TYPE http_requests_total counter")
for i in range(121):
v = 30 * i if i <= 60 else 30 * (i - 61) # restart after 00:15:00
print(f'http_requests_total{{instance="api-2"}} {v} {start + 15 * i}')
print("# TYPE linkly_db_errors_total counter")
errors_at = {37, 41, 43, 70} # sample numbers where one error was counted
v = 0
for i in range(121):
v += i in errors_at
print(f'linkly_db_errors_total{{instance="api-2"}} {v} {start + 15 * i}')
print("# EOF")
PY
promtool tsdb create-blocks-from openmetrics data.om data >/dev/null
echo "global: {}" > prometheus.yml
prometheus --config.file=prometheus.yml --storage.tsdb.path=data --storage.tsdb.retention.time=100y \
--web.listen-address=127.0.0.1:9090 >prom.log 2>&1 &
for i in $(seq 100); do curl -sf 127.0.0.1:9090/-/ready >/dev/null && break; sleep 0.2; done
q() { printf '%-42s ' "$2"; promtool query instant --time="$1" http://127.0.0.1:9090 "$2" | sed 's/.*=> //'; }
q 2026-01-01T00:14:00Z 'http_requests_total[2m]'
q 2026-01-01T00:14:00Z 'rate(http_requests_total[2m])'
q 2026-01-01T00:17:00Z 'http_requests_total'
q 2026-01-01T00:17:00Z 'rate(http_requests_total[2m])'
q 2026-01-01T00:17:00Z 'resets(http_requests_total[5m])'
q 2026-01-01T00:17:00Z 'increase(http_requests_total[10m])'
q 2026-01-01T00:11:00Z 'increase(linkly_db_errors_total[2m])'
q 2026-01-01T00:11:00Z 'rate(linkly_db_errors_total[2m])'
q 2026-01-01T00:11:00Z 'irate(linkly_db_errors_total[2m])'http_requests_total[2m] http_requests_total{instance="api-2"} =>
1470 @[1767226335]
1500 @[1767226350]
1530 @[1767226365]
1560 @[1767226380]
1590 @[1767226395]
1620 @[1767226410]
1650 @[1767226425]
1680 @[1767226440]
rate(http_requests_total[2m]) 1.9999999999999998 @[1767226440]
http_requests_total 210 @[1767226620]
rate(http_requests_total[2m]) 1.75 @[1767226620]
resets(http_requests_total[5m]) 1 @[1767226620]
increase(http_requests_total[10m]) 1169.230769230769 @[1767226620]
increase(linkly_db_errors_total[2m]) 2.2857142857142856 @[1767226260]
rate(linkly_db_errors_total[2m]) 0.019047619047619046 @[1767226260]
irate(linkly_db_errors_total[2m]) 0 @[1767226260]
What to read from it:
- Before the restart,
rateis 2/s (printed as1.9999999999999998: floating point). At 00:17 the[2m]window starts right at the restart: the new counter begins at 0 at 00:15:15, and Prometheus never extrapolates a counter back past zero, so it sees 210 requests in 120 s, 1.75/s.resets()over 5 minutes counts the restart, which is also a handy "did my pods restart?" signal. increase(…[10m])spans the restart: the drop from 1800 to 0 is counted as a restart from zero instead of −1800. It would be 1200 for 10 minutes at 2/s; it comes out at about 1169 because the requests served between the last scrape before the crash and the restart were never observed.increase(linkly_db_errors_total[2m])is not an integer even though errors are counted in whole numbers. That surprises everyone once.
Why increase() returns fractions: extrapolation
A window of [2m] almost never starts and ends exactly on a sample. Prometheus takes the first and last samples inside the window, computes their difference, and then extrapolates to the window's edges (by up to half a scrape interval past the last sample, or to the point where the counter would reach zero at the start). increase() is just rate() multiplied by the window length, so it inherits the fractions.
This model implements Prometheus's algorithm (extrapolatedRate in its source) and reproduces the numbers above from the same samples (down to the last digit, apart from the order of a floating-point operation in the first line):
def extrapolated(samples, range_start, range_end, is_rate):
"""samples: [(t, v)] sorted by time; PromQL windows are (start, end]."""
pts = [(t, v) for t, v in samples if range_start < t <= range_end]
if len(pts) < 2:
return None
result = pts[-1][1] - pts[0][1]
for (_, prev), (_, cur) in zip(pts, pts[1:]):
if cur < prev: # counter reset: count the drop as a restart from 0
result += prev
sampled = pts[-1][0] - pts[0][0]
avg_gap = sampled / (len(pts) - 1)
to_start = pts[0][0] - range_start
to_end = range_end - pts[-1][0]
if result > 0 and pts[0][1] >= 0: # don't extrapolate below zero
to_start = min(to_start, sampled * (pts[0][1] / result))
threshold = avg_gap * 1.1
extrapolate_to = sampled
extrapolate_to += to_start if to_start < threshold else avg_gap / 2
extrapolate_to += to_end if to_end < threshold else avg_gap / 2
result *= extrapolate_to / sampled
if is_rate:
result /= range_end - range_start
return result
start = 1767225600
requests = [(start + 15 * i, 30 * i if i <= 60 else 30 * (i - 61)) for i in range(121)]
errors, v = [], 0
for i in range(121):
v += i in {37, 41, 43, 70}
errors.append((start + 15 * i, v))
def at(minute): return start + 60 * minute
print("rate(http_requests_total[2m]) @14m ", extrapolated(requests, at(12), at(14), True))
print("rate(http_requests_total[2m]) @17m ", extrapolated(requests, at(15), at(17), True))
print("increase(http_requests_total[10m]) @17m", extrapolated(requests, at(7), at(17), False))
print("increase(errors[2m]) @11m ", extrapolated(errors, at(9), at(11), False))
print("rate(errors[2m]) @11m ", extrapolated(errors, at(9), at(11), True))rate(http_requests_total[2m]) @14m 2.0
rate(http_requests_total[2m]) @17m 1.75
increase(http_requests_total[10m]) @17m 1169.230769230769
increase(errors[2m]) @11m 2.2857142857142856
rate(errors[2m]) @11m 0.019047619047619046
The practical lessons:
- Read
increase()results as estimates. For "exactly how many errors happened?", use logs or a database, not Prometheus. - The window must contain at least two samples, or the result is empty. With a 15 s scrape interval,
[1m]is the practical minimum; make it at least 4× the scrape interval so one missed scrape doesn't create gaps. In Grafana, use$__rate_interval, which does this calculation for you (Dashboard Variables). - Always
rate()first, then aggregate:sum(rate(x[5m])), neverrate(sum(x)[5m]). Summing raw counters first hides each instance's reset, and the sum appears to drop whenever one pod restarts.
irate vs rate
irate only looks at the last two samples, so it reacts instantly and is very noisy: here it says 0, because no error happened between the last two samples, while rate says about 0.019/s. Use irate for high-resolution graphs of fast-moving counters; never for alerts (a single quiet interval resets it) or recording rules.
Test yourself
Why is rate(sum(http_requests_total)[5m]) wrong?
- It is syntactically invalid in every case
- Summing raw counters first hides per-instance resets, so a pod restart looks like a huge drop
- sum() doesn't work on counters
- It's correct
increase(errors_total[5m]) returns 2.4. What's the explanation?
- A bug in Prometheus
- Extrapolation from the samples inside the window to its edges
- Errors are counted in fractions
- The counter is a gauge
With a 30 s scrape interval, which range is the minimum safe choice for rate()?
- [30s]
- [1m]
- [2m]
- [5s]