Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1492 from aluzzardi/profiling
Browse files Browse the repository at this point in the history
Enable profiling over HTTP in debug mode
  • Loading branch information
vieux committed Dec 8, 2015
2 parents ba976ed + 0bddb00 commit 4aafe4a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
21 changes: 20 additions & 1 deletion api/primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -100,8 +102,21 @@ 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("/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)
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)
Expand Down Expand Up @@ -134,5 +149,9 @@ func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHan
}
}

if debug {
profilerSetup(r, "/debug/")
}

return r
}
2 changes: 1 addition & 1 deletion api/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cli/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion test/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

# Returns true if all nodes have joined the swarm.
Expand Down

0 comments on commit 4aafe4a

Please sign in to comment.