Understanding of rate() function of PromQL
Asked Answered
T

3

12

I went through the PromQL docs and found rate little bit confusing. Then I tried one query from Prometheus query dashboard and found below given results

Time Count increase  rate(count[1m])
15s  4381  0          0
30s  4381  0          0
45s  4381  0          0
1m   4381  0          0

15s  4381  0          0
30s  4402  21         0.700023
45s  4402  0          0.700023
2m   4423  21         0.7

15s  4423  0          0.7
30s  4440  17         0.56666666
45s  4440  0          0.56666666
3m   4456  16         0.53333333

Last column value I am getting from dashboard but I am not able to understand how is this calculated.

Resolution - 15s

scrape_interval: 30s

Thoughtout answered 17/3, 2021 at 14:13 Comment(0)
T
15

The "increase" function calculates how much some counter has grown and the "rate" function calculates the amount per second the measure grows.

Analyzing your data I think you used [30s] for the "increase" and [1m] for the "rate" (the correct used values are important to the result).

Basically, for example, in time 2m we have:

increase[30s] = count at 2m - count at 1.5m = 4423 - 4402 = 21
rate[1m]      = (count at 2m - count at 1m) / 60 = (4423 - 4381) / 60 = 0.7

Prometheus documentation: increase and rate.

Tartarean answered 17/3, 2021 at 18:3 Comment(2)
@macelo Yes you are correct according to my calculation also 0.7 is justified but I am curious about 0.700023 even wondering how prometheus calculated this rate.Thoughtout
This happens because rate (increase, delta, etc) performs "extrapolation". To understand what it means, look here: metricfire.com/blog/understanding-the-prometheus-rate-function/…Egoist
D
12

Prometheus calculates rate(count[d]) at timestamp t in the following way:

  1. It obtains raw samples per each time series with count name on the time range (t-d ... t]. Note that t-d timestamp isn't included in the range, while t timestamp is included in the range. For example, when calculating rate(count[1m]) at a timestamp t=2m the following raw samples are selected: 4423 @ 2m, 4402 @ 1m45s, 4402 @ 1m30s, 4381 @ 1m15s. Note that the 4381 @ 1m sample isn't included in calculations.
  2. Then it calculates the difference between the last and the first sample on the selected time range per each time series with the name count. Prometheus can detect and remove time series resets to zero on the selected time range, but let's skip this for now for the sake of clarity. In the case above it calculates 4423 @ 2m - 4381 @ 1m15s = 42.
  3. Then it divides results from step 2 by the duration d in seconds per each time series with name count. In the case above it calculates 42 / 1m = 42 / 60s = 0.7.

The actual result for rate(count[1m]) @ 2m - 0.700023 - differs from the calculated result - 0.7 - because of extrapolation, which can be applied to results calculated at step 2 if timestamps for the first and/or the last raw sample are located too far from the selected time range bounds. See more details about the extrapolation in this issue.

Note also that Prometheus misses possible counter increase on the time range [1m ... 1m15s] when calculating both rate() and increase(). See more details about this issue here and here.

Deanery answered 6/4, 2022 at 17:54 Comment(0)
B
4

I found this:

rate = Count_{now} - Count_{now-30s} / 30s

Time Count increase  rate(count[1m])

15s  4381  0          0
30s  4381  0          0
45s  4381  0          0
1m   4381  0          0

15s  4381  0          0
30s  4402  21         = [4402-4381]/30s = 0.7
45s  4402  0          = [4402-4381]/30s = 0.7
2m   4423  21         = [4423-4402]/30s = 0.700023

15s  4423  0          = [4423-4402]/30s = 0.7
30s  4440  17         = [4440-4423]/30s = 0.56666666
45s  4440  0          = [4440-4423]/30s = 0.56666666
3m   4456  16         = [4456-4440]/30s = 0.53333333
Brann answered 27/1, 2022 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.