简体   繁体   English

PromQL的rate()function的理解

[英]Understanding of rate() function of PromQL

I went through the PromQL docs and found rate little bit confusing.我浏览了 PromQL 文档,发现 rate 有点令人困惑。 Then I tried one query from Prometheus query dashboard and found below given results然后我尝试了来自 Prometheus 查询仪表板的一个查询,发现下面给出的结果

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分辨率 - 15s

scrape_interval: 30s scrape_interval:30s

Prometheus calculates rate(count[d]) at timestamp t in the following way: Prometheus 通过以下方式计算时间戳t处的rate(count[d])

  1. It obtains raw samples per each time series with count name on the time range (td... t] . Note that td 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.它获取每个时间序列的原始样本, count名称在时间范围内(td... t] 。请注意, td时间戳不包含在范围内,而t时间戳包含在范围内。例如,在计算rate(count[1m])rate(count[1m])在时间戳t=2m时选择了以下原始样本: 4423 @ 2m, 4402 @ 1m45s, 4402 @ 1m30s, 4381 @ 1m15s 。请注意, 4381 @ 1m样本不包括在计算中。
  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 .然后它计算每个时间序列在选定时间范围内的最后一个样本和第一个样本之间的差异,名称为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. Prometheus 可以检测并删除选定时间范围内的时间序列重置为零,但为了清楚起见,我们暂时跳过这一点。 In the case above it calculates 4423 @ 2m - 4381 @ 1m15s = 42 .在上面的例子中,它计算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 .然后它将步骤 2 的结果除以每个时间序列的持续时间d (以秒为单位),名称为count In the case above it calculates 42 / 1m = 42 / 60s = 0.7 .在上面的例子中,它计算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. rate(count[1m]) @ 2m - 0.700023的实际结果与计算结果不同 - 0.7 - 因为外推,如果第一个和/或最后一个原始样本的时间戳,则可以将其应用于在步骤 2 计算的结果距离选定的时间范围边界太远。 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() .另请注意,在计算rate()increase()时,Prometheus 错过了时间范围[1m... 1m15s]上可能的计数器增加。 See more details about this issue here and here . 在此处此处查看有关此问题的更多详细信息。

The "increase" function calculates how much some counter has grown and the "rate" function calculates the amount per second the measure grows. “增加” function 计算一些计数器增长了多少,“速率” function 计算每秒度量增长的数量。

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).分析您的数据我认为您使用 [30s] 表示“增加”,使用 [1m] 表示“速率”(正确使用的值对结果很重要)。

Basically, for example, in time 2m we have:基本上,例如,在时间 2m 我们有:

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 . Prometheus 文档: 增加速率

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM