github.com/nullne/docker@v1.13.0-rc1/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/block", pprof.Handler("block").ServeHTTP)
    23  	r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP)
    24  	r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
    25  	r.HandleFunc("/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
    26  }
    27  
    28  // Replicated from expvar.go as not public.
    29  func expVars(w http.ResponseWriter, r *http.Request) {
    30  	first := true
    31  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
    32  	fmt.Fprintf(w, "{\n")
    33  	expvar.Do(func(kv expvar.KeyValue) {
    34  		if !first {
    35  			fmt.Fprintf(w, ",\n")
    36  		}
    37  		first = false
    38  		fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
    39  	})
    40  	fmt.Fprintf(w, "\n}\n")
    41  }