From ae7cdd86286f15e3520819c58bba23ec18e898ad Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 26 Jun 2025 18:15:10 +0200 Subject: [PATCH] orchard controller run: introduce "--listen-pprof" command-line argument (#326) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * orchard controller run: introduce "--pprof" command-line flag * --pprof → --listen-pprof * Log pprof HTTP server error, if any --- internal/command/controller/run.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/command/controller/run.go b/internal/command/controller/run.go index 86168a5..482a282 100644 --- a/internal/command/controller/run.go +++ b/internal/command/controller/run.go @@ -12,6 +12,8 @@ import ( "github.com/gin-gonic/gin" "github.com/spf13/cobra" "go.uber.org/zap" + "net/http" + _ "net/http/pprof" "os" "path/filepath" "strconv" @@ -22,6 +24,7 @@ var ErrRunFailed = errors.New("failed to run controller") var address string var addressSSH string +var addressPprof string var debug bool var noTLS bool var sshNoClientAuth bool @@ -45,10 +48,9 @@ func newRunCommand() *cobra.Command { "address to listen on") cmd.Flags().StringVar(&addressSSH, "listen-ssh", "", "address for the built-in SSH server to listen on (e.g. \":6122\")") + cmd.Flags().StringVar(&addressPprof, "listen-pprof", "", + "start pprof HTTP server on localhost:6060 for diagnostic purposes (e.g. \"localhost:6060\")") cmd.Flags().BoolVar(&debug, "debug", false, "enable debug logging") - - // flags for auto-init if necessary - // this simplifies the user experience to run the controller in serverless environments cmd.Flags().StringVar(&controllerCertPath, "controller-cert", "", "use the controller certificate from the specified path instead of the auto-generated one"+ " (requires --controller-key)") @@ -94,6 +96,14 @@ func runController(cmd *cobra.Command, args []string) (err error) { } }() + if addressPprof != "" { + go func() { + if err := http.ListenAndServe(addressPprof, nil); err != nil { + logger.Sugar().Errorf("pprof server failed: %v", err) + } + }() + } + // Redirect standard's library package-global logger // to our zap logger at debug level if _, err := zap.RedirectStdLogAt(logger, zap.DebugLevel); err != nil {