orchard controller run: introduce "--listen-pprof" command-line argument (#326)

* orchard controller run: introduce "--pprof" command-line flag

* --pprof → --listen-pprof

* Log pprof HTTP server error, if any
This commit is contained in:
Nikolay Edigaryev 2025-06-26 18:15:10 +02:00 committed by GitHub
parent 10041dfb06
commit ae7cdd8628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 3 deletions

View File

@ -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 {