diff --git a/mymetric/prometheus.go b/mymetric/prometheus.go index 04e85b7..7bbd8aa 100644 --- a/mymetric/prometheus.go +++ b/mymetric/prometheus.go @@ -5,6 +5,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "log" + "net/http" "time" ) @@ -73,11 +74,24 @@ const ( ) // 监控指标路由 -func GinExport(engine *gin.Engine) { - engine.GET(MetricsRoute, gin.WrapH(promhttp.HandlerFor( +func GinExport(authKey string) gin.HandlerFunc { + pm := promhttp.HandlerFor( prometheus.DefaultGatherer, 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 {