Wednesday, 4 June 2025

Prometheus rate() Function: Measuring How Fast Your Metrics Are Growing

The rate() function calculates the average per-second increase of a counter over a given time range. In simple terms, It tells you how fast a value (like a counter) is increasing every second, averaged over the specified time window.

 

Syntax

rate(metric_name[time_range])

 

Here,

·      metric_name: A Prometheus counter metric (like http_requests_total)

·      [time_range]: The range of time you want to calculate the rate over (like [5m] for 5 minutes)

 

Points to note:

·      rate() works only on counters (monotonically increasing values).

·      It returns an instant vector, meaning one data point per series for the current evaluation time.

·      It’s perfect for alerting and graphs, especially for slow-moving metrics.

 

Example

Let’s say we have a counter metric: http_requests_total, which counts total HTTP requests to your server.

 

Here’s some made-up data from a 5-minute window:

Time

prometheus_http_requests_total{handler="/metrics"}

10:00:00 

1000

10:01:00 

1100

10:02:00 

1220

10:03:00 

1300

10:04:00 

1410

10:05:00 

1500

 

So in 5 minutes, the count increased from 1000 1500, which is a total of 500 requests.

 

Now let’s calculate the rate:

rate(prometheus_http_requests_total{handler="/metrics"}[5m])

 

This gives you: 500 requests / 300 seconds = 1.6667 requests/sec

 

So Prometheus would return a single value of 1.6667 (on a per-series basis), representing average rate of increase in requests per second in the last 5 minutes.

 


You can combine rate() with aggregation functions like topk to find out which metrics are increasing the fastest. For example, which endpoints or services are handling the most HTTP requests per second.

 

topk(3, rate(prometheus_http_requests_total[5m])) 

 


avg(rate(prometheus_http_requests_total[5m])) by (code)

Calculating the average rate of increase per second per status code across multiple time series (usually instances).

 

In summary,

·      Always apply rate() before using aggregators like sum(), avg().

·      Only use it on counters and histograms (not gauges).

·      Great for alerting thresholds like rate(errors_total[5m]) > 0.5

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment