Stage 6 · Operate
PromQL in Depth
rate and irate
Calculating counter velocity with rate(), irate(), increase(), reset handling, and scrape interval tradeoffs.
The rate() Function
rate() calculates the per-second average increase of a counter over a time range. It is the standard function for converting counters into rates. It handles counter resets gracefully by accounting for drops in value as new counters.
# Requests per second over last 5 minutes
rate(http_requests_total[5m])
# Error rate per second
rate(http_requests_total{status=~"5.."}[5m])
# Bytes per second throughput
rate(network_bytes_total[5m])rate() needs at least two samples within the range window, but it works best with 4 or more. With fewer samples, the result becomes noisy or returns no data. Ensure your range window is at least 4x your scrape interval.
The irate() Function
irate() calculates the per-second instantaneous rate using only the last two data points in the range. It responds faster to changes than rate(), but it is more sensitive to noise and scrape jitter.
# Slow and smooth — good for alerts
rate(http_requests_total[5m])
# Fast and responsive — good for dashboards
irate(http_requests_total[5m])| Function | Data Points Used | Responsiveness | Best For |
|---|---|---|---|
| rate() | All in range | Smoothed | Alerts, capacity planning |
| irate() | Last 2 only | Instantaneous | Dashboard graphs, spikes |
The increase() Function
increase() returns the total increase of a counter over a time range, in the counter's original units. It is equivalent to rate() multiplied by the range duration. Use it when you need absolute counts rather than rates.
# Total requests in last 1 hour
increase(http_requests_total[1h])
# Total errors in last 24 hours
increase(http_requests_total{status=~"5.."}[24h])
# Equivalent to rate() * 3600 for 1 hour
rate(http_requests_total[1h]) * 3600Counter Reset Handling
When a counter decreases between scrapes, Prometheus assumes a reset (process restart, target re-deployment). rate() and increase() detect resets by adding the current value to the previous value instead of counting the decrease as a negative change.
Sample 1: 100
Sample 2: 150 (+50)
Sample 3: 120 — reset detected, treated as 120, not -30
Sample 4: 200 (+80 from 120)If your range window is shorter than the gap between scrapes, rate() may miss the reset entirely and produce misleading results. Always use a range window that spans at least 4 scrape intervals.
Choosing the Right Window
The range window controls smoothing. Short windows are responsive but noisy. Long windows are smooth but hide short spikes. The right choice depends on whether you are alerting, graphing, or performing capacity planning.
| Use Case | Recommended Window | Why |
|---|---|---|
| Alert on error rate spikes | 5m | Balances responsiveness with noise reduction |
| Dashboard showing throughput | 1m - 5m | Shows recent behavior without smoothing too much |
| Capacity planning over months | 7d - 30d | Smooths out daily and weekly patterns |
Common Patterns
# QPS per endpoint
sum by(endpoint) (rate(http_requests_total[5m]))
# Error ratio (5xx / total)
sum(rate(http_requests_total{status=~"5.."}[5m]))
/
sum(rate(http_requests_total[5m]))
# Request rate per second, only endpoints with traffic
sum by(endpoint) (rate(http_requests_total[5m]))
> 0.1Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.