You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义Prometheus Exporter多端点共享指标问题求助

解决方案

核心思路

拆分Registry职责:用一个全局共享Registry存放跨端点通用指标(如promhttp_metric_handler_requests_total),每个业务端点使用独立Registry存储专属业务指标。请求处理时,合并共享Registry与当前业务Registry的指标返回;仅/metrics端点额外加入Go运行时指标(DefaultGatherer)。

具体实现步骤

1. 初始化共享Registry与通用指标

手动创建promhttp的通用指标并注册到全局共享Registry,替代默认的自动注册逻辑,确保所有端点复用同一指标实例:

package main

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

// 全局共享Registry,存储跨端点通用指标
var sharedRegistry = prometheus.NewRegistry()
var promhttpHandlerOpts promhttp.HandlerOpts

func init() {
	// 手动定义promhttp请求计数指标
	promhttpRequestsTotal := prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "promhttp_metric_handler_requests_total",
			Help: "Total number of scrapes by HTTP status code.",
		},
		[]string{"code", "handler"},
	)
	sharedRegistry.MustRegister(promhttpRequestsTotal)

	// 配置promhttp handler使用我们的共享指标
	promhttpHandlerOpts = promhttp.HandlerOpts{
		Registry: sharedRegistry,
		// 可按需添加ErrorLog等其他配置
	}
}

2. 为业务端点创建独立Registry

每个业务端点(如/metrics/access_points)创建专属Registry,仅注册该端点的业务Collector:

// access_points端点专属Registry
var accessPointsRegistry = prometheus.NewRegistry()

func init() {
	// 注册该端点的自定义采集器
	accessPointsRegistry.MustRegister(NewAccessPointsCollector())
}

3. 自定义Handler合并指标集

实现Gatherer合并逻辑,根据端点类型决定是否加入Go运行时指标:

import "net/http"

// 合并多个Gatherer的指标
func mergeGatherers(gatherers ...prometheus.Gatherer) prometheus.Gatherer {
	return prometheus.GathererFunc(func() ([]*prometheus.MetricFamily, error) {
		var allMetrics []*prometheus.MetricFamily
		for _, g := range gatherers {
			mfs, err := g.Gather()
			if err != nil {
				return nil, err
			}
			allMetrics = append(allMetrics, mfs...)
		}
		return allMetrics, nil
	})
}

func main() {
	// /metrics端点:共享指标 + Go运行时指标 + 默认业务指标(如有)
	http.Handle("/metrics", promhttp.HandlerFor(
		mergeGatherers(sharedRegistry, prometheus.DefaultGatherer),
		promhttpHandlerOpts,
	))

	// /metrics/access_points端点:共享指标 + 专属业务指标(无Go运行时指标)
	http.Handle("/metrics/access_points", promhttp.HandlerFor(
		mergeGatherers(sharedRegistry, accessPointsRegistry),
		promhttpHandlerOpts,
	))

	// 启动HTTP服务
	http.ListenAndServe(":9100", nil)
}

关键注意事项

  • 所有端点必须使用同一套promhttp通用指标实例,禁止在业务Registry中重复注册,避免计数分裂。
  • 合并Gatherer时需确保指标名称无冲突,业务指标与通用指标命名要区分开。
  • 若新增其他业务端点,只需复制access_points的模式:创建专属Registry + 合并共享Registry即可。

内容的提问来源于stack exchange,提问作者SlightlyCultured

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:33:17