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