agent: Use EnableDebug to control adding pprof HTTP endpoints

This commit is contained in:
Armon Dadgar 2014-03-19 17:50:57 -07:00
parent 69065eea70
commit 4a2b7e5e39
3 changed files with 14 additions and 5 deletions

View File

@ -62,6 +62,7 @@ func (c *Command) readConfig() *Config {
cmdFlags.BoolVar(&cmdConfig.Bootstrap, "bootstrap", false, "enable server bootstrap mode") cmdFlags.BoolVar(&cmdConfig.Bootstrap, "bootstrap", false, "enable server bootstrap mode")
cmdFlags.StringVar(&cmdConfig.StatsiteAddr, "statsite", "", "address of statsite instance") cmdFlags.StringVar(&cmdConfig.StatsiteAddr, "statsite", "", "address of statsite instance")
cmdFlags.IntVar(&cmdConfig.Protocol, "protocol", -1, "protocol version") cmdFlags.IntVar(&cmdConfig.Protocol, "protocol", -1, "protocol version")
cmdFlags.BoolVar(&cmdConfig.EnableDebug, "debug", false, "enable debug features")
if err := cmdFlags.Parse(c.args); err != nil { if err := cmdFlags.Parse(c.args); err != nil {
return nil return nil
} }
@ -163,7 +164,7 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter) c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter)
if config.HTTPAddr != "" { if config.HTTPAddr != "" {
server, err := NewHTTPServer(agent, logOutput, config.HTTPAddr) server, err := NewHTTPServer(agent, config.EnableDebug, logOutput, config.HTTPAddr)
if err != nil { if err != nil {
agent.Shutdown() agent.Shutdown()
c.Ui.Error(fmt.Sprintf("Error starting http server: %s", err)) c.Ui.Error(fmt.Sprintf("Error starting http server: %s", err))

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/http/pprof"
"strconv" "strconv"
"time" "time"
) )
@ -23,7 +24,7 @@ type HTTPServer struct {
// NewHTTPServer starts a new HTTP server to provide an interface to // NewHTTPServer starts a new HTTP server to provide an interface to
// the agent. // the agent.
func NewHTTPServer(agent *Agent, logOutput io.Writer, bind string) (*HTTPServer, error) { func NewHTTPServer(agent *Agent, enableDebug bool, logOutput io.Writer, bind string) (*HTTPServer, error) {
// Create the mux // Create the mux
mux := http.NewServeMux() mux := http.NewServeMux()
@ -40,7 +41,7 @@ func NewHTTPServer(agent *Agent, logOutput io.Writer, bind string) (*HTTPServer,
listener: list, listener: list,
logger: log.New(logOutput, "", log.LstdFlags), logger: log.New(logOutput, "", log.LstdFlags),
} }
srv.registerHandlers() srv.registerHandlers(enableDebug)
// Start the server // Start the server
go http.Serve(list, mux) go http.Serve(list, mux)
@ -53,7 +54,7 @@ func (s *HTTPServer) Shutdown() {
} }
// registerHandlers is used to attach our handlers to the mux // registerHandlers is used to attach our handlers to the mux
func (s *HTTPServer) registerHandlers() { func (s *HTTPServer) registerHandlers(enableDebug bool) {
s.mux.HandleFunc("/", s.Index) s.mux.HandleFunc("/", s.Index)
s.mux.HandleFunc("/v1/status/leader", s.wrap(s.StatusLeader)) s.mux.HandleFunc("/v1/status/leader", s.wrap(s.StatusLeader))
@ -86,6 +87,13 @@ func (s *HTTPServer) registerHandlers() {
s.mux.HandleFunc("/v1/agent/service/register", s.wrap(s.AgentRegisterService)) s.mux.HandleFunc("/v1/agent/service/register", s.wrap(s.AgentRegisterService))
s.mux.HandleFunc("/v1/agent/service/deregister", s.wrap(s.AgentDeregisterService)) s.mux.HandleFunc("/v1/agent/service/deregister", s.wrap(s.AgentDeregisterService))
if enableDebug {
s.mux.HandleFunc("/debug/pprof/", pprof.Index)
s.mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
s.mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
s.mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
}
} }
// wrap is used to wrap functions to make them more convenient // wrap is used to wrap functions to make them more convenient

View File

@ -15,7 +15,7 @@ import (
func makeHTTPServer(t *testing.T) (string, *HTTPServer) { func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
conf := nextConfig() conf := nextConfig()
dir, agent := makeAgent(t, conf) dir, agent := makeAgent(t, conf)
server, err := NewHTTPServer(agent, agent.logOutput, conf.HTTPAddr) server, err := NewHTTPServer(agent, true, agent.logOutput, conf.HTTPAddr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }