Video in progress
How do rate() and increase() work in PromQL, and why should you apply sum() after rate()?
rate() gives the counter's growth over the range divided per second, while increase() gives the total growth over that range. Both correctly handle the counter dropping to 0 on a restart; that is why you do rate first and then sum, not the other way around.
📖 Answer
A counter's raw value (http_server_requests_seconds_count = 1,532,004) is almost useless: it depends on when the pod started. The interesting thing is the rate of growth.
rate(payment_failed_total[5m]) # average errors per second over the last 5 minutes
increase(payment_failed_total[1h]) # total errors in the last hour
increase is really rate × range, so it extrapolates and may not be a whole number (for example, 41.7). That isn't a bug.
Counter reset: a pod restarted and the value dropped from 5000 to 0. rate notices this and treats the drop as a reset, so it doesn't produce a negative rate.
Now the classic mistake:
# wrong: sum first, then rate
rate(sum(payment_failed_total)[5m:])
# right: rate for each series first, then sum
sum(rate(payment_failed_total[5m]))
In the first, if one pod restarts the sum drops, and rate takes it as a reset of the whole counter, so a big, false spike appears on the graph.
Choosing the range:
- the range should be at least 4 times the scrape interval (with a 15s scrape, at least
[1m], usually[5m]); - over a very short range, fewer than two points fall in it and the result comes out empty.
If you need to see sharp spikes, there is irate(), which looks only at the last two points. It's handy for graphs, but very noisy for alerts.
Don't apply rate to a gauge. To see how a gauge changes there are deriv() and delta().
Sign up to mark studied questions and save your progress.