add an option to expose a pprof http endpoint

This commit is contained in:
Shivaram Lingamneni 2018-03-13 14:46:39 -04:00
parent b211fd35da
commit 211ed6af99
3 changed files with 39 additions and 1 deletions

View File

@ -237,7 +237,8 @@ type Config struct {
Logging []logger.LoggingConfig
Debug struct {
RecoverFromErrors *bool `yaml:"recover-from-errors"`
RecoverFromErrors *bool `yaml:"recover-from-errors"`
PprofListener *string `yaml:"pprof-listener"`
StackImpact StackImpactConfig
}

View File

@ -13,6 +13,8 @@ import (
"log"
"math/rand"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
@ -120,6 +122,7 @@ type Server struct {
recoverFromErrors bool
rehashMutex sync.Mutex // tier 4
rehashSignal chan os.Signal
pprofServer *http.Server
proxyAllowedFrom []string
signals chan os.Signal
snomasks *SnoManager
@ -968,6 +971,8 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
}
}
server.setupPprofListener(config)
// we are now open for business
server.setupListeners(config)
@ -987,6 +992,32 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
return nil
}
func (server *Server) setupPprofListener(config *Config) {
pprofListener := ""
if config.Debug.PprofListener != nil {
pprofListener = *config.Debug.PprofListener
}
if server.pprofServer != nil {
if pprofListener == "" || (pprofListener != server.pprofServer.Addr) {
server.logger.Info("rehash", "Stopping pprof listener", server.pprofServer.Addr)
server.pprofServer.Close()
server.pprofServer = nil
}
}
if pprofListener != "" && server.pprofServer == nil {
ps := http.Server{
Addr: pprofListener,
}
go func() {
if err := ps.ListenAndServe(); err != nil {
server.logger.Error("rehash", fmt.Sprintf("pprof listener failed: %v", err))
}
}()
server.pprofServer = &ps
server.logger.Info("rehash", "Started pprof listener", server.pprofServer.Addr)
}
}
func (server *Server) loadMOTD(motdPath string, useFormatting bool) error {
server.logger.Info("rehash", "Using MOTD", motdPath)
motdLines := make([]string, 0)

View File

@ -316,6 +316,12 @@ debug:
# this to false.
recover-from-errors: true
# optionally expose a pprof http endpoint: https://golang.org/pkg/net/http/pprof/
# it is strongly recommended that you don't expose this on a public interface;
# if you need to access it remotely, you can use an SSH tunnel.
# set to `null`, "", leave blank, or omit to disable
pprof-listener: "localhost:6060"
# enabling StackImpact profiling
stackimpact:
# whether to use StackImpact