Prometheus的4大指标类型

Prometheus 会将所有采集到的样本数据以 时间序列的方式保存,每条时间序列通过指标名称和一组标签集命名。

在时间序列中的每一个点称为一个 样本(sample),样本由以下三部分组成:

1)指标(metric):指标名称和描述当前样本特征的标签集。

2)时间戳(timestamp):一个精确到毫秒的时间戳。

3)样本值(value): 一个 float64 的浮点型数据表示当前样本的值。

一个规范的指标数据一般包含以下三个部分:

1)# HELP,说明该指标的用途。

2)# TYPE,说明该指标的数据类型。

3)具体采集的指标。

其中指标的具体格式如下:

<指标名称>{<标签名称>=<标签值>, ...} 数据

其中指标的数据类型有如下4种,都在export提供的/metrics接口的type行有展示。

1)counter:计数器

2)gauge:仪表盘

3)histogram:直方图

4)summary:摘要
 

counter

计数器,只能增加不能减小,常用于表示发生的错误数、已完成的任务数、服务的请求数。

安装并且启动好prometheus后,可以访问http://localhost:9090/metrics来看到prometheus自己的指标。

# HELP prometheus_http_requests_total Counter of HTTP requests.
# TYPE prometheus_http_requests_total counter
prometheus_http_requests_total{code="200",handler="/"} 0
prometheus_http_requests_total{code="200",handler="/-/healthy"} 0
prometheus_http_requests_total{code="200",handler="/-/quit"} 0
prometheus_http_requests_total{code="200",handler="/-/ready"} 4
prometheus_http_requests_total{code="200",handler="/-/reload"} 0
prometheus_http_requests_total{code="200",handler="/alerts"} 0
prometheus_http_requests_total{code="200",handler="/api/v1/*path"} 0

gauge

仪表盘,记录瞬时值,是什么值就是什么,可大可小,常用于记录CPU的LOAD值、内存使用率、磁盘使用率、线程数、连接数等类似的指标。

# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 33
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.21.0"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.0341712e+07
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.533216e+06
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 5.9602e+06

histogram

直方图,按照样本的指标值的区间进行分组,统计样本的指标值出现在各区间的次数,可以清晰的知道指标值的区间分布规律,常用于请求响应时间的区间分布、请求响应的字节数大小区间分布等类似数据。要获取样本的指标值的分位数需要在服务端进行计算,会消耗服务端的计算资源。

直方图包含3种类型的指标:

1)样本的指标值的在分组中的数量,命名为_bucket{le=""},表示指标值小于等于上边界的所有样本的数量。

2)所有样本的指标值的总和,命名为_sum。

3)样本总数,命名为_count。

安装并且启动好prometheus后,可以访问http://localhost:9090/metrics来看到prometheus自己的指标。下面的例子来自prometheus自带的http请求耗时的histogram信息,可以看出请求路径为“/”的http请求中,响应时间小于0.1秒的有2个,小于0.2秒的有5,小于0.4秒的有9个,小于1秒的有11个,总的请求次数为11次,总的请求耗时为 2.000284668秒。

# HELP prometheus_http_request_duration_seconds Histogram of latencies for HTTP requests.
# TYPE prometheus_http_request_duration_seconds histogram
prometheus_http_request_duration_seconds_bucket{handler="/",le="0.1"} 2
prometheus_http_request_duration_seconds_bucket{handler="/",le="0.2"} 5
prometheus_http_request_duration_seconds_bucket{handler="/",le="0.4"} 9
prometheus_http_request_duration_seconds_bucket{handler="/",le="1"} 11
prometheus_http_request_duration_seconds_bucket{handler="/",le="3"} 11
prometheus_http_request_duration_seconds_bucket{handler="/",le="8"} 11
prometheus_http_request_duration_seconds_bucket{handler="/",le="20"} 11
prometheus_http_request_duration_seconds_bucket{handler="/",le="60"} 11
prometheus_http_request_duration_seconds_bucket{handler="/",le="120"} 11
prometheus_http_request_duration_seconds_bucket{handler="/",le="+Inf"} 11
prometheus_http_request_duration_seconds_sum{handler="/"} 2.000284668
prometheus_http_request_duration_seconds_count{handler="/"} 11

summary

摘要,和histogram类似,记录一段时间内样本的指标值的分布结果,由客户端计算好样本指标值的分位数结果,服务端可以直接展示,不需要消耗计算资源。

summary类型的样本也会提供3种指标,假设指标名称为。

1)样本值的分位数分布情况,命名为{quantile="<>"}。

2)所有样本值的大小总和,命名为_sum。

3)样本总数,命名为_count。

安装并且启动好prometheus后,可以访问http://localhost:9090/metrics来看到prometheus自己的指标。下面的例子来自prometheus自带的GC的summary信息,可以看出GO语言的GC总次数是213,GC的总耗时是0.25431598秒,其中的中位数为(quantile="0.5")的耗时为0.001254684秒,也就是说213次GC中有50%的次数是小于0.001254684秒的。

# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.9042e-05
go_gc_duration_seconds{quantile="0.25"} 0.000584189
go_gc_duration_seconds{quantile="0.5"} 0.001254684
go_gc_duration_seconds{quantile="0.75"} 0.001590227
go_gc_duration_seconds{quantile="1"} 0.006444609
go_gc_duration_seconds_sum 0.25431598
go_gc_duration_seconds_count 213

猜你喜欢

转载自blog.csdn.net/zhangzhaokun/article/details/132670048