脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Golang - prometheus client_go为应用程序自定义监控指标

prometheus client_go为应用程序自定义监控指标

2023-02-16 12:14a朋 Golang

这篇文章主要为大家介绍了prometheus client_go为应用程序自定义监控指标详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

使用prometheus client_go为应用程序添加监控指标

使用prometheus client_go为应用程序添加监控指标时,通常为http注册一个client_go默认的handler,这样就可以通过/metrics接口,拉取应用程序的metrics指标了:

?
1
http.Handle("/metrics", promhttp.Handler())

但是,该默认的handler会自动引入go的指标和proc的指标:

go的指标:

?
1
2
3
4
go_gc_duration_seconds
go_goroutines
go_info
....

proc的指标

?
1
2
3
process_start_time_seconds
process_cpu_seconds_total
....

默认handler为啥会引入go指标和proc指标,如果不需要要,可以去掉它们吗?

原因

从源码中找原因,http handler:

?
1
http.Handle("/metrics", promhttp.Handler())

client_go中该handler的实现:

?
1
2
3
4
5
6
// prometheus/client_golang/prometheus/promhttp/http.go
func Handler() http.Handler {
    return InstrumentMetricHandler(
        prometheus.DefaultRegisterer, HandlerFor(prometheus.DefaultGatherer, HandlerOpts{}),
    )
}

其中DefaultRegister、DefaultGather指向同一个Registry对象,即defaultRegistry

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// prometheus/client_golang/prometheus/registry.go
var (
    defaultRegistry              = NewRegistry()
    DefaultRegisterer Registerer = defaultRegistry
    DefaultGatherer   Gatherer   = defaultRegistry
)
func init() {
    MustRegister(NewProcessCollector(ProcessCollectorOpts{}))    // 采集Proc指标
    MustRegister(NewGoCollector())                                // 采集Go指标
}
func MustRegister(cs ...Collector) {
    DefaultRegisterer.MustRegister(cs...)
}

该Registry对象在init()中,被注入了:

  • NewProcessCollector:采集进程的指标信息;
  • NewGoCollector: 采集go runtime的指标信息;

去掉Proc和Go指标

在实现自己的exporter或为应用程序添加指标时,若不需要Proc/Go指标,可以:

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

其中:

prometheus.NewRegistry()创建自己的注册表(不使用defaultRegistry);

registry.MustRegister():

  • 若添加了ProcessCollector,会自动添加process_*监控指标;
  • 若添加了GoCollector,会自动添加go_*监控指标;
  • promhttp.HandlerFor创建针对registry的http handler;
  • promhttp.HandlerOpts{Registry: registry}: 将添加promhttp_*相关的指标;

参考: https://github.com/prometheus...

以上就是prometheus client_go为应用程序自定义监控指标的详细内容,更多关于prometheus client_go监控指标的资料请关注服务器之家其它相关文章!

原文链接:https://segmentfault.com/a/1190000043419656

延伸 · 阅读

精彩推荐
  • GolangGolang template 包基本原理分析

    Golang template 包基本原理分析

    这篇文章主要为大家介绍了Golang template 包基本原理分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    大大怪8712022-11-14
  • Golang深入Golang中的sync.Pool详解

    深入Golang中的sync.Pool详解

    这篇文章主要介绍了深入Golang中的sync.Pool详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    sunsky30312042021-05-27
  • GolangUbuntu安装Go语言运行环境

    Ubuntu安装Go语言运行环境

    由于最近偏爱Ubuntu,在加上作为一门开源语言,在Linux上从源代码开始搭建环境更让人觉得有趣味性。让我们直接先从Go语言的环境搭建开始 ...

    脚本之家3942020-04-26
  • GolangGo语言快速入门图文教程

    Go语言快速入门图文教程

    Go是 Goolge 开发的一种静态型、编译型、并发型,并具有垃圾回收功能的语言,Go 语言上手非常容易,它的风格类似于 C 语言,Go 语言号称是互联网时代的...

    异常编程4022021-06-26
  • GolangGo语言如何并发超时处理详解

    Go语言如何并发超时处理详解

    大家都知道golang并没有在语言层次上提供超时操作,但可以通过一些小技巧实现超时。下面来一起看看吧,有需要的朋友们可以参考借鉴。 ...

    daisy3752020-05-03
  • GolangGo语言基础切片的创建及初始化示例详解

    Go语言基础切片的创建及初始化示例详解

    这篇文章主要为大家介绍了Go语言基础切片的创建及初始化示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    枫少文10772021-12-07
  • Golanggoland把go项目打包进docker镜像的全过程记录

    goland把go项目打包进docker镜像的全过程记录

    golang编译的应用是不需要依赖其他运行环境的,下面这篇文章主要给大家介绍了关于goland把go项目打包进docker镜像的相关资料,文中通过图文介绍的非常详细...

    年少轻与狂11302022-08-10
  • GolangGo语言struct类型详解

    Go语言struct类型详解

    这篇文章主要介绍了Go语言struct类型详解,struct是一种数据类型,可以用来定义自己想的数据类型,需要的朋友可以参考下 ...

    junjie6012020-04-09