首页 > 其他分享 >Golang 应用接入 Prometheus 监控

Golang 应用接入 Prometheus 监控

时间:2023-04-06 13:03:00浏览次数:46  
标签:HELP 接入 bytes prometheus Golang memstats Prometheus go TYPE


前言

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 命令来安装相关依赖库,示例如下:

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 库。

$ 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 应用的一些默认指标数据(包括运行时指标、进程相关指标以及构建相关的指标):

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 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。

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)
}

执行以下命令启动应用:

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 counter
myapp_processed_ops_total 4
......

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

prometheus.yml 配置示例:

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - 172.16.106.251:2112

Golang 应用接入 Prometheus 监控_github

总结

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

源码地址:


参考资料:

  • [1]:https://prometheus.io/docs/guides/go-application/


标签:HELP,接入,bytes,prometheus,Golang,memstats,Prometheus,go,TYPE
From: https://blog.51cto.com/u_15181572/6172673

相关文章

  • golang开发需要掌握的核心包以及中间件,涵盖项目的各个领域,值得收藏
    golang开发需要掌握的核心包以及中间件,涵盖项目的各个领域,值得收藏。常用包常用包 说明fmt 实现格式化的输入输出操作,其中的fmt.Printf()和fmt.Println()是开发者使用最为频繁的函数。io 实现了一系列非平台相关的IO相关接口和实现,比如提供了对os中系统相关的IO功能的封装。我们......
  • Golang基础-Select
    基本概念select是Go中的一个控制结构,类似于switch语句。select语句只能用于通道(channel)操作,每个case必须是一个通道操作,要么是发送要么是接收。select语句会监听所有指定的通道上的操作,一旦其中一个通道准备好就会执行相应的代码块。如果多个通道都准备好,那么sel......
  • PrometheusAlert微信报警模板示例
    配置入口如下: 代码配置:{{$var:=.externalURL}}{{range$k,$v:=.alerts}}{{ifeq$v.status"resolved"}}[PROMETHEUS-恢复信息]({{$v.generatorURL}})>**[{{$v.labels.alertname}}]({{$var}})**><fontcolor="info">开......
  • Golang入门教程(一)GOPATH与工作空间(Windows)
    https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.2.md Windows环境:下面我就以mymath为例来讲述如何编写应用包:cd$GOPATH/srcmkdirmymath//我的环境:$GOPATH=c:\mygo新建文件sqrt.go,内容如下://$GOPATH/src/mymath/sqrt.go源码如......
  • 如何在ubuntu22下安装docker版的golang来编译go语言写的代码
    为了让我们的ubuntu22系统更干净清爽我们使用docker首先使用snapinstalldocker安装docker后即可使用docker了docker命令的使用方法1:将你的代码下载到用户目录(~)下面(例如~/github/xixi/...)2:使用cd命令进到你代码需要运行gobuild的文件夹3:然后使用下面命令即可给你的代码编译......
  • golang CVE-2016-2183漏洞,https需要添加tls设置加密算法CipherSuites白名单,将弱加密算
    golangCVE-2016-2183漏洞,https需要添加tls设置加密算法白名单,将弱加密算法DES和3DES去掉。服务端样例代码packagemainimport("crypto/tls""fmt""net/http")funchandler(writerhttp.ResponseWriter,request*http.Request){fmt.Fprintf(wri......
  • Golang基本语法笔记
    数据类型整型取值范围varnint8n=100fmt.Println(n)//100没有问题//如果赋值为200则不行因为int8取值范围最大是127字符串 v1:='A'v2:="A" //单引号存储的是ASCII编码//A的ASCII值=65//B的ASCII值B=66//a的ASCII值a=97 fmt.Printf("v1的类型是%T,%d,值为%s\n"......
  • Golang环境安装
    Go语言简介什么是Go语言Go语言是谷歌2009发布的第二款开源编程语言。Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。Go语言具有很强的表达能力,它简洁、清晰而高效。得益于其并发机制,用......
  • HCIP-ICT实战进阶12-接入安全技术介绍
    HCIP-ICT实战进阶12-接入安全技术介绍HCIP最后一篇理论博客了,这个搞完我再考虑要不要把系统集成也整一份博客,还是把HCIP实验的博客整理整理,这学期争取去国科那边接接项目吧.0前言在这篇博客中,我将介绍常见的以太网交换安全技术,包括端口隔离、端口安全、MAC地址表安......
  • LiveVISGAT1400视图库服务-支持海康大华华为宇视天地伟业等设备视图库接入使用说明
    @目录LiveVISGAT1400视图库服务安装使用说明1、服务说明1.1、安装包说明1.2、视图库服务1.3、配置视图库服务参数2、服务运行2.1、Windows2.2、Linux3、配置设备接入3.1、海康视图库接入示例3.2、大华视图库接入示例4、平台使用4.1、管理平台4.2、接口文档5、统一编码规则6、Live......