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)
|
2017-03-16 11:05:20 -04:00
|
|
|
r.HandleFunc("/pprof/{name}", handlePprof)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePprof(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var name string
|
|
|
|
if vars := mux.Vars(r); vars != nil {
|
|
|
|
name = vars["name"]
|
|
|
|
}
|
|
|
|
pprof.Handler(name).ServeHTTP(w, r)
|
2015-03-31 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2017-02-07 22:22:21 -05:00
|
|
|
fmt.Fprintln(w, "{")
|
2015-03-31 14:38:17 -04:00
|
|
|
expvar.Do(func(kv expvar.KeyValue) {
|
|
|
|
if !first {
|
2017-02-07 22:22:21 -05:00
|
|
|
fmt.Fprintln(w, ",")
|
2015-03-31 14:38:17 -04:00
|
|
|
}
|
|
|
|
first = false
|
|
|
|
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
|
|
|
})
|
2017-02-07 22:22:21 -05:00
|
|
|
fmt.Fprintln(w, "\n}")
|
2015-03-31 14:38:17 -04:00
|
|
|
}
|