Prometheus直方图

Prometheus直方图

简介

  • prometheus中的histogram是用来设置直方图指标的;

  • histogram指标是为了消除指标采样中的长尾效应;

  • Prometheus 的 histogram 是一种累积直方图;

使用

  • histogram使用时,和其他指标相比,还需要指定buckets分布;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 使用了[0.1,0.2,0.4,1,3,8,20,60,120,+Inf]这几个分桶来采样数据
# HELP prometheus_tsdb_compaction_chunk_range Final time range of chunks on their first compaction
# TYPE prometheus_tsdb_compaction_chunk_range histogram
prometheus_tsdb_compaction_chunk_range_bucket{le="100"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="1600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="6400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="25600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="102400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="409600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="1.6384e+06"} 260
prometheus_tsdb_compaction_chunk_range_bucket{le="6.5536e+06"} 780
prometheus_tsdb_compaction_chunk_range_bucket{le="2.62144e+07"} 780
prometheus_tsdb_compaction_chunk_range_bucket{le="+Inf"} 780
prometheus_tsdb_compaction_chunk_range_sum 1.1540798e+09
prometheus_tsdb_compaction_chunk_range_count 780

// 过去1小时内的P95(95%的请求耗时都小于等于这个值)
histogram_quantile(0.95, rate(prometheus_http_request_duration_seconds_bucket[1h]))

分位数

  • prometheus自带histogram_quantile()函数来计算histogram类型指标的分位数情况;

  • 该函数通过线性插值的方式计算出指定分位的指标估算值,如quantile=0.95, 表示指定采样时间范围内第95%个指标的估算值;

  • 通过分位数可以很方便的了解指标的异常采样情况;如果分位值出现毛刺,则表明该指标可能出现异常情况;

1
2
//分位值计算公式
return bucketStart + (bucketEnd-bucketStart)*float64(rank/count)

histogram和summary的区别

  • histogram可以聚合,summary无法聚合;

  • histogram在查询时计算分位值;summary在采样时计算;

  • histogram的分位数是通过插值估算来的,summary在采样时直接统计,后者根准确;

参考

  1. 一文搞懂 Prometheus 的直方图 - 云+社区 - 腾讯云

  2. Metrics类型 - prometheus-book

  3. 最佳实践 - histograms and summaries直方图和总数 - 《Prometheus 非官方中文手册》 - 书栈网 · BookStack

updatedupdated2024-05-102024-05-10