pprof.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package pprof
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "net/http/pprof"
  6. )
  7. const (
  8. DefaultPrefix = "/debug/pprof"
  9. )
  10. func getPrefix(prefixOptions ...string) string {
  11. prefix := DefaultPrefix
  12. if len(prefixOptions) > 0 {
  13. prefix = prefixOptions[0]
  14. }
  15. return prefix
  16. }
  17. // Register the standard HandlerFuncs from the net/http/pprof package with
  18. // the provided gin.Engine. prefixOptions is a optional. If not prefixOptions,
  19. // the default path prefix is used, otherwise first prefixOptions will be path prefix.
  20. func Register(r *gin.Engine, prefixOptions ...string) {
  21. prefix := getPrefix(prefixOptions...)
  22. prefixRouter := r.Group(prefix)
  23. {
  24. prefixRouter.GET("/", pprofHandler(pprof.Index))
  25. prefixRouter.GET("/cmdline", pprofHandler(pprof.Cmdline))
  26. prefixRouter.GET("/profile", pprofHandler(pprof.Profile))
  27. prefixRouter.POST("/symbol", pprofHandler(pprof.Symbol))
  28. prefixRouter.GET("/symbol", pprofHandler(pprof.Symbol))
  29. prefixRouter.GET("/trace", pprofHandler(pprof.Trace))
  30. prefixRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP))
  31. prefixRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP))
  32. prefixRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP))
  33. prefixRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP))
  34. prefixRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP))
  35. }
  36. }
  37. func pprofHandler(h http.HandlerFunc) gin.HandlerFunc {
  38. handler := http.HandlerFunc(h)
  39. return func(c *gin.Context) {
  40. handler.ServeHTTP(c.Writer, c.Request)
  41. }
  42. }