profiler.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //go:build !tinygo
  2. // +build !tinygo
  3. package middleware
  4. import (
  5. "expvar"
  6. "net/http"
  7. "net/http/pprof"
  8. "github.com/go-chi/chi/v5"
  9. )
  10. // Profiler is a convenient subrouter used for mounting net/http/pprof. ie.
  11. //
  12. // func MyService() http.Handler {
  13. // r := chi.NewRouter()
  14. // // ..middlewares
  15. // r.Mount("/debug", middleware.Profiler())
  16. // // ..routes
  17. // return r
  18. // }
  19. func Profiler() http.Handler {
  20. r := chi.NewRouter()
  21. r.Use(NoCache)
  22. r.Get("/", func(w http.ResponseWriter, r *http.Request) {
  23. http.Redirect(w, r, r.RequestURI+"/pprof/", http.StatusMovedPermanently)
  24. })
  25. r.HandleFunc("/pprof", func(w http.ResponseWriter, r *http.Request) {
  26. http.Redirect(w, r, r.RequestURI+"/", http.StatusMovedPermanently)
  27. })
  28. r.HandleFunc("/pprof/*", pprof.Index)
  29. r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
  30. r.HandleFunc("/pprof/profile", pprof.Profile)
  31. r.HandleFunc("/pprof/symbol", pprof.Symbol)
  32. r.HandleFunc("/pprof/trace", pprof.Trace)
  33. r.Handle("/vars", expvar.Handler())
  34. r.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
  35. r.Handle("/pprof/threadcreate", pprof.Handler("threadcreate"))
  36. r.Handle("/pprof/mutex", pprof.Handler("mutex"))
  37. r.Handle("/pprof/heap", pprof.Handler("heap"))
  38. r.Handle("/pprof/block", pprof.Handler("block"))
  39. r.Handle("/pprof/allocs", pprof.Handler("allocs"))
  40. return r
  41. }