Prometheus 时序数据分为 Counter, Gauge, Histogram, Summary 四种类型。
Counter
Counter 表示收集的数据是按照时间推移不断累加变化的,我们往往用它记录服务请求总量、错误总数等。
例如 Prometheus server 中 http_requests_total
, 表示 Prometheus 处理的 http 请求总数,我们可以使用 rate
, 很容易得到任意区间数据的均值,这个会在 PromQL 一节中细讲。
Gauge
Gauge 表示采集的数据是一个瞬时值,时间前后没有关联,可以任意变化(忽高忽低),比如可以用来记录内存、磁盘使用量。
例如 Prometheus server 中 go_goroutines
, 表示 Prometheus 当前 goroutines 的数量。
Histogram
Histogram 由 <basename>_bucket{le="<upper inclusive bound>"}
,<basename>_bucket{le="+Inf"}
, <basename>_sum
,<basename>_count
组成,主要对采样的数据(例如请求耗时或响应大小)按照预设的 bucket(统计区间)进行累计,然后针对某个时间范围统计区间的变化情况,可以很方便得出指标的分位值,例如我们可以用它来计算最近5分钟请求耗时的95峰值。
例如 Prometheus server 中 prometheus_http_request_duration_seconds
, 表示 Prometheus HTTP 请求耗时,我们可以用它计算耗时的分位数。
Summary
Summary 和 Histogram 类似,由 <basename>{quantile="<φ>"}
,<basename>_sum
,<basename>_count
组成,主要用于表示一段时间内数据采样结果(通常是请求持续时间或响应大小),它直接存储了 quantile 数据,而不是根据统计区间计算出来的。
例如 Prometheus server 中 prometheus_target_interval_length_seconds
。
Histogram vs Summary
-
都包含
<basename>_sum
,<basename>_count
。 -
Histogram 需要通过
<basename>_bucket
设定分布实时计算 quantile, 而 Summary 直接存储了 quantile 的值。 -
真实应用场景中,大家使用 Histogram 更多一些。
AI 总结
-
Counter (计数器):
- 特性:只增不减,表示某个事件发生的次数。
- 用途:适用于记录如请求数、错误数等统计信息。
- 示例:
http_requests_total := prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Total number of HTTP requests.", }, []string{"method", "status"}, )
-
Gauge (仪表):
- 特性:可以随时增加或减少,表示一个特定的值。
- 用途:适用于记录如内存使用量、当前活动用户数等动态的信息。
- 示例:
memory_usage := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "memory_usage", Help: "Current memory usage.", }, []string{"application"}, )
-
Histogram (直方图):
- 特性:用来统计值的分布,持有桶(buckets),可以观察到数值的分布情况以及总数。
- 用途:适用于记录与请求处理时间等相关的分布情况。
- 示例:
request_duration := prometheus.NewHistogramVec( prometheus.HistogramOpts{ Name: "request_duration_seconds", Help: "Histogram of response durations.", Buckets: prometheus.DefBuckets, }, []string{"method"}, )
-
Summary (摘要):
- 特性:类似于直方图,提供数据的特定分位数(percentiles)以及总体总数。
- 用途:适用于需要计算特定分位(如中位数、百分位等)的场景。
- 示例:
request_duration_summary := prometheus.NewSummaryVec( prometheus.SummaryOpts{ Name: "request_duration_summary", Help: "Summary of response durations.", Objectives: map[float64]float64{ 0.5: 0.05, // 50th percentile 0.9: 0.01, // 90th percentile }, }, []string{"method"}, )
- metrics raw output
# HELP http_requests_total Total number of HTTP requests
# TYPE http_requests_total counter
http_requests_total 12
# HELP http_active_connections Current number of active HTTP connections
# TYPE http_active_connections gauge
http_active_connections 1
# HELP http_response_time_seconds Histogram of HTTP response time
# TYPE http_response_time_seconds histogram
http_response_time_seconds_bucket{le="0.001"} 0
http_response_time_seconds_bucket{le="0.01"} 1
http_response_time_seconds_bucket{le="0.1"} 10
http_response_time_seconds_bucket{le="1"} 11
http_response_time_seconds_bucket{le="2"} 11
http_response_time_seconds_bucket{le="3"} 11
http_response_time_seconds_bucket{le="5"} 11
http_response_time_seconds_bucket{le="+Inf"} 11
http_response_time_seconds_sum 0.2799697239999999
http_response_time_seconds_count 11
# HELP http_response_time_summary Summary of HTTP response time
# TYPE http_response_time_summary summary
http_response_time_summary{quantile="0.5"} 0.013999999999999999
http_response_time_summary{quantile="0.9"} 0.07140000000000003
http_response_time_summary{quantile="0.99"} 0.117
http_response_time_summary_sum 0.2970000000000001
http_response_time_summary_count 11