使用github.com/prometheus/client_golang/prometheus包的注意事项

1.panic: duplicate metrics collector registration attempted

重复注册。

错误代码如下

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

但是这样是可以的(value不一样)

promauto.NewCounter(prometheus.CounterOpts{

        Name:        "name",

        ConstLabels: prometheus.Labels{"k1""v1"},

    })

    promauto.NewCounter(prometheus.CounterOpts{

        Name:        "name",

        ConstLabels: prometheus.Labels{"k1""v2"},

    })

2.panic: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "name", help: "", constLabels: {k2="v2"}, variableLabels: []} has different label names or a different help string

name一样但是labels的key不一样也会导致报错。

特别注意,label的key必须是完全一样

错误示例:

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k2""v2"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v2"},

})

当name一样,help不一样也会导致报错

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    Help:        "help",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    Help:        "help1",

    ConstLabels: prometheus.Labels{"k1""v2"},

})

正确示例:

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v2"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v22"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v2"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v11""k2""v22"},

})

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Prometheus Client 是一个用于监控和度量的开源工具集,而 Prometheus Client GolangPrometheus 官方提供的 Golang 版本的客户端库。允许 Golang 应用程序暴露指标(metrics)并将其暴露给 Prometheus 服务器进行收集和分析。 使用 Prometheus Client Golang,你可以在你的 Golang 应用程序中定义和注册自定义指标,并且通过 HTTP 接口将指标暴露给 Prometheus 服务器。这样,你就可以使用 Prometheus 的强大功能来监控和可视化你的应用程序的性能指标、错误率、资源使用情况等。 要使用 Prometheus Client Golang,你需要导入 `github.com/prometheus/client_golang/prometheus` 使用其中的函数和结构体来定义和注册指标。然后,在你的应用程序中,可以通过适当的接口将指标暴露给 Prometheus 服务器。 以下是一个简单的示例,展示了如何在 Golang 应用程序中使用 Prometheus Client Golang: ```go package main import ( "net/http" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { // 创建一个新的 Counter 指标 counter := prometheus.NewCounter(prometheus.CounterOpts{ Name: "my_counter", Help: "A simple counter", }) // 注册指标 prometheus.MustRegister(counter) // 增加指标值 counter.Inc() // 创建一个 HTTP 处理程序来暴露指标 http.Handle("/metrics", promhttp.Handler()) // 启动 HTTP 服务器 http.ListenAndServe(":8080", nil) } ``` 在上面的示例中,我们首先创建了一个名为 `my_counter` 的 Counter 指标。然后,我们注册这个指标,并通过 `Inc()` 方法增加其值。接下来,我们创建了一个 HTTP 处理程序来暴露指标,并将其绑定到 `/metrics` 路径上。最后,我们启动了一个 HTTP 服务器来监听端口 8080,并通过该端口暴露指标给 Prometheus。 通过运行上面的代码,你可以在浏览器中访问 `http://localhost:8080/metrics` 查看 Prometheus 格式的指标数据。这些数据可以被 Prometheus 服务器抓取并进行监控和分析。 希望这个简单示例能帮助你了解如何在 Golang 应用程序中使用 Prometheus Client Golang。有关更多详细信息和更高级的用法,请参考 Prometheus Client Golang 的官方文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值