Skip to main content

Working with PromQL Rate Functions

1. Calculating Rate

The rate() function in PromQL calculates the per-second average rate of increase of a counter over a given time range.

Function Parameters Description
rate(v range-vector) v - A range vector of counter values Returns the per-second average rate over the specified time window

Example: Calculating rate over 5 minutes

rate(http_requests_total[5m])

2. Using irate for Instant Rate

The irate() function computes the per-second rate of increase based on the last two data points in a range vector.

Function Parameters Description
irate(v range-vector) v - A range vector of counter values Returns the per-second rate based on the last two samples

Example: Using irate for instant rate

irate(http_requests_total[5m])

3. Computing Increase

The increase() function calculates the total increase in a counter over a specified time range.

Function Parameters Description
increase(v range-vector) v - A range vector of counter values Returns the total increase over the specified time window

Example: Calculating increase over 1 hour

increase(http_requests_total[1h])

4. Computing Delta

The delta() function computes the difference between the first and last value in a range vector.

Function Parameters Description
delta(v range-vector) v - A range vector of metric values Returns the difference between first and last value in the time window

Example: Calculating delta over 10 minutes

delta(cpu_usage_seconds_total[10m])

5. Using idelta for Instant Delta

The idelta() function returns the difference between the last two samples in a range vector.

Function Parameters Description
idelta(v range-vector) v - A range vector of metric values Returns the difference between the last two samples

Example: Using idelta for instant delta

idelta(http_requests_total[5m])

6. Predicting Linear Values

The predict_linear() function predicts future values based on linear regression over a specified time window.

Function Parameters Description
predict_linear(v range-vector, t scalar) v - A range vector of metric values
t - Time in seconds to predict ahead
Predicts linearly increasing values over time

Example: Predicting linear growth over 1 hour

predict_linear(http_requests_total[1h], 3600)

7. Understanding Rate vs irate

While rate() averages over the entire time window, irate() focuses on the last two samples.

Rate vs irate

  • rate(): Averages over the entire time window, smoother but less responsive
  • irate(): Based on last two samples, more sensitive to spikes

8. Handling Counter Resets

Prometheus handles counter resets automatically by detecting and adjusting for resets in counter values.

Note: When a counter resets, Prometheus adjusts the rate calculation to account for the reset.

9. Choosing Range Duration

Selecting an appropriate range duration is crucial for accurate rate calculations.

Duration Use Case Recommendation
5m Short-term trends Good for detecting recent anomalies
1h Longer-term trends Useful for identifying sustained patterns
1d Historical analysis Best for analyzing daily behavior

10. Using Rate with Aggregations

The rate() function can be combined with aggregation operators like sum, avg, etc.

Example: Aggregated rate calculation

sum(rate(http_requests_total[5m])) by (job)

11. Calculating Per-Second Rates

Per-second rates are essential for understanding instantaneous changes in counter metrics.

Function Description Use Case
rate(v range-vector) Per-second average rate Monitoring system performance
irate(v range-vector) Per-second rate based on last two samples Detecting sudden changes or spikes

12. Avoiding Rate Pitfalls

Common pitfalls when using rate functions include incorrect time windows and ignoring counter resets.

Common Pitfalls

  • Using too short a time window can lead to noisy data
  • Not accounting for counter resets can cause incorrect rate calculations
  • Applying rate() to non-counter metrics can produce misleading results

Was this article helpful?