Course contentsShow
← Cloud and Infrastructure
Lesson 1968 of 2,98942. Observability - MetricsFree lesson

Rate and Counter Functions

Calculate per-second rates from counters using rate() and irate() for velocity measurements.

Rate and Counter Functions

You'll learn how to turn ever-increasing counter metrics into meaningful per-second rate measurements using Prometheus functions.

Why Counters Need Rate Calculations

Counters only go up (until they reset). Looking at raw counter values tells you the total accumulated events—like a car's odometer showing total miles—but not your current speed. To understand velocity (requests per second, errors per second, etc.), you need to calculate the rate of change over time.

The rate() Function

rate() calculates the average per-second increase over a time window. It handles counter resets automatically.

rate(http_requests_total[5m])

This shows the average requests per second over the last 5 minutes. Think of it as checking your average speed over a highway stretch—smoothed and reliable.

When to use: For alerting and graphing trends where you want smoothed, stable measurements.

The irate() Function

irate() calculates the instant rate using only the last two data points in the time window.

irate(http_requests_total[5m])

This captures the most recent rate of change—like glancing at your speedometer right now.

When to use: For graphs needing high sensitivity to sudden changes, showing real-time spikes or drops.

Key Differences

  • rate(): Averages across the entire window → smoother, more stable
  • irate(): Uses only the latest two points → more responsive, but noisier

Both automatically detect and handle counter resets (when a process restarts and the counter goes back to zero).

Choosing the Right Window

The [5m] window should be at least 4× your scrape interval. Too short = missing data; too long = delayed detection.

Key Takeaway:
Use rate() to convert counters into smooth per-second velocities for trends and alerts; use irate() for responsive, real-time spike detection.