Prometheus Go client library 详解

开发 后端
本文我们主要介绍四种指标类型的含义,通过 Label 可以将四种类型的指标数据,按照 Label 的维度分组统计,我们以支持 Label 的 CounterVec 为例,介绍了它的使用方式。

介绍

Prometheus 支持 4 种 指标类型,分别是 Counter、Gauge、Histogram 和 Summary。

  • Counter 指标类型,指标值是只能递增,不能递减的数值。需要注意的是,当 Prometheus server 重启时,指标值会被重置为 0。该指标类型可用于统计接口的请求数、错误数等使用场景。
  • Gauge 指标类型,指标值是可增可减的数值。该指标类型可用于统计 CPU、内存和硬盘的使用情况,goroutine 的数量等使用场景。
  • Histogram 指标类型,指标值基于桶分布。开发者可以自定义桶的区间。该指标类型可用于统计接口的延时请求数等使用场景。
  • Summary 指标类型,与 Histogram 类似,区别是 Histogram 直接统计了不同区间中的指标数值,而 Summary 是基于客户端级别,因此不能统计多个实例的聚合数据。该指标类型可用于预先不知道指标桶划分区间的场景。

使用方式

一般在实际应用场景中,通常一个指标需要对应多条时序数据(Label Name 为维度),此时就需要使用支持标签的指标类型。

Prometheus 有 4 种支持标签的指标类型,分别是 ConterVec、GaugeVec、HistogramVec、SummaryVec。

1.CounterVec

CounterVec 与 Counter 的区别是,它支持 Label,我们可以按照 Lable 维度,将同一个指标的数据按照 Lable 分组统计。例如,同一个 Api 接口的请求数,我们可以定义 Lable (Code、Method),按照状态码和 HTTP 请求方式,分组统计同一个 Api 接口的请求数。

示例代码:

var (
 // 标签名
 labelNames = []string{"host", "code", "path", "method"}
 // HttpReqs 实例化 CounterVec
 HttpReqs *prometheus.CounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
  Name: "http_requests_total",
  Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
 },
  labelNames,
 )
)

阅读上面这段代码,我们使用 NewCounterVec 创建一个实例,它支持多个方法,我们可以使用其中一个性能相对较高的方法 WithLabelValues,返回一个 Counter。

示例代码:

func Metrics() gin.HandlerFunc {
 return func(c *gin.Context) {
  c.Next()
  host := c.RemoteIP()
  code := fmt.Sprintf("%d", c.Writer.Status())
  method := c.Request.Method
  labelsByHttpReqs := []string{host, code, c.FullPath(), method}
  prometheus_metrics.HttpReqs.WithLabelValues(labelsByHttpReqs...).Inc()
 }
}

Counter 支持两个方法,分别是 Inc() 和 Add(),其中 Inc() 将 Counter 增加 1,Add() 将 Counter 增加给定值,需要注意的是,给定值必须为非负值,否则会引发 panic。

需要注意的是,在我们创建指标之后,还需要使用 Register() 接口的 Register() 方法,注册之后才可以被收集到指标数据。如果需要注册多个指标,可以使用 MustRegister() 方法。

示例代码:

reg := prometheus.NewRegistry()
reg.MustRegister(prometheus_metrics.HttpReqs, prometheus_metrics.OpsQueued, prometheus_metrics.Latencies, prometheus_metrics.Temps)

2.GaugeVec

GaugeVec 与 Gauge 的区别是,它支持 Label,我们可以按照 Lable 维度,将同一个指标的数据按照 Lable 分组统计。

示例代码:

var (
 labelNamesByOpsQueued = []string{
  "user",
  "type",
 }
 OpsQueued = prometheus.NewGaugeVec(
  prometheus.GaugeOpts{
   Name:      "ops_queued",
   Help:      "Number of blob storage operations waiting to be processed, partitioned by user and type.",
  },
  labelNamesByOpsQueued,
 )
)

阅读上面这段代码,我们使用 NewGaugeVec 创建实例。

3.HistogramVec

HistogramVec 与 Histogram 的区别是,它支持 Label,我们可以按照 Lable 维度,将同一个指标的数据按照 Lable 分组统计。

示例代码:

var (
 labelNamesByLatencies = []string{"method", "code"}
 Latencies             = prometheus.NewHistogramVec(
  prometheus.HistogramOpts{
   Name:    "http_request_duration_seconds",
   Help:    "Tracks the latencies for HTTP requests.",
   Buckets: []float64{0.99, 0.9, 0.5},
  },
  labelNamesByLatencies,
 )
)

4.SummaryVec

SummaryVec 与 Summary 的区别是,它支持 Label,我们可以按照 Lable 维度,将同一个指标的数据按照 Lable 分组统计。

示例代码:

var (
 labelNamesByTemps = []string{"species"}
 Temps             = prometheus.NewSummaryVec(
  prometheus.SummaryOpts{
   Name:       "pond_temperature_celsius",
   Help:       "The temperature of the frog pond.",
   Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
  },
  labelNamesByTemps,
 )
)

阅读上面这段代码,使用 NewSummaryVec 创建实例。

总结

本文我们主要介绍 4 种指标类型的含义,通过 Label 可以将 4 种类型的指标数据,按照 Label 的维度分组统计,我们以支持 Label 的 CounterVec 为例,介绍了它的使用方式,其余 3 种支持 Label 的指标也提供了简单的使用示例。

责任编辑:赵宁宁 来源: Golang语言开发栈
相关推荐

2022-08-31 09:28:04

MQTTOthers

2013-01-05 09:21:55

Ubuntu for UbuntuAndroid

2021-10-14 08:07:33

Go 应用Prometheus监控

2010-03-17 12:20:15

Python Libr

2023-04-17 14:32:20

2023-11-26 23:59:16

PromQL算术运算符

2010-03-17 12:53:43

Python Libr

2009-03-04 10:38:36

DDCClient桌面虚拟化

2023-11-06 01:39:02

Go语言开发

2023-04-02 23:13:07

Go语言bufio

2023-07-14 08:12:21

计时器unsafecontext

2023-02-10 09:40:36

Go语言并发

2023-04-09 23:09:59

Go语言函数

2021-02-06 18:19:54

TimeGo语言

2021-05-24 09:15:42

Go熔断熔断器

2009-09-10 11:10:21

Linq Librar

2016-12-12 09:01:47

Amazon Go核心技术

2021-11-28 22:33:01

Go选项模式

2013-01-18 10:47:46

IBMdW

2020-10-14 08:33:23

Prometheus监控体系
点赞
收藏

51CTO技术栈公众号