main
lzf 2025-07-22 17:58:46 +08:00
parent 4ced8ae869
commit a545116f52
1 changed files with 17 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"log" "log"
"net/http"
"time" "time"
) )
@ -73,11 +74,24 @@ const (
) )
// 监控指标路由 // 监控指标路由
func GinExport(engine *gin.Engine) { func GinExport(authKey string) gin.HandlerFunc {
engine.GET(MetricsRoute, gin.WrapH(promhttp.HandlerFor( pm := promhttp.HandlerFor(
prometheus.DefaultGatherer, prometheus.DefaultGatherer,
promhttp.HandlerOpts{Timeout: time.Second * 3}, promhttp.HandlerOpts{Timeout: time.Second * 3},
))) )
return func(ctx *gin.Context) {
if authKey != "" {
value := ctx.Query("key")
if authKey != value {
ctx.String(http.StatusForbidden, "metric auth key is empty")
return
}
}
pm.ServeHTTP(ctx.Writer, ctx.Request)
}
} }
func HistogramKey(name string) string { func HistogramKey(name string) string {