From 33d7a282d9ad5c46a62330a932bea626a9fd4401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Mon, 12 Aug 2024 14:28:25 +0800 Subject: [PATCH] serve pprof in metrics server if metrics server listens on 0.0.0.0 (#4384) Signed-off-by: zhangzujian --- cmd/controller/controller.go | 8 +++++--- cmd/daemon/cniserver.go | 5 +++-- cmd/ovn_monitor/ovn_monitor.go | 2 +- cmd/pinger/pinger.go | 2 +- pkg/metrics/server.go | 13 ++++++++++++- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/controller/controller.go b/cmd/controller/controller.go index 050d16047cc..8849f550bd9 100644 --- a/cmd/controller/controller.go +++ b/cmd/controller/controller.go @@ -53,7 +53,9 @@ func CmdMain() { ctrl.SetLogger(klog.NewKlogr()) ctx := signals.SetupSignalHandler() go func() { - if config.EnablePprof { + metricsAddr := util.GetDefaultListenAddr() + servePprofInMetricsServer := config.EnableMetrics && metricsAddr == "0.0.0.0" + if config.EnablePprof && !servePprofInMetricsServer { mux := http.NewServeMux() mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) @@ -87,8 +89,8 @@ func CmdMain() { } metrics.InitKlogMetrics() metrics.InitClientGoMetrics() - addr := util.JoinHostPort(util.GetDefaultListenAddr(), config.PprofPort) - if err := metrics.Run(ctx, config.KubeRestConfig, addr, config.SecureServing); err != nil { + addr := util.JoinHostPort(metricsAddr, config.PprofPort) + if err := metrics.Run(ctx, config.KubeRestConfig, addr, config.SecureServing, servePprofInMetricsServer); err != nil { util.LogFatalAndExit(err, "failed to run metrics server") } <-ctx.Done() diff --git a/cmd/daemon/cniserver.go b/cmd/daemon/cniserver.go index 26b0068ee04..f84389c5155 100644 --- a/cmd/daemon/cniserver.go +++ b/cmd/daemon/cniserver.go @@ -113,7 +113,8 @@ func main() { }() } - if config.EnablePprof { + servePprofInMetricsServer := config.EnableMetrics && addr == "0.0.0.0" + if config.EnablePprof && !servePprofInMetricsServer { mux := http.NewServeMux() mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) @@ -143,7 +144,7 @@ func main() { } listenAddr := util.JoinHostPort(addr, config.PprofPort) - if err = metrics.Run(ctx, nil, listenAddr, config.SecureServing); err != nil { + if err = metrics.Run(ctx, nil, listenAddr, config.SecureServing, servePprofInMetricsServer); err != nil { util.LogFatalAndExit(err, "failed to run metrics server") } <-stopCh diff --git a/cmd/ovn_monitor/ovn_monitor.go b/cmd/ovn_monitor/ovn_monitor.go index fd7f6032266..7a387d4a424 100644 --- a/cmd/ovn_monitor/ovn_monitor.go +++ b/cmd/ovn_monitor/ovn_monitor.go @@ -46,7 +46,7 @@ func CmdMain() { ctrl.SetLogger(klog.NewKlogr()) ctx := signals.SetupSignalHandler() - if err = metrics.Run(ctx, nil, addr, config.SecureServing); err != nil { + if err = metrics.Run(ctx, nil, addr, config.SecureServing, false); err != nil { util.LogFatalAndExit(err, "failed to run metrics server") } <-ctx.Done() diff --git a/cmd/pinger/pinger.go b/cmd/pinger/pinger.go index 6a9f3d37deb..0758b1e03db 100644 --- a/cmd/pinger/pinger.go +++ b/cmd/pinger/pinger.go @@ -34,7 +34,7 @@ func main() { go func() { pinger.InitPingerMetrics() metrics.InitKlogMetrics() - if err := metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.Port), false); err != nil { + if err := metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.Port), false, false); err != nil { util.LogFatalAndExit(err, "failed to run metrics server") } <-ctx.Done() diff --git a/pkg/metrics/server.go b/pkg/metrics/server.go index f389f398e39..ef22a0bcee0 100644 --- a/pkg/metrics/server.go +++ b/pkg/metrics/server.go @@ -3,6 +3,8 @@ package metrics import ( "context" "fmt" + "net/http" + "net/http/pprof" "k8s.io/client-go/rest" "k8s.io/klog/v2" @@ -11,7 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/metrics/server" ) -func Run(ctx context.Context, config *rest.Config, addr string, secureServing bool) error { +func Run(ctx context.Context, config *rest.Config, addr string, secureServing, withPprof bool) error { if config == nil { config = ctrl.GetConfigOrDie() } @@ -28,6 +30,15 @@ func Run(ctx context.Context, config *rest.Config, addr string, secureServing bo if secureServing { options.FilterProvider = filters.WithAuthenticationAndAuthorization } + if withPprof { + options.ExtraHandlers = map[string]http.Handler{ + "/debug/pprof/": http.HandlerFunc(pprof.Index), + "/debug/pprof/cmdline": http.HandlerFunc(pprof.Cmdline), + "/debug/pprof/profile": http.HandlerFunc(pprof.Profile), + "/debug/pprof/symbol": http.HandlerFunc(pprof.Symbol), + "/debug/pprof/trace": http.HandlerFunc(pprof.Trace), + } + } svr, err := server.NewServer(options, config, client) if err != nil { klog.Error(err)