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

Enable profiling over HTTP in debug mode #1492

Merged
merged 3 commits into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 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,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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r.HandleFunc("/debug/pprof/trace", pprof.Trace)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly this. I probably should have done this by example like you did. If there is a reason to not expose it, that's fine. Don't let me hold up the merge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's new in go 1.5.x, I added a commit to the PR


// 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 +148,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