一、前言
Prometheus 提供了 官方版 Golang 库 用于采集并暴露监控数据,本文快速为你介绍如何使用官方版 Golang 库来暴露 Golang runtime 相关的数据,以及其它一些基本简单的示例,并使用 Prometheus 监控服务来采集指标展示数据。
TIPS:
有关全面的 API 文档,请参见 Prometheus 的各种 Go 库的 GoDoc 文档。
二、安装
通过 go get
命令来安装相关依赖库,示例如下:
gogetgithub.com/prometheus/client_golang/prometheusgogetgithub.com/prometheus/client_golang/prometheus/promautogogetgithub.com/prometheus/client_golang/prometheus/promhttp
安装所需的 prometheus, promauto 和 promhttp 库。
$timegogetgithub.com/prometheus/client_golang/prometheusreal0m2.054suser0m0.000ssys0m0.015s$timegogetgithub.com/prometheus/client_golang/prometheus/promautoreal0m4.368suser0m0.000ssys0m0.015s$timegogetgithub.com/prometheus/client_golang/prometheus/promhttpreal0m3.109suser0m0.000ssys0m0.047s
三、快速开始(运行时指标)
准备一个 HTTP 服务,路径通常使用 /metrics。可以直接使用 prometheus/promhttp 里提供的 Handler 函数。 如下是一个简单的示例应用,通过 http://localhost:2112/metrics 暴露 Golang 应用的一些默认指标数据(包括运行时指标、进程相关指标以及构建相关的指标):
packagemainimport("net/http""github.com/prometheus/client_golang/prometheus/promhttp")funcmain(){//提供/metricsHTTP端点http.Handle("/metrics",promhttp.Handler())//端口号http.ListenAndServe(":2112",nil)}
执行以下命令启动应用:
gorunmain.go
执行以下命令,访问基础内置指标数据:
curlhttp://localhost:2112/metrics
#HELPgo_gc_duration_secondsAsummaryofthepausedurationofgarbagecollectioncycles.#TYPEgo_gc_duration_secondssummarygo_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_sum0go_gc_duration_seconds_count0#HELPgo_goroutinesNumberofgoroutinesthatcurrentlyexist.#TYPEgo_goroutinesgaugego_goroutines7#HELPgo_infoInformationabouttheGoenvironment.#TYPEgo_infogaugego_info{version="go1.16.7"}1#HELPgo_memstats_alloc_bytesNumberofbytesallocatedandstillinuse.#TYPEgo_memstats_alloc_bytesgaugego_memstats_alloc_bytes611920#HELPgo_memstats_alloc_bytes_totalTotalnumberofbytesallocated,eveniffreed.#TYPEgo_memstats_alloc_bytes_totalcountergo_memstats_alloc_bytes_total611920#HELPgo_memstats_buck_hash_sys_bytesNumberofbytesusedbytheprofilingbuckethashtable.#TYPEgo_memstats_buck_hash_sys_bytesgaugego_memstats_buck_hash_sys_bytes4081#HELPgo_memstats_frees_totalTotalnumberoffrees.#TYPEgo_memstats_frees_totalcountergo_memstats_frees_total112#HELPgo_memstats_gc_cpu_fractionThefractionofthisprogram'savailableCPUtimeusedbytheGCsincetheprogramstarted.#TYPEgo_memstats_gc_cpu_fractiongaugego_memstats_gc_cpu_fraction0#HELPgo_memstats_gc_sys_bytesNumberofbytesusedforgarbagecollectionsystemmetadata.#TYPEgo_memstats_gc_sys_bytesgaugego_memstats_gc_sys_bytes3.962144e+06#HELPgo_memstats_heap_alloc_bytesNumberofheapbytesallocatedandstillinuse.#TYPEgo_memstats_heap_alloc_bytesgaugego_memstats_heap_alloc_bytes611920#HELPgo_memstats_heap_idle_bytesNumberofheapbyteswaitingtobeused.#TYPEgo_memstats_heap_idle_bytesgaugego_memstats_heap_idle_bytes6.5273856e+07#HELPgo_memstats_heap_inuse_bytesNumberofheapbytesthatareinuse.#TYPEgo_memstats_heap_inuse_bytesgaugego_memstats_heap_inuse_bytes1.47456e+06#HELPgo_memstats_heap_objectsNumberofallocatedobjects.#TYPEgo_memstats_heap_objectsgaugego_memstats_heap_objects2296#HELPgo_memstats_heap_released_bytesNumberofheapbytesreleasedtoOS.#TYPEgo_memstats_heap_released_bytesgaugego_memstats_heap_released_bytes6.5273856e+07#HELPgo_memstats_heap_sys_bytesNumberofheapbytesobtainedfromsystem.#TYPEgo_memstats_heap_sys_bytesgaugego_memstats_heap_sys_bytes6.6748416e+07#HELPgo_memstats_last_gc_time_secondsNumberofsecondssince1970oflastgarbagecollection.#TYPEgo_memstats_last_gc_time_secondsgaugego_memstats_last_gc_time_seconds0#HELPgo_memstats_lookups_totalTotalnumberofpointerlookups.#TYPEgo_memstats_lookups_totalcountergo_memstats_lookups_total0#HELPgo_memstats_mallocs_totalTotalnumberofmallocs.#TYPEgo_memstats_mallocs_totalcountergo_memstats_mallocs_total2408#HELPgo_memstats_mcache_inuse_bytesNumberofbytesinusebymcachestructures.#TYPEgo_memstats_mcache_inuse_bytesgaugego_memstats_mcache_inuse_bytes14400#HELPgo_memstats_mcache_sys_bytesNumberofbytesusedformcachestructuresobtainedfromsystem.#TYPEgo_memstats_mcache_sys_bytesgaugego_memstats_mcache_sys_bytes16384#HELPgo_memstats_mspan_inuse_bytesNumberofbytesinusebymspanstructures.#TYPEgo_memstats_mspan_inuse_bytesgaugego_memstats_mspan_inuse_bytes37672#HELPgo_memstats_mspan_sys_bytesNumberofbytesusedformspanstructuresobtainedfromsystem.#TYPEgo_memstats_mspan_sys_bytesgaugego_memstats_mspan_sys_bytes49152#HELPgo_memstats_next_gc_bytesNumberofheapbyteswhennextgarbagecollectionwilltakeplace.#TYPEgo_memstats_next_gc_bytesgaugego_memstats_next_gc_bytes4.473924e+06#HELPgo_memstats_other_sys_bytesNumberofbytesusedforothersystemallocations.#TYPEgo_memstats_other_sys_bytesgaugego_memstats_other_sys_bytes1.033983e+06#HELPgo_memstats_stack_inuse_bytesNumberofbytesinusebythestackallocator.#TYPEgo_memstats_stack_inuse_bytesgaugego_memstats_stack_inuse_bytes360448#HELPgo_memstats_stack_sys_bytesNumberofbytesobtainedfromsystemforstackallocator.#TYPEgo_memstats_stack_sys_bytesgaugego_memstats_stack_sys_bytes360448#HELPgo_memstats_sys_bytesNumberofbytesobtainedfromsystem.#TYPEgo_memstats_sys_bytesgaugego_memstats_sys_bytes7.2174608e+07#HELPgo_threadsNumberofOSthreadscreated.#TYPEgo_threadsgaugego_threads7#HELPpromhttp_metric_handler_requests_in_flightCurrentnumberofscrapesbeingserved.#TYPEpromhttp_metric_handler_requests_in_flightgaugepromhttp_metric_handler_requests_in_flight1#HELPpromhttp_metric_handler_requests_totalTotalnumberofscrapesbyHTTPstatuscode.#TYPEpromhttp_metric_handler_requests_totalcounterpromhttp_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。
packagemainimport("net/http""time""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/promauto""github.com/prometheus/client_golang/prometheus/promhttp")funcrecordMetrics(){//每2秒,计数器增加1。gofunc(){for{opsProcessed.Inc()time.Sleep(2*time.Second)}}()}//公开了myapp_processed_ops_total计数器var(opsProcessed=promauto.NewCounter(prometheus.CounterOpts{Name:"myapp_processed_ops_total",Help:"Thetotalnumberofprocessedevents",}))funcmain(){recordMetrics()http.Handle("/metrics",promhttp.Handler())http.ListenAndServe(":2112",nil)}
执行以下命令启动应用:
gorunmain.go
执行以下命令,访问暴露的指标:
curlhttp://localhost:2112/metrics
从输出结果我们可以看到 myapp_processed_ops_total 计数器相关的信息,包括帮助文本、类型信息、指标名和当前值。
如下所示:
......#HELPmyapp_processed_ops_totalThetotalnumberofprocessedevents#TYPEmyapp_processed_ops_totalcountermyapp_processed_ops_total4......
我们可以配置一个 Prometheus 实例,从应用程序中获取指标。这是一个 prometheus.yml 配置示例:
$timegogetgithub.com/prometheus/client_golang/prometheusreal0m2.054suser0m0.000ssys0m0.015s$timegogetgithub.com/prometheus/client_golang/prometheus/promautoreal0m4.368suser0m0.000ssys0m0.015s$timegogetgithub.com/prometheus/client_golang/prometheus/promhttpreal0m3.109suser0m0.000ssys0m0.047s0
五、总结
本文通过两个示例展示了如何将 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/