为 Go 应用添加 Prometheus 监控指标

运维 系统运维
我们首先从一个最简单的 Go 应用程序开始,在端口 8080 的 /metrics 端点上暴露客户端库的默认注册表,暂时还没有跟踪任何其他自定义的监控指标。

[[428815]]

前面我们了解了如何仪表化应用,接下来我们将学习使用 Prometheus 的 Go 客户端库来为一个 Go 应用程序添加和暴露监控指标。

创建应用

我们首先从一个最简单的 Go 应用程序开始,在端口 8080 的 /metrics 端点上暴露客户端库的默认注册表,暂时还没有跟踪任何其他自定义的监控指标。

先创建一个名为 instrument-demo 的目录,在该目录下面初始化项目:

  1. ☸ ➜ mkdir instrument-demo && cd instrument-demo 
  2. ☸ ➜ go mod init github.com/cnych/instrument-demo 

上面的命令会在 instrument-demo 目录下面生成一个 go.mod 文件,在同目录下面新建一个 main.go 的入口文件,内容如下所示:

  1. package main 
  2.  
  3. import ( 
  4.  "net/http" 
  5.  
  6.  "github.com/prometheus/client_golang/prometheus/promhttp" 
  7.  
  8. func main() { 
  9.     // Serve the default Prometheus metrics registry over HTTP on /metrics. 
  10.  http.Handle("/metrics", promhttp.Handler()) 
  11.  http.ListenAndServe(":8080", nil) 

然后执行下面的命令下载 Prometheus 客户端库依赖:

  1. ☸ ➜ export GOPROXY="https://goproxy.cn" 
  2. ☸ ➜ go mod tidy 
  3. go: finding module for package github.com/prometheus/client_golang/prometheus/promhttp 
  4. go: found github.com/prometheus/client_golang/prometheus/promhttp in github.com/prometheus/client_golang v1.11.0 
  5. go: downloading google.golang.org/protobuf v1.26.0-rc.1 

然后直接执行 go run 命令启动服务:

  1. ☸ ➜ go run main.go 

然后我们可以在浏览器中访问 http://localhost:8080/metrics 来获得默认的监控指标数据:

  1. # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. 
  2. # TYPE go_gc_duration_seconds summary 
  3. go_gc_duration_seconds{quantile="0"} 0 
  4. go_gc_duration_seconds{quantile="0.25"} 0 
  5. go_gc_duration_seconds{quantile="0.5"} 0 
  6. go_gc_duration_seconds{quantile="0.75"} 0 
  7. go_gc_duration_seconds{quantile="1"} 0 
  8. go_gc_duration_seconds_sum 0 
  9. go_gc_duration_seconds_count 0 
  10. # HELP go_goroutines Number of goroutines that currently exist. 
  11. # TYPE go_goroutines gauge 
  12. go_goroutines 6 
  13. ...... 
  14. # HELP go_threads Number of OS threads created. 
  15. # TYPE go_threads gauge 
  16. go_threads 8 
  17. # HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served. 
  18. # TYPE promhttp_metric_handler_requests_in_flight gauge 
  19. promhttp_metric_handler_requests_in_flight 1 
  20. # HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code. 
  21. # TYPE promhttp_metric_handler_requests_total counter 
  22. promhttp_metric_handler_requests_total{code="200"} 1 
  23. promhttp_metric_handler_requests_total{code="500"} 0 
  24. promhttp_metric_handler_requests_total{code="503"} 0 

我们并没有在代码中添加什么业务逻辑,但是可以看到依然有一些指标数据输出,这是因为 Go 客户端库默认在我们暴露的全局默认指标注册表中注册了一些关于 promhttp 处理器和运行时间相关的默认指标,根据不同指标名称的前缀可以看出:

  • go_*:以 go_ 为前缀的指标是关于 Go 运行时相关的指标,比如垃圾回收时间、goroutine 数量等,这些都是 Go 客户端库特有的,其他语言的客户端库可能会暴露各自语言的其他运行时指标。
  • promhttp_*:来自 promhttp 工具包的相关指标,用于跟踪对指标请求的处理。

这些默认的指标是非常有用,但是更多的时候我们需要自己控制,来暴露一些自定义指标。这就需要我们去实现自定义的指标了。

添加自定义指标

接下来我们来自定义一个的 gauge 指标来暴露当前的温度。创建一个新的文件 custom-metric/main.go,内容如下所示:

  1. package main 
  2.  
  3. import ( 
  4.  "net/http" 
  5.  
  6.  "github.com/prometheus/client_golang/prometheus" 
  7.  "github.com/prometheus/client_golang/prometheus/promhttp" 
  8.  
  9. func main() { 
  10.     // 创建一个没有任何 label 标签的 gauge 指标 
  11.  temp := prometheus.NewGauge(prometheus.GaugeOpts{ 
  12.   Name"home_temperature_celsius"
  13.   Help: "The current temperature in degrees Celsius."
  14.  }) 
  15.  
  16.  // 在默认的注册表中注册该指标 
  17.  prometheus.MustRegister(temp
  18.  
  19.  // 设置 gauge 的值为 39 
  20.  temp.Set(39) 
  21.  
  22.  // 暴露指标 
  23.  http.Handle("/metrics", promhttp.Handler()) 
  24.  http.ListenAndServe(":8080", nil) 

上面文件中和最初的文件就有一些变化了:

  • 我们使用 prometheus.NewGauge() 函数创建了一个自定义的 gauge 指标对象,指标名称为 home_temperature_celsius,并添加了一个注释信息。
  • 然后使用 prometheus.MustRegister() 函数在默认的注册表中注册了这个 gauge 指标对象。
  • 通过调用 Set() 方法将 gauge 指标的值设置为 39。
  • 然后像之前一样通过 HTTP 暴露默认的注册表。

需要注意的是除了 prometheus.MustRegister() 函数之外还有一个 prometheus.Register() 函数,一般在 golang 中我们会将 Mustxxx 开头的函数定义为必须满足条件的函数,如果不满足会返回一个 panic 而不是一个 error 操作,所以如果这里不能正常注册的话会抛出一个 panic。

现在我们来运行这个程序:

  1. ☸ ➜ go run ./custom-metric 

启动后重新访问指标接口 http://localhost:8080/metrics,仔细对比我们会发现多了一个名为 home_temperature_celsius 的指标:

  1. ... 
  2. # HELP home_temperature_celsius The current temperature in degrees Celsius. 
  3. # TYPE home_temperature_celsius gauge 
  4. home_temperature_celsius 42 
  5. ... 

这样我们就实现了添加一个自定义的指标的操作,整体比较简单,当然在实际的项目中需要结合业务来确定添加哪些自定义指标。

自定义注册表

前面我们是使用 prometheus.MustRegister() 函数来将指标注册到全局默认注册中,此外我们还可以使用 prometheus.NewRegistry() 函数来创建和使用自己的非全局的注册表。

既然有全局的默认注册表,为什么我们还需要自定义注册表呢?这主要是因为:

  • 全局变量通常不利于维护和测试,创建一个非全局的注册表,并明确地将其传递给程序中需要注册指标的地方,这也一种更加推荐的做法。
  • 全局默认注册表包括一组默认的指标,我们有时候可能希望除了自定义的指标之外,不希望暴露其他的指标。

下面的示例程序演示了如何创建、使用和暴露一个非全局注册表对象,创建一个文件 custom-registry/main.go,内容如下所示:

  1. package main 
  2.  
  3. import ( 
  4.  "net/http" 
  5.  
  6.  "github.com/prometheus/client_golang/prometheus" 
  7.  "github.com/prometheus/client_golang/prometheus/promhttp" 
  8.  
  9. func main() { 
  10.  // 创建一个自定义的注册表 
  11.  registry := prometheus.NewRegistry() 
  12.  // 可选: 添加 process 和 Go 运行时指标到我们自定义的注册表中 
  13.  registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) 
  14.  registry.MustRegister(prometheus.NewGoCollector()) 
  15.  
  16.  // 创建一个简单呃 gauge 指标。 
  17.  temp := prometheus.NewGauge(prometheus.GaugeOpts{ 
  18.   Name"home_temperature_celsius"
  19.   Help: "The current temperature in degrees Celsius."
  20.  }) 
  21.  
  22.  // 使用我们自定义的注册表注册 gauge 
  23.  registry.MustRegister(temp
  24.  
  25.  // 设置 gague 的值为 39 
  26.  temp.Set(39) 
  27.  
  28.  // 暴露自定义指标 
  29.  http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{Registry: registry})) 
  30.  http.ListenAndServe(":8080", nil) 

上面我们没有使用全局默认的注册表了,而是创建的一个自定义的注册表:

  • 首先使用 prometheus.NewRegistry() 函数创建我们自己的注册表对象。
  • 然后使用自定义注册表对象上面的 MustRegister() 哈是来注册 guage 指标,而不是调用 prometheus.MustRegister() 函数(这会使用全局默认的注册表)。
  • 如果我们希望在自定义注册表中也有进程和 Go 运行时相关的指标,我们可以通过实例化 Collector 收集器来添加他们。
  • 最后在暴露指标的时候必须通过调用 promhttp.HandleFor() 函数来创建一个专门针对我们自定义注册表的 HTTP 处理器,为了同时暴露前面示例中的 promhttp_* 相关的指标,我们还需要在 promhttp.HandlerOpts 配置对象的 Registry 字段中传递我们的注册表对象。

同样我们重新运行上面的自定义注册表程序:

  1. ☸ ➜ go run ./custom-metric 

启动后再次访问指标接口 http://localhost:8080/metrics,可以发现和上面示例中的指标数据是相同的。

指标定制

Gauges

前面的示例我们已经了解了如何添加 gauge 类型的指标,创建了一个没有任何标签的指标,直接使用 prometheus.NewGauge() 函数即可实例化一个 gauge 类型的指标对象,通过 prometheus.GaugeOpts 对象可以指定指标的名称和注释信息:

  1. queueLength := prometheus.NewGauge(prometheus.GaugeOpts{ 
  2.  Name"queue_length"
  3.  Help: "The number of items in the queue."
  4. }) 

我们知道 gauge 类型的指标值是可以上升或下降的,所以我们可以为 gauge 指标设置一个指定的值,所以 gauge 指标对象暴露了 Set()、Inc()、Dec()、Add() 和 Sub() 这些函数来更改指标值:

  1. // 使用 Set() 设置指定的值 
  2. queueLength.Set(0) 
  3.  
  4. // 增加或减少 
  5. queueLength.Inc()   // +1:Increment the gauge by 1. 
  6. queueLength.Dec()   // -1:Decrement the gauge by 1. 
  7. queueLength.Add(23) // Increment by 23. 
  8. queueLength.Sub(42) // Decrement by 42. 

另外 gauge 仪表盘经常被用来暴露 Unix 的时间戳样本值,所以也有一个方便的方法来将 gauge 设置为当前的时间戳:

  1. demoTimestamp.SetToCurrentTime() 

最终 gauge 指标会被渲染成如下所示的数据:

  1. # HELP queue_length The number of items in the queue. 
  2. # TYPE queue_length gauge 
  3. queue_length 42 

Counters

要创建一个 counter 类型的指标和 gauge 比较类似,只是用 prometheus.NewCounter() 函数来初始化指标对象:

  1. totalRequests := prometheus.NewCounter(prometheus.CounterOpts{ 
  2.  Name"http_requests_total"
  3.  Help: "The total number of handled HTTP requests."
  4. }) 

我们知道 counter 指标只能随着时间的推移而不断增加,所以我们不能为其设置一个指定的值或者减少指标值,所以该对象下面只有 Inc() 和 Add() 两个函数:

  1. totalRequests.Inc()   // +1:Increment the counter by 1. 
  2. totalRequests.Add(23) // +n:Increment the counter by 23. 

当服务进程重新启动的时候,counter 指标值会被重置为 0,不过不用担心数据错乱,我们一般会使用的 rate() 函数会自动处理。

最终 counter 指标会被渲染成如下所示的数据:

  1. # HELP http_requests_total The total number of handled HTTP requests. 
  2. # TYPE http_requests_total counter 
  3. http_requests_total 7734 

Histograms

创建直方图指标比 counter 和 gauge 都要复杂,因为需要配置把观测值归入的 bucket 的数量,以及每个 bucket 的上边界。Prometheus 中的直方图是累积的,所以每一个后续的 bucket 都包含前一个 bucket 的观察计数,所有 bucket 的下限都从 0 开始的,所以我们不需要明确配置每个 bucket 的下限,只需要配置上限即可。

同样要创建直方图指标对象,我们使用 prometheus.NewHistogram() 函数来进行初始化:

  1. requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{ 
  2.   Name:    "http_request_duration_seconds"
  3.   Help:    "A histogram of the HTTP request durations in seconds."
  4.   // Bucket 配置:第一个 bucket 包括所有在 0.05s 内完成的请求,最后一个包括所有在10s内完成的请求。 
  5.   Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, 
  6. }) 

这里和前面不一样的地方在于除了指定指标名称和帮助信息之外,还需要配置 Buckets。如果我们手动去枚举所有的 bucket 可能很繁琐,所以 Go 客户端库为为我们提供了一些辅助函数可以帮助我们生成线性或者指数增长的 bucket,比如 prometheus.LinearBuckets() 和 prometheus.ExponentialBuckets() 函数。

直方图会自动对数值的分布进行分类和计数,所以它只有一个 Observe() 方法,每当你在代码中处理要跟踪的数据时,就会调用这个方法。例如,如果你刚刚处理了一个 HTTP 请求,花了 0.42 秒,则可以使用下面的代码来跟踪。

  1. requestDurations.Observe(0.42) 

由于跟踪持续时间是直方图的一个常见用例,Go 客户端库就提供了辅助函数,用于对代码的某些部分进行计时,然后自动观察所产生的持续时间,将其转化为直方图,如下代码所示:

  1. // 启动一个计时器 
  2. timer := prometheus.NewTimer(requestDurations) 
  3.  
  4. // [...在应用中处理请求...] 
  5.  
  6. // 停止计时器并观察其持续时间,将其放进 requestDurations 的直方图指标中去 
  7. timer.ObserveDuration() 

直方图指标最终会生成如下所示的数据:

  1. # HELP http_request_duration_seconds A histogram of the HTTP request durations in seconds. 
  2. # TYPE http_request_duration_seconds histogram 
  3. http_request_duration_seconds_bucket{le="0.05"} 4599 
  4. http_request_duration_seconds_bucket{le="0.1"} 24128 
  5. http_request_duration_seconds_bucket{le="0.25"} 45311 
  6. http_request_duration_seconds_bucket{le="0.5"} 59983 
  7. http_request_duration_seconds_bucket{le="1"} 60345 
  8. http_request_duration_seconds_bucket{le="2.5"} 114003 
  9. http_request_duration_seconds_bucket{le="5"} 201325 
  10. http_request_duration_seconds_bucket{le="+Inf"} 227420 
  11. http_request_duration_seconds_sum 88364.234 
  12. http_request_duration_seconds_count 227420 

每个配置的存储桶最终作为一个带有 _bucket 后缀的计数器时间序列,使用 le(小于或等于) 标签指示该存储桶的上限,具有上限的隐式存储桶 +Inf 也暴露于比最大配置的存储桶边界花费更长的时间的请求,还包括使用后缀 _sum 累积总和和计数 _count 的指标,这些时间序列中的每一个在概念上都是一个 counter 计数器(只能上升的单个值),只是它们是作为直方图的一部分创建的。

Summaries

创建和使用摘要与直方图非常类似,只是我们需要指定要跟踪的 quantiles 分位数值,而不需要处理 bucket 桶,比如我们想要跟踪 HTTP 请求延迟的第 50、90 和 99 个百分位数,那么我们可以创建这样的一个摘要对象:

  1. requestDurations := prometheus.NewSummary(prometheus.SummaryOpts{ 
  2.     Name:       "http_request_duration_seconds"
  3.     Help:       "A summary of the HTTP request durations in seconds."
  4.     Objectives: map[float64]float64{ 
  5.       0.5: 0.05,   // 第50个百分位数,最大绝对误差为0.05。 
  6.       0.9: 0.01,   // 第90个百分位数,最大绝对误差为0.01。 
  7.       0.99: 0.001, // 第90个百分位数,最大绝对误差为0.001。 
  8.     }, 
  9.   }, 

这里和前面不一样的地方在于使用 prometheus.NewSummary() 函数初始化摘要指标对象的时候,需要通过 prometheus.SummaryOpts{} 对象的 Objectives 属性指定想要跟踪的分位数值。

同样摘要指标对象创建后,跟踪持续时间的方式和直方图是完全一样的,使用一个 Observe() 函数即可:

  1. requestDurations.Observe(0.42) 

虽然直方图桶可以跨维度汇总(如端点、HTTP 方法等),但这对于汇总 quantiles 分位数值来说在统计学上是无效的。例如,你不能对两个单独的服务实例的第 90 百分位延迟进行平均,并期望得到一个有效的整体第 90 百分位延迟。如果需要按维度进行汇总,那么我们需要使用直方图而不是摘要指标。

摘要指标最终生成的指标数据与直方图非常类似,不同之处在于使用 quantile 标签来表示分位数序列,并且这些序列没有扩展指标名称的后缀:

  1. # HELP http_request_duration_seconds A summary of the HTTP request durations in seconds. 
  2. # TYPE http_request_duration_seconds summary 
  3. http_request_duration_seconds{quantile="0.5"} 0.052 
  4. http_request_duration_seconds{quantile="0.90"} 0.564 
  5. http_request_duration_seconds{quantile="0.99"} 2.372 
  6. http_request_duration_seconds_sum 88364.234 
  7. http_request_duration_seconds_count 227420 

标签

到目前为止,我们还没有为指标对象添加任何的标签,要创建具有标签维度的指标,我们可以调用类似于 NewXXXVec() 的构造函数来初始化指标对象:

  • NewGauge() 变成 NewGaugeVec()
  • NewCounter() 变成 NewCounterVec()
  • NewSummary() 变成 NewSummaryVec()
  • NewHistogram() 变成 NewHistogramVec()

这些函数允许我们指定一个额外的字符串切片参数,提供标签名称的列表,通过它来拆分指标。

例如,为了按照房子以及测量温度的房间来划分我们早期的温度表指标,可以这样创建指标。

  1. temp := prometheus.NewGaugeVec( 
  2.   prometheus.GaugeOpts{ 
  3.     Name"home_temperature_celsius"
  4.     Help: "The current temperature in degrees Celsius."
  5.   }, 
  6.   // 两个标签名称,通过它们来分割指标。 
  7.   []string{"house""room"}, 

然后要访问一个特有标签的子指标,需要在设置其值之前,用 house 和 room 标签的各自数值,对产生的 gauge 向量调用 WithLabelValues() 方法来处理下:

  1. // 为 home=ydzs 和 room=living-room 设置指标值 
  2. temp.WithLabelValues("ydzs""living-room").Set(27) 

如果你喜欢在选择的子指标中明确提供标签名称,可以使用效率稍低的 With() 方法来代替:

  1. temp.With(prometheus.Labels{"house""ydzs""room""living-room"}).Set(66) 

不过需要注意如果向这两个方法传递不正确的标签数量或不正确的标签名称,这两个方法都会触发 panic。

下面是我们按照 house 和 room 标签维度区分指标的完整示例,创建一个名为 label-metric/main.go 的新文件,内容如下所示:

  1. package main 
  2.  
  3. import ( 
  4.  "net/http" 
  5.  
  6.  "github.com/prometheus/client_golang/prometheus" 
  7.  "github.com/prometheus/client_golang/prometheus/promhttp" 
  8.  
  9. func main() { 
  10.     // 创建带 house 和 room 标签的 gauge 指标对象 
  11.  temp := prometheus.NewGaugeVec( 
  12.   prometheus.GaugeOpts{ 
  13.    Name"home_temperature_celsius"
  14.    Help: "The current temperature in degrees Celsius."
  15.   }, 
  16.   // 指定标签名称 
  17.   []string{"house""room"}, 
  18.  ) 
  19.  
  20.  // 注册到全局默认注册表中 
  21.  prometheus.MustRegister(temp
  22.  
  23.  // 针对不同标签值设置不同的指标值 
  24.  temp.WithLabelValues("cnych""living-room").Set(27) 
  25.  temp.WithLabelValues("cnych""bedroom").Set(25.3) 
  26.  temp.WithLabelValues("ydzs""living-room").Set(24.5) 
  27.  temp.WithLabelValues("ydzs""bedroom").Set(27.7) 
  28.  
  29.  // 暴露自定义的指标 
  30.  http.Handle("/metrics", promhttp.Handler()) 
  31.  http.ListenAndServe(":8080", nil) 

上面代码非常清晰了,运行下面的程序:

  1. ☸ ➜ go run ./label-metric 

启动完成后重新访问指标端点 http://localhost:8080/metrics,可以找到 home_temperature_celsius 指标不同标签维度下面的指标值:

  1. ... 
  2. # HELP home_temperature_celsius The current temperature in degrees Celsius. 
  3. # TYPE home_temperature_celsius gauge 
  4. home_temperature_celsius{house="cnych",room="bedroom"} 25.3 
  5. home_temperature_celsius{house="cnych",room="living-room"} 27 
  6. home_temperature_celsius{house="ydzs",room="bedroom"} 27.7 
  7. home_temperature_celsius{house="ydzs",room="living-room"} 24.5 
  8. ... 

注意:当使用带有标签维度的指标时,任何标签组合的时间序列只有在该标签组合被访问过至少一次后才会出现在 /metrics 输出中,这对我们在 PromQL 查询的时候会产生一些问题,因为它希望某些时间序列一直存在,我们可以在程序第一次启动时,将所有重要的标签组合预先初始化为默认值。

同样的方式在其他几个指标类型中使用标签的方法与上面的方式一致。

 

责任编辑:姜华 来源: k8s技术圈
相关推荐

2023-11-06 01:39:02

Go语言开发

2022-07-08 08:00:31

Prometheus监控

2022-05-12 08:01:26

vmagentprometheus

2021-03-26 20:37:14

Prometheus监控指标

2023-02-23 08:00:27

PostgreSQPrometheus指标

2021-10-08 06:22:00

Prometheus 仪表化应用运维

2023-09-06 08:46:47

2023-12-29 08:01:52

自定义指标模板

2023-12-27 18:05:13

2012-07-30 09:41:04

Android搜索功能

2011-03-29 10:56:19

Cacti监控

2024-01-03 10:00:11

Prometheus指标Go

2020-12-30 08:09:46

运维Prometheus 监控

2022-05-18 08:32:05

服务监控Prometheus开源

2020-10-20 09:07:11

监控PrometheusZabbix

2022-05-19 08:21:02

vmalert监控

2022-07-11 09:36:38

SpringJava开发

2023-10-09 07:31:25

2022-11-08 00:00:00

监控系统Prometheus

2020-12-29 10:45:22

运维Prometheus-监控
点赞
收藏

51CTO技术栈公众号