github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/api/server/profiler.go (about)

     1  package server
     2  
     3  import (
     4  	"expvar"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/pprof"
     8  
     9  	"github.com/gorilla/mux"
    10  )
    11  
    12  const debugPathPrefix = "/debug/"
    13  
    14  func profilerSetup(mainRouter *mux.Router) {
    15  	var r = mainRouter.PathPrefix(debugPathPrefix).Subrouter()
    16  	r.HandleFunc("/vars", expVars)
    17  	r.HandleFunc("/pprof/", pprof.Index)
    18  	r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
    19  	r.HandleFunc("/pprof/profile", pprof.Profile)
    20  	r.HandleFunc("/pprof/symbol", pprof.Symbol)
    21  	r.HandleFunc("/pprof/trace", pprof.Trace)
    22  	r.HandleFunc("/pprof/{name}", handlePprof)
    23  }
    24  
    25  func handlePprof(w http.ResponseWriter, r *http.Request) {
    26  	var name string
    27  	if vars := mux.Vars(r); vars != nil {
    28  		name = vars["name"]
    29  	}
    30  	pprof.Handler(name).ServeHTTP(w, r)
    31  }
    32  
    33  // Replicated from expvar.go as not public.
    34  func expVars(w http.ResponseWriter, r *http.Request) {
    35  	first := true
    36  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
    37  	fmt.Fprintln(w, "{")
    38  	expvar.Do(func(kv expvar.KeyValue) {
    39  		if !first {
    40  			fmt.Fprintln(w, ",")
    41  		}
    42  		first = false
    43  		fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
    44  	})
    45  	fmt.Fprintln(w, "\n}")
    46  }