Add support for proper use of pprof

This commit is contained in:
Dane Everitt 2022-01-30 14:50:37 -05:00
parent 1965e68a78
commit c3df8d2309
2 changed files with 12 additions and 21 deletions

1
.gitignore vendored
View File

@ -49,3 +49,4 @@ debug
.DS_Store .DS_Store
*.pprof *.pprof
*.pdf *.pdf
pprof.*

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
log2 "log" log2 "log"
"net/http" "net/http"
_ "net/http/pprof"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -20,7 +21,6 @@ import (
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/gammazero/workerpool" "github.com/gammazero/workerpool"
"github.com/mitchellh/colorstring" "github.com/mitchellh/colorstring"
"github.com/pkg/profile"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/acme" "golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert" "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") rootCommand.PersistentFlags().BoolVar(&debug, "debug", false, "pass in order to run wings in debug mode")
// Flags specifically used when running the API. // 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().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().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") 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) { 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() printLogo()
log.Debug("running in debug mode") log.Debug("running in debug mode")
log.WithField("config_file", configPath).Info("loading configuration from file") log.WithField("config_file", configPath).Info("loading configuration from file")
@ -325,6 +307,14 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
TLSConfig: config.DefaultTLSConfig, 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. // Check if the server should run with TLS but using autocert.
if autotls { if autotls {
m := autocert.Manager{ m := autocert.Manager{