2015-03-31 14:38:17 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/pprof"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2016-02-16 13:58:24 -05:00
|
|
|
const debugPathPrefix = "/debug/"
|
|
|
|
|
|
|
|
func profilerSetup(mainRouter *mux.Router) {
|
|
|
|
var r = mainRouter.PathPrefix(debugPathPrefix).Subrouter()
|
2015-04-01 04:56:30 -04:00
|
|
|
r.HandleFunc("/vars", expVars)
|
2015-03-31 14:38:17 -04:00
|
|
|
r.HandleFunc("/pprof/", pprof.Index)
|
|
|
|
r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
|
|
|
|
r.HandleFunc("/pprof/profile", pprof.Profile)
|
|
|
|
r.HandleFunc("/pprof/symbol", pprof.Symbol)
|
2016-05-13 03:24:44 -04:00
|
|
|
r.HandleFunc("/pprof/trace", pprof.Trace)
|
2015-03-31 14:38:17 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replicated from expvar.go as not public.
|
2015-04-01 04:56:30 -04:00
|
|
|
func expVars(w http.ResponseWriter, r *http.Request) {
|
2015-03-31 14:38:17 -04:00
|
|
|
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")
|
|
|
|
}
|