profiler.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package server
  2. import (
  3. "expvar"
  4. "fmt"
  5. "net/http"
  6. "net/http/pprof"
  7. "github.com/gorilla/mux"
  8. )
  9. func profilerSetup(mainRouter *mux.Router, path string) {
  10. var r = mainRouter.PathPrefix(path).Subrouter()
  11. r.HandleFunc("/vars", expVars)
  12. r.HandleFunc("/pprof/", pprof.Index)
  13. r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
  14. r.HandleFunc("/pprof/profile", pprof.Profile)
  15. r.HandleFunc("/pprof/symbol", pprof.Symbol)
  16. r.HandleFunc("/pprof/block", pprof.Handler("block").ServeHTTP)
  17. r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP)
  18. r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
  19. r.HandleFunc("/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
  20. }
  21. // Replicated from expvar.go as not public.
  22. func expVars(w http.ResponseWriter, r *http.Request) {
  23. first := true
  24. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  25. fmt.Fprintf(w, "{\n")
  26. expvar.Do(func(kv expvar.KeyValue) {
  27. if !first {
  28. fmt.Fprintf(w, ",\n")
  29. }
  30. first = false
  31. fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
  32. })
  33. fmt.Fprintf(w, "\n}\n")
  34. }