github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin-contrib/pprof/pprof.go (about)

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