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 |
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 |
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 |
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 |
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 |
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 valuest - Time in seconds to predict ahead |
Predicts linearly increasing values over time |
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.
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.
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.