diff --git a/.gitignore b/.gitignore index 4f2bd7e..91d19a1 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,4 @@ debug .DS_Store *.pprof *.pdf +pprof.* \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index ab0dcc7..90b6865 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,7 @@ import ( "fmt" log2 "log" "net/http" + _ "net/http/pprof" "os" "path" "path/filepath" @@ -20,7 +21,6 @@ import ( "github.com/docker/docker/client" "github.com/gammazero/workerpool" "github.com/mitchellh/colorstring" - "github.com/pkg/profile" "github.com/spf13/cobra" "golang.org/x/crypto/acme" "golang.org/x/crypto/acme/autocert" @@ -75,7 +75,8 @@ func init() { rootCommand.PersistentFlags().BoolVar(&debug, "debug", false, "pass in order to run wings in debug mode") // Flags specifically used when running the API. - rootCommand.Flags().String("profiler", "", "the profiler to run for this instance") + rootCommand.Flags().Bool("pprof", false, "if the pprof profiler should be enabled. The profiler will bind to localhost:6060 by default") + rootCommand.Flags().Int("pprof-port", 6060, "If provided with --pprof, the port it will run on") rootCommand.Flags().Bool("auto-tls", false, "pass in order to have wings generate and manage it's own SSL certificates using Let's Encrypt") rootCommand.Flags().String("tls-hostname", "", "required with --auto-tls, the FQDN for the generated SSL certificate") rootCommand.Flags().Bool("ignore-certificate-errors", false, "ignore certificate verification errors when executing API calls") @@ -86,25 +87,6 @@ func init() { } func rootCmdRun(cmd *cobra.Command, _ []string) { - switch cmd.Flag("profiler").Value.String() { - case "cpu": - defer profile.Start(profile.CPUProfile).Stop() - case "mem": - defer profile.Start(profile.MemProfile).Stop() - case "alloc": - defer profile.Start(profile.MemProfile, profile.MemProfileAllocs).Stop() - case "heap": - defer profile.Start(profile.MemProfile, profile.MemProfileHeap).Stop() - case "routines": - defer profile.Start(profile.GoroutineProfile).Stop() - case "mutex": - defer profile.Start(profile.MutexProfile).Stop() - case "threads": - defer profile.Start(profile.ThreadcreationProfile).Stop() - case "block": - defer profile.Start(profile.BlockProfile).Stop() - } - printLogo() log.Debug("running in debug mode") log.WithField("config_file", configPath).Info("loading configuration from file") @@ -325,6 +307,14 @@ func rootCmdRun(cmd *cobra.Command, _ []string) { TLSConfig: config.DefaultTLSConfig, } + profile, _ := cmd.Flags().GetBool("pprof") + if profile { + profilePort, _ := cmd.Flags().GetInt("pprof-port") + go func() { + http.ListenAndServe(fmt.Sprintf("localhost:%d", profilePort), nil) + }() + } + // Check if the server should run with TLS but using autocert. if autotls { m := autocert.Manager{