Files
xlgo-core/router/metrics.go
T
杭州明婳科技 0f13292a46 GLM 5.2修改版
2026-07-02 18:26:27 +08:00

27 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// RegisterMetricsRoute 注册 Prometheus 指标暴露端点(#18H8c 修正)。
//
// 默认路径 /metrics。传入 path 可自定义。本函数仅注册暴露端点,不再用 r.Use
// 挂采集中间件——采集中间件改由 Registry.SetMetricsMiddleware 在 Apply 内作为
// 首个全局中间件装入,使所有经注册中心注册的业务路由都被采集,且不依赖本函数
// 相对其他路由注册的调用顺序。/metrics 端点自身与 /health 等基础路由不经采集中间件。
//
// 用法:
//
// router.RegisterMetricsRoute(r) // /metrics
// router.RegisterMetricsRoute(r, "/metrics") // 等价
func RegisterMetricsRoute(r *gin.Engine, path ...string) {
p := "/metrics"
if len(path) > 0 && path[0] != "" {
p = path[0]
}
// 幂等注册(H8d 收尾):重复调用跳过,避免重复路由 panic。
registerGETOnce(r, p, gin.WrapH(promhttp.Handler()))
}