【问题标题】:Multiple Endpoints for Prometheus in GoGo 中 Prometheus 的多个端点
【发布时间】:2017-10-09 12:55:00
【问题描述】:

我目前正在开发一个由 Prometheus 监控的用Go (golang) 编写的程序。

现在程序应该服务于两个端点/metrics/service。 当 Prometheus 在/metrics 上抓取时,它应该公开它自己的指标(例如发出的请求、请求延迟……),当在/service 上抓取时,它应该查询 API,从那里获取指标并将它们公开给 Prometheus .

对于我创建的第一部分,例如通过

的计数器
requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
    Namespace: "SERVICE",
    Subsystem: "service_metrics",
    Name:      "request_count",
    Help:      "Number of requests received.",
}, fieldKeys)

并通过以下方式提供服务:

http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8090", nil)

对于/service 部分,我查询API,提取值并通过Gauge.Set(value) 更新不同的仪表

  • 如何在不同的端点上公开最后一个仪表而不 启动另一台服务器(不同的端口)?
  • 我是否必须创建自己的收集器(我没有自定义指标,所以 不,对吧?)?

【问题讨论】:

标签: go microservices prometheus exporter


【解决方案1】:

您可以使用prometheus.NewRegistry 创建自定义收集器,并使用promhttp.HandlerFor 将其公开给您想要的某个端点。

var (
        // custom collector
        reg = prometheus.NewRegistry()
        // some metrics
        myGauge = prometheus.NewGaugeVec(
                prometheus.GaugeOpts{
                        Name: "gauge_name",
                        Help: "guage_help",
                },
                []string{"l"},
        )
)

func init() {
        // register metrics to my collector
        reg.MustRegister(myGauge)
}

func main() {
        // instrument
        myGauge.WithLabelValues("l").Set(123)

        // expose endpoint
        http.Handle("/service", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2023-01-28
    相关资源
    最近更新 更多