diff --git a/command/agent/command.go b/command/agent/command.go index b00a0828b9..435bfe36aa 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -62,6 +62,7 @@ func (c *Command) readConfig() *Config { cmdFlags.BoolVar(&cmdConfig.Bootstrap, "bootstrap", false, "enable server bootstrap mode") cmdFlags.StringVar(&cmdConfig.StatsiteAddr, "statsite", "", "address of statsite instance") 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 { return nil } @@ -163,7 +164,7 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter) if config.HTTPAddr != "" { - server, err := NewHTTPServer(agent, logOutput, config.HTTPAddr) + server, err := NewHTTPServer(agent, config.EnableDebug, logOutput, config.HTTPAddr) if err != nil { agent.Shutdown() c.Ui.Error(fmt.Sprintf("Error starting http server: %s", err)) diff --git a/command/agent/http.go b/command/agent/http.go index 921050b536..33e84601ae 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -8,6 +8,7 @@ import ( "log" "net" "net/http" + "net/http/pprof" "strconv" "time" ) @@ -23,7 +24,7 @@ type HTTPServer struct { // NewHTTPServer starts a new HTTP server to provide an interface to // 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 mux := http.NewServeMux() @@ -40,7 +41,7 @@ func NewHTTPServer(agent *Agent, logOutput io.Writer, bind string) (*HTTPServer, listener: list, logger: log.New(logOutput, "", log.LstdFlags), } - srv.registerHandlers() + srv.registerHandlers(enableDebug) // Start the server go http.Serve(list, mux) @@ -53,7 +54,7 @@ func (s *HTTPServer) Shutdown() { } // 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("/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/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 diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 387e6fb6fa..90fdaa79af 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -15,7 +15,7 @@ import ( func makeHTTPServer(t *testing.T) (string, *HTTPServer) { conf := nextConfig() 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 { t.Fatalf("err: %v", err) }