写点什么

性能监控之 Golang 应用接入 Prometheus 监控

作者:zuozewei
  • 2021 年 12 月 24 日
  • 本文字数:4227 字

    阅读完需:约 14 分钟

性能监控之 Golang 应用接入 Prometheus 监控

一、前言

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


TIPS:


有关全面的 API 文档,请参见 Prometheus 的各种 Go 库的 GoDoc 文档。

二、安装

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


go get github.com/prometheus/client_golang/prometheusgo get github.com/prometheus/client_golang/prometheus/promautogo get github.com/prometheus/client_golang/prometheus/promhttp
复制代码


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


$ time go get github.com/prometheus/client_golang/prometheus
real 0m2.054suser 0m0.000ssys 0m0.015s
$ time go get github.com/prometheus/client_golang/prometheus/promauto
real 0m4.368suser 0m0.000ssys 0m0.015s
$ time go get github.com/prometheus/client_golang/prometheus/promhttp
real 0m3.109suser 0m0.000ssys 0m0.047s
复制代码

三、快速开始(运行时指标)

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


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)}
复制代码


执行以下命令启动应用:


go run main.go
复制代码


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


curl http://localhost:2112/metrics
复制代码


# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.# TYPE go_gc_duration_seconds summarygo_gc_duration_seconds{quantile="0"} 0go_gc_duration_seconds{quantile="0.25"} 0go_gc_duration_seconds{quantile="0.5"} 0go_gc_duration_seconds{quantile="0.75"} 0go_gc_duration_seconds{quantile="1"} 0go_gc_duration_seconds_sum 0go_gc_duration_seconds_count 0# HELP go_goroutines Number of goroutines that currently exist.# TYPE go_goroutines gaugego_goroutines 7# HELP go_info Information about the Go environment.# TYPE go_info gaugego_info{version="go1.16.7"} 1# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.# TYPE go_memstats_alloc_bytes gaugego_memstats_alloc_bytes 611920# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.# TYPE go_memstats_alloc_bytes_total countergo_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 gaugego_memstats_buck_hash_sys_bytes 4081# HELP go_memstats_frees_total Total number of frees.# TYPE go_memstats_frees_total countergo_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 gaugego_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 gaugego_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 gaugego_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 gaugego_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 gaugego_memstats_heap_inuse_bytes 1.47456e+06# HELP go_memstats_heap_objects Number of allocated objects.# TYPE go_memstats_heap_objects gaugego_memstats_heap_objects 2296# HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.# TYPE go_memstats_heap_released_bytes gaugego_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 gaugego_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 gaugego_memstats_last_gc_time_seconds 0# HELP go_memstats_lookups_total Total number of pointer lookups.# TYPE go_memstats_lookups_total countergo_memstats_lookups_total 0# HELP go_memstats_mallocs_total Total number of mallocs.# TYPE go_memstats_mallocs_total countergo_memstats_mallocs_total 2408# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.# TYPE go_memstats_mcache_inuse_bytes gaugego_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 gaugego_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 gaugego_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 gaugego_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 gaugego_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 gaugego_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 gaugego_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 gaugego_memstats_stack_sys_bytes 360448# HELP go_memstats_sys_bytes Number of bytes obtained from system.# TYPE go_memstats_sys_bytes gaugego_memstats_sys_bytes 7.2174608e+07# HELP go_threads Number of OS threads created.# TYPE go_threads gaugego_threads 7# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.# TYPE promhttp_metric_handler_requests_in_flight gaugepromhttp_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 counterpromhttp_metric_handler_requests_total{code="200"} 0promhttp_metric_handler_requests_total{code="500"} 0promhttp_metric_handler_requests_total{code="503"} 0
复制代码

四、应用层面指标

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


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) } }()}
//公开了 myapp_processed_ops_total 计数器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)}
复制代码


执行以下命令启动应用:


go run main.go
复制代码


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


curl http://localhost:2112/metrics
复制代码


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


如下所示:


......# HELP myapp_processed_ops_total The total number of processed events# TYPE myapp_processed_ops_total countermyapp_processed_ops_total 4......
复制代码


我们可以配置一个 Prometheus 实例,从应用程序中获取指标。这是一个 prometheus.yml 配置示例:


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


五、总结

本文通过两个示例展示了如何将 Golang 相关的指标暴露给 Prometheus 监控服务,以及如何使用内置的可视化的图表查看监控数据。


本文只使用了计数类型 Counter 的指标,对于其它场景可能还需要 Gauge,Histgram 以及 Summary 类型的指标,指标类型。​


源码地址:



参考资料:


发布于: 1 小时前
用户头像

zuozewei

关注

测试及性能领域创作者 | 公众号@7DGroup 2017.12.23 加入

「7DGroup」技术公众号作者,CSDN博客专家、测试领域优质创作者,华为云·云享专家,极客时间《全链路压测实战30讲》专栏作者之一,极客时间《性能测试实战30讲》、《高楼的性能工程实战课》专栏编委。

评论

发布
暂无评论
性能监控之 Golang 应用接入 Prometheus 监控