github.com/akashshinde/docker@v1.9.1/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  func profilerSetup(mainRouter *mux.Router, path string) {
    13  	var r = mainRouter.PathPrefix(path).Subrouter()
    14  	r.HandleFunc("/vars", expVars)
    15  	r.HandleFunc("/pprof/", pprof.Index)
    16  	r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
    17  	r.HandleFunc("/pprof/profile", pprof.Profile)
    18  	r.HandleFunc("/pprof/symbol", pprof.Symbol)
    19  	r.HandleFunc("/pprof/block", pprof.Handler("block").ServeHTTP)
    20  	r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP)
    21  	r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
    22  	r.HandleFunc("/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
    23  }
    24  
    25  // Replicated from expvar.go as not public.
    26  func expVars(w http.ResponseWriter, r *http.Request) {
    27  	first := true
    28  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
    29  	fmt.Fprintf(w, "{\n")
    30  	expvar.Do(func(kv expvar.KeyValue) {
    31  		if !first {
    32  			fmt.Fprintf(w, ",\n")
    33  		}
    34  		first = false
    35  		fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
    36  	})
    37  	fmt.Fprintf(w, "\n}\n")
    38  }