From 3dee6675d21f56e6042e4537f0eab3e5c29ef249 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Fri, 13 Dec 2024 14:00:12 +0800 Subject: [PATCH] metrics/exp: allow configuring metrics HTTP server on separate endpoint (#21290) --- internal/debug/flags.go | 10 +++++++--- metrics/exp/exp.go | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/debug/flags.go b/internal/debug/flags.go index 1e9970b73c..cf5b49e475 100644 --- a/internal/debug/flags.go +++ b/internal/debug/flags.go @@ -313,7 +313,9 @@ func Setup(ctx *cli.Context) error { // pprof server if ctx.Bool(pprofFlag.Name) { address := fmt.Sprintf("%s:%d", ctx.String(pprofAddrFlag.Name), ctx.Int(pprofPortFlag.Name)) - StartPProf(address) + // This context value ("metrics-addr") represents the utils.MetricsHTTPFlag.Name. + // It cannot be imported because it will cause a cyclical dependency. + StartPProf(address, !ctx.IsSet("metrics-addr") && !ctx.IsSet("metrics.addr")) } if len(logFile) > 0 || rotation { @@ -323,10 +325,12 @@ func Setup(ctx *cli.Context) error { return nil } -func StartPProf(address string) { +func StartPProf(address string, withMetrics bool) { // Hook go-metrics into expvar on any /debug/metrics request, load all vars // from the registry into expvar, and execute regular expvar handler. - exp.Exp(metrics.DefaultRegistry) + if withMetrics { + exp.Exp(metrics.DefaultRegistry) + } log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address)) go func() { if err := http.ListenAndServe(address, nil); err != nil { diff --git a/metrics/exp/exp.go b/metrics/exp/exp.go index 28435becea..622c2b5289 100644 --- a/metrics/exp/exp.go +++ b/metrics/exp/exp.go @@ -58,6 +58,7 @@ func ExpHandler(r metrics.Registry) http.Handler { func Setup(address string) { m := http.NewServeMux() m.Handle("/debug/metrics", ExpHandler(metrics.DefaultRegistry)) + m.Handle("/debug/metrics/prometheus", prometheus.Handler(metrics.DefaultRegistry)) log.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/debug/metrics", address)) go func() { if err := http.ListenAndServe(address, m); err != nil {