前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang 应用接入 Prometheus 监控

Golang 应用接入 Prometheus 监控

作者头像
高楼Zee
发布2021-09-23 17:06:32
2.5K0
发布2021-09-23 17:06:32
举报
文章被收录于专栏:7DGroup7DGroup

前言

Prometheus 提供了官方版 Golang 库(https://github.com/prometheus/client_golang) 用于采集并暴露监控数据,本文快速为你介绍如何使用官方版 Golang 库来暴露 Golang runtime 相关的数据,以及其它一些基本简单的示例,并使用 Prometheus 监控服务来采集指标展示数据。

❝有关全面的 API 文档,请参见 Prometheus 的各种 Go 库的 GoDoc (https://godoc.org/github.com/prometheus/client_golang)文档。 ❞

安装

通过 go get 命令来安装相关依赖库,示例如下:

代码语言:javascript
复制
go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promauto
go get github.com/prometheus/client_golang/prometheus/promhttp

安装所需的 prometheus, promauto 和 promhttp 库。

代码语言:javascript
复制
$ time go get github.com/prometheus/client_golang/prometheus

real    0m2.054s
user    0m0.000s
sys     0m0.015s

$ time go get github.com/prometheus/client_golang/prometheus/promauto

real    0m4.368s
user    0m0.000s
sys     0m0.015s

$ time go get github.com/prometheus/client_golang/prometheus/promhttp

real    0m3.109s
user    0m0.000s
sys     0m0.047s

快速开始(运行时指标)

准备一个 HTTP 服务,路径通常使用 /metrics。可以直接使用 prometheus/promhttp 里提供的 Handler 函数。如下是一个简单的示例应用,通过 http://localhost:2112/metrics 暴露 Golang 应用的一些默认指标数据(包括运行时指标、进程相关指标以及构建相关的指标):

代码语言:javascript
复制
package main

import (
 "net/http"

 "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
 //提供 /metrics HTTP 端点
 http.Handle("/metrics", promhttp.Handler())
 //端口号
 http.ListenAndServe(":2112", nil)
}

执行以下命令启动应用:

代码语言:javascript
复制
go run main.go

执行以下命令,访问基础内置指标数据:

代码语言:javascript
复制
curl http://localhost:2112/metrics
代码语言:javascript
复制
# 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"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.16.7"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 611920
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 611920
# 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 4081
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 112
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 0
# 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 3.962144e+06
# HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 611920
# HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE go_memstats_heap_idle_bytes gauge
go_memstats_heap_idle_bytes 6.5273856e+07
# HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE go_memstats_heap_inuse_bytes gauge
go_memstats_heap_inuse_bytes 1.47456e+06
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 2296
# HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
# TYPE go_memstats_heap_released_bytes gauge
go_memstats_heap_released_bytes 6.5273856e+07
# HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE go_memstats_heap_sys_bytes gauge
go_memstats_heap_sys_bytes 6.6748416e+07
# HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 0
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 2408
# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 14400
# HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 16384
# HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 37672
# HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 49152
# HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE go_memstats_next_gc_bytes gauge
go_memstats_next_gc_bytes 4.473924e+06
# HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE go_memstats_other_sys_bytes gauge
go_memstats_other_sys_bytes 1.033983e+06
# HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 360448
# HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 360448
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 7.2174608e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 7
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 0
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

应用层面指标

应用程序仅公开默认的 Go 指标。我们还可以注册自定义应用程序指定指标。如下示例应用程序公开了 myapp_processed_ops_total 计数器(/prometheus/concepts/metric_types#counter),该计数器对到目前为止已处理的操作数量进行计数。每2秒,计数器增加1。

代码语言:javascript
复制
package main

import (
 "net/http"
 "time"

 "github.com/prometheus/client_golang/prometheus"
 "github.com/prometheus/client_golang/prometheus/promauto"
 "github.com/prometheus/client_golang/prometheus/promhttp"
)

func recordMetrics() {
 //每2秒,计数器增加1。
 go func() {
  for {
   opsProcessed.Inc()
   time.Sleep(2 * time.Second)
  }
 }()
}

var (
 opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
  Name: "myapp_processed_ops_total",
  Help: "The total number of processed events",
 })
)

func main() {
 recordMetrics()

 http.Handle("/metrics", promhttp.Handler())
 http.ListenAndServe(":2112", nil)
}

执行以下命令启动应用:

代码语言:javascript
复制
go run main.go

执行以下命令,访问暴露的指标:

代码语言:javascript
复制
curl http://localhost:2112/metrics

从输出结果我们可以看到 myapp_processed_ops_total 计数器相关的信息,包括帮助文本、类型信息、指标名和当前值。

如下所示:

代码语言:javascript
复制
......
# HELP myapp_processed_ops_total The total number of processed events
# TYPE myapp_processed_ops_total counter
myapp_processed_ops_total 4
......

我们可以配置(/prometheus/prometheus/configuration/configuration#scrape_config) 一个 Prometheus 实例,从应用程序中获取指标。这是一个

prometheus.yml 配置示例:

代码语言:javascript
复制
scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - 172.16.106.251:2112

总结

本文通过两个示例展示了如何将 Golang 相关的指标暴露给 Prometheus 监控服务。本文只使用了计数类型 Counter 的指标,对于其它场景可能还需要 Gauge,Histgram 以及 Summary 类型的指标类型。

源码地址:

  • https://github.com/zuozewei/blog-example/tree/master/Performance-testing/04-full-link/go_prometheus

参考资料:

  • [1]:https://prometheus.io/docs/guides/go-application/
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 7DGroup 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 安装
    • 快速开始(运行时指标)
      • 应用层面指标
        • 总结
        相关产品与服务
        Prometheus 监控服务
        Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档