From 3da4d47d50c9619b08f30e6fd927195e3045f5eb Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Wed, 30 Oct 2024 10:10:14 +0000 Subject: [PATCH] Move the pprof endpoints to the main HTTP server rather than starting a second server Signed-off-by: Richard Wall --- pkg/agent/config.go | 2 +- pkg/agent/run.go | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/agent/config.go b/pkg/agent/config.go index 241f70f8..25e4d253 100644 --- a/pkg/agent/config.go +++ b/pkg/agent/config.go @@ -281,7 +281,7 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) { "enable-pprof", "", false, - "Enables the pprof profiling server on the agent (port: 6060).", + "Enables the pprof profiling endpoints on the agent server (port: 8081).", ) c.PersistentFlags().BoolVarP( &cfg.Prometheus, diff --git a/pkg/agent/run.go b/pkg/agent/run.go index 6db9035a..fc4a828f 100644 --- a/pkg/agent/run.go +++ b/pkg/agent/run.go @@ -34,7 +34,7 @@ import ( "github.com/jetstack/preflight/pkg/logs" "github.com/jetstack/preflight/pkg/version" - _ "net/http/pprof" + "net/http/pprof" ) var Flags AgentCmdFlags @@ -74,19 +74,18 @@ func Run(cmd *cobra.Command, args []string) { logs.Log.Fatalf("While evaluating configuration: %v", err) } - if Flags.Profiling { - logs.Log.Printf("pprof profiling was enabled.\nRunning profiling on port :6060") - go func() { - err := http.ListenAndServe(":6060", nil) - if err != nil && !errors.Is(err, http.ErrServerClosed) { - logs.Log.Fatalf("failed to run pprof profiler: %s", err) - } - }() - } - go func() { server := http.NewServeMux() + if Flags.Profiling { + logs.Log.Printf("pprof profiling was enabled.") + server.HandleFunc("/debug/pprof/", pprof.Index) + server.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + server.HandleFunc("/debug/pprof/profile", pprof.Profile) + server.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + server.HandleFunc("/debug/pprof/trace", pprof.Trace) + } + if Flags.Prometheus { logs.Log.Printf("Prometheus was enabled.\nRunning prometheus on port :8081") prometheus.MustRegister(metricPayloadSize)