![](/img/trans.png)
[英]Understanding histogram_quantile based on rate in Prometheus
[英]How to get the quantile of rate in prometheus
我正在看这篇文章
# TYPE prometheus_http_request_duration_seconds histogram
prometheus_http_request_duration_seconds_bucket{handler="/",le="0.1"} 25547
prometheus_http_request_duration_seconds_bucket{handler="/",le="0.2"} 26688
prometheus_http_request_duration_seconds_bucket{handler="/",le="0.4"} 27760
prometheus_http_request_duration_seconds_bucket{handler="/",le="1"} 28641
prometheus_http_request_duration_seconds_bucket{handler="/",le="3"} 28782
我很困惑为什么
histogram_quantile(0.9,
rate(prometheus_http_request_duration_seconds_bucket[5m])
)
不给你单位observe event / second
的速率分位数,而是给你单位second / observe event
的请求持续时间的分位数
rate(prometheus_http_request_duration_seconds_bucket[5m]
应该给你number of observe event in certain bucket / second
5 分钟内的平均秒数
我想histogram_quantile
然后会给你率分位数
我一定是理解错了
如果您需要在 Prometheus 中对比率进行分位数,那么您可能需要以下 PromQL 查询:
quantile_over_time(0.95, rate(some_counter[5m])[24h:5m])
它计算过去 24 小时内给定some_counter
在 5 分钟间隔内的平均每秒速率(例如,它计算 24h/5m=288 速率结果),然后计算这些速率结果的第 95 个百分位数。
此查询使用子查询功能和quantile_over_time function。
rate()
函数在这里指定分位数计算的时间窗口,如histogram_quantile() 函数所示。 它的意思是“在过去 5 分钟内,我的 90% 的用户经历的最大 http 响应时间是多少?”
histogram_quantile()
函数通过假设桶内的线性分布来插入分位数值, le
给出最大观察时间。 桶是一个计数器,用于测量自过程开始以来观察的发生次数。 rate()
通过计算每秒(平均)观察的出现次数来建立链接,从中可以在时间窗口内插入响应时间(平均)。
你是对的,因为平均值,它不是 100% 准确的度量,但函数做了很多假设,并且桶的选择已经引入了偏差。
我猜你可以使用irate()
来计算瞬时分位数,但它可能会更嘈杂。
这和这里是 prometheus 中 historgram_quantile 的代码。
举个例子,
assumed the original bucket is :
[50][100][150][200][200] with corresponding upperbound 5s,10s,15s,20s,+Inf.
then the rate(xx[5m]) returned a bucket like this:
[20/5*60][40/5*60][60/5*60][80/5*60][80/5*60]
histogram_quantile will delegate the returned bucket to another function bucketQuantile.
It used the rough following logic to compute the percentile:
1) get the total rank of the percentile
such as 90ile is 0.9 * total counts = 0.9 * (80/5*60)
2) compute the value of 90ile
last upperbound before the total rank position is 15 secs;
current upperbound of the total rank is 20 secs;
the count in the bucket that 90ile position belongs is (80/5*60)-(60/5*60);
the internal rank in that single bucket of 90ile position is (0.9 * 80/5*60)-(60/5*60);
finally, the value of 90ile is: 15 sec + (internal rank / that bucket count) * (20sec-15sec) = 15 + 3 * ( (0.9 * 80/5*60)-(60/5*60) / (80/5*60)-(60/5*60) ) =
15 + 3 * ( (0.9*80 - 60)/(80-60) ) = 15 + 3 * ( 12/20) = 15+3*0.6= 16.8 sec
就是这样,你可以看到分母 5*60 在计算中实际上没有影响。 所以 rate() 函数只是用来指定时间窗口 5 分钟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.