2020-11-13 18:21:07 +05:30
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/pprof"
|
|
|
|
|
2021-12-03 02:28:08 +05:30
|
|
|
"github.com/go-chi/chi/v5"
|
2020-11-13 18:21:07 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Profiler is a convenient subrouter used for mounting net/http/pprof. ie.
|
|
|
|
//
|
|
|
|
// func MyService() http.Handler {
|
|
|
|
// r := chi.NewRouter()
|
|
|
|
// // ..middlewares
|
|
|
|
// r.Mount("/debug", middleware.Profiler())
|
|
|
|
// // ..routes
|
|
|
|
// return r
|
|
|
|
// }
|
|
|
|
func Profiler() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(NoCache)
|
|
|
|
|
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2021-03-01 04:38:33 +05:30
|
|
|
http.Redirect(w, r, r.RequestURI+"/pprof/", http.StatusMovedPermanently)
|
2020-11-13 18:21:07 +05:30
|
|
|
})
|
|
|
|
r.HandleFunc("/pprof", func(w http.ResponseWriter, r *http.Request) {
|
2021-03-01 04:38:33 +05:30
|
|
|
http.Redirect(w, r, r.RequestURI+"/", http.StatusMovedPermanently)
|
2020-11-13 18:21:07 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
r.HandleFunc("/pprof/*", pprof.Index)
|
|
|
|
r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
|
|
|
|
r.HandleFunc("/pprof/profile", pprof.Profile)
|
|
|
|
r.HandleFunc("/pprof/symbol", pprof.Symbol)
|
|
|
|
r.HandleFunc("/pprof/trace", pprof.Trace)
|
|
|
|
r.HandleFunc("/vars", expVars)
|
|
|
|
|
2021-12-03 02:28:08 +05:30
|
|
|
r.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
|
|
|
|
r.Handle("/pprof/threadcreate", pprof.Handler("threadcreate"))
|
|
|
|
r.Handle("/pprof/mutex", pprof.Handler("mutex"))
|
|
|
|
r.Handle("/pprof/heap", pprof.Handler("heap"))
|
|
|
|
r.Handle("/pprof/block", pprof.Handler("block"))
|
|
|
|
r.Handle("/pprof/allocs", pprof.Handler("allocs"))
|
|
|
|
|
2020-11-13 18:21:07 +05:30
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replicated from expvar.go as not public.
|
|
|
|
func expVars(w http.ResponseWriter, r *http.Request) {
|
|
|
|
first := true
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
fmt.Fprintf(w, "{\n")
|
|
|
|
expvar.Do(func(kv expvar.KeyValue) {
|
|
|
|
if !first {
|
|
|
|
fmt.Fprintf(w, ",\n")
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
|
|
|
})
|
|
|
|
fmt.Fprintf(w, "\n}\n")
|
|
|
|
}
|