From 7609d5279743b47450cc1273ee75504bb6abf8b6 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 31 Mar 2015 11:38:17 -0700 Subject: [PATCH] Move Profiler into specific http.Handler Signed-off-by: Michael Crosby --- api/server/profiler.go | 52 ++++++++++++++++++++++++++++++++++++++++++ api/server/server.go | 32 +------------------------- 2 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 api/server/profiler.go diff --git a/api/server/profiler.go b/api/server/profiler.go new file mode 100644 index 0000000000..f27dc6cca4 --- /dev/null +++ b/api/server/profiler.go @@ -0,0 +1,52 @@ +package server + +import ( + "expvar" + "fmt" + "net/http" + "net/http/pprof" + + "github.com/gorilla/mux" +) + +func NewProfiler() http.Handler { + var ( + p = &Profiler{} + r = mux.NewRouter() + ) + r.HandleFunc("/vars", p.expVars) + 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) + p.r = r + return p +} + +// Profiler enables pprof and expvar support via a HTTP API. +type Profiler struct { + r *mux.Router +} + +func (p *Profiler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + p.r.ServeHTTP(w, r) +} + +// Replicated from expvar.go as not public. +func (p *Profiler) expVars(w http.ResponseWriter, r *http.Request) { + first := true + w.Header().Set("Content-Type", "application/json; charset=utf-8") + fmt.Fprintf(w, "{\n") + expvar.Do(func(kv expvar.KeyValue) { + if !first { + fmt.Fprintf(w, ",\n") + } + first = false + fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value) + }) + fmt.Fprintf(w, "\n}\n") +} diff --git a/api/server/server.go b/api/server/server.go index 96abf5c3b5..3d248bb58d 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -6,13 +6,11 @@ import ( "encoding/base64" "encoding/json" - "expvar" "fmt" "io" "io/ioutil" "net" "net/http" - "net/http/pprof" "os" "strconv" "strings" @@ -1308,38 +1306,11 @@ func makeHttpHandler(eng *engine.Engine, logging bool, localMethod string, local } } -// Replicated from expvar.go as not public. -func expvarHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=utf-8") - fmt.Fprintf(w, "{\n") - first := true - expvar.Do(func(kv expvar.KeyValue) { - if !first { - fmt.Fprintf(w, ",\n") - } - first = false - fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value) - }) - fmt.Fprintf(w, "\n}\n") -} - -func AttachProfiler(router *mux.Router) { - router.HandleFunc("/debug/vars", expvarHandler) - router.HandleFunc("/debug/pprof/", pprof.Index) - router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) - router.HandleFunc("/debug/pprof/profile", pprof.Profile) - router.HandleFunc("/debug/pprof/symbol", pprof.Symbol) - router.HandleFunc("/debug/pprof/block", pprof.Handler("block").ServeHTTP) - router.HandleFunc("/debug/pprof/heap", pprof.Handler("heap").ServeHTTP) - router.HandleFunc("/debug/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP) - router.HandleFunc("/debug/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP) -} - // we keep enableCors just for legacy usage, need to be removed in the future func createRouter(eng *engine.Engine, logging, enableCors bool, corsHeaders string, dockerVersion string) *mux.Router { r := mux.NewRouter() if os.Getenv("DEBUG") != "" { - AttachProfiler(r) + r.Handle("/debug", NewProfiler()) } m := map[string]map[string]HttpApiFunc{ "GET": { @@ -1494,7 +1465,6 @@ func newListener(proto, addr string, bufferRequests bool) (net.Listener, error) if bufferRequests { return listenbuffer.NewListenBuffer(proto, addr, activationLock) } - return net.Listen(proto, addr) }