From f1155ca431373b79a6c83e4597ae205c146f3cba Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 3 Dec 2015 03:01:05 -0800 Subject: [PATCH 1/3] Enable profiling over HTTP in debug mode Signed-off-by: Andrea Luzzardi --- api/primary.go | 20 +++++++++++++++++++- api/replica.go | 2 +- cli/manage.go | 4 ++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api/primary.go b/api/primary.go index d741282bc7..6e1d0c8fd1 100644 --- a/api/primary.go +++ b/api/primary.go @@ -4,6 +4,8 @@ import ( "crypto/tls" "net/http" + "net/http/pprof" + log "github.com/Sirupsen/logrus" "github.com/docker/swarm/cluster" "github.com/gorilla/mux" @@ -100,8 +102,20 @@ func writeCorsHeaders(w http.ResponseWriter, r *http.Request) { w.Header().Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS") } +func profilerSetup(mainRouter *mux.Router, path string) { + var r = mainRouter.PathPrefix(path).Subrouter() + r.HandleFunc("/pprof/", pprof.Index) + r.HandleFunc("/pprof/cmdline", pprof.Cmdline) + r.HandleFunc("/pprof/profile", pprof.Profile) + r.HandleFunc("/pprof/symbol", pprof.Symbol) + r.HandleFunc("/pprof/block", pprof.Handler("block").ServeHTTP) + r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP) + r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP) + r.HandleFunc("/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP) +} + // NewPrimary creates a new API router. -func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, enableCors bool) *mux.Router { +func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, debug, enableCors bool) *mux.Router { // Register the API events handler in the cluster. eventsHandler := newEventsHandler() cluster.RegisterEventHandler(eventsHandler) @@ -134,5 +148,9 @@ func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHan } } + if debug { + profilerSetup(r, "/debug/") + } + return r } diff --git a/api/replica.go b/api/replica.go index b9a12e371a..5b9e6c6488 100644 --- a/api/replica.go +++ b/api/replica.go @@ -7,7 +7,7 @@ import ( "strings" ) -var localRoutes = []string{"/info", "/_ping"} +var localRoutes = []string{"/info", "/_ping", "/debug"} // Replica is an API replica that reserves proxy to the primary. type Replica struct { diff --git a/cli/manage.go b/cli/manage.go index 2fb39bbad5..260cc8bc67 100644 --- a/cli/manage.go +++ b/cli/manage.go @@ -140,7 +140,7 @@ func setupReplication(c *cli.Context, cluster cluster.Cluster, server *api.Serve candidate := leadership.NewCandidate(client, p, addr, leaderTTL) follower := leadership.NewFollower(client, p) - primary := api.NewPrimary(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.Bool("cors")) + primary := api.NewPrimary(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.GlobalBool("debug"), c.Bool("cors")) replica := api.NewReplica(primary, tlsConfig) go func() { @@ -307,7 +307,7 @@ func manage(c *cli.Context) { setupReplication(c, cl, server, discovery, addr, leaderTTL, tlsConfig) } else { - server.SetHandler(api.NewPrimary(cl, tlsConfig, &statusHandler{cl, nil, nil}, c.Bool("cors"))) + server.SetHandler(api.NewPrimary(cl, tlsConfig, &statusHandler{cl, nil, nil}, c.GlobalBool("debug"), c.Bool("cors"))) } log.Fatal(server.ListenAndServe()) From 4748bb14ddddb0343d2344e16e4fb82e2f918842 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 7 Dec 2015 21:59:03 -0800 Subject: [PATCH 2/3] add trace Signed-off-by: Victor Vieux --- api/primary.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/primary.go b/api/primary.go index 6e1d0c8fd1..7f5a5e1c4f 100644 --- a/api/primary.go +++ b/api/primary.go @@ -108,6 +108,7 @@ func profilerSetup(mainRouter *mux.Router, path string) { r.HandleFunc("/pprof/cmdline", pprof.Cmdline) r.HandleFunc("/pprof/profile", pprof.Profile) r.HandleFunc("/pprof/symbol", pprof.Symbol) + r.HandleFunc("/debug/pprof/trace", pprof.Trace) r.HandleFunc("/pprof/block", pprof.Handler("block").ServeHTTP) r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP) r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP) From 0bddb0096fdc76805744d6b0b5fce2b70a8cb338 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 7 Dec 2015 22:04:14 -0800 Subject: [PATCH 3/3] longer retries Signed-off-by: Victor Vieux --- test/integration/helpers.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/helpers.bash b/test/integration/helpers.bash index 764dac6953..69f87a80f6 100644 --- a/test/integration/helpers.bash +++ b/test/integration/helpers.bash @@ -82,7 +82,7 @@ function retry() { # Waits until the given docker engine API becomes reachable. function wait_until_reachable() { - retry 10 1 docker -H $1 info + retry 15 1 docker -H $1 info } # Start the swarm manager in background.