forked from pneymrl2f/nightingale
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.0 KiB
59 lines
2.0 KiB
package pprof
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/pprof"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
// DefaultPrefix url prefix of pprof
|
|
DefaultPrefix = "/debug/pprof"
|
|
)
|
|
|
|
func getPrefix(prefixOptions ...string) string {
|
|
prefix := DefaultPrefix
|
|
if len(prefixOptions) > 0 {
|
|
prefix = prefixOptions[0]
|
|
}
|
|
return prefix
|
|
}
|
|
|
|
// Register the standard HandlerFuncs from the net/http/pprof package with
|
|
// the provided gin.Engine. prefixOptions is a optional. If not prefixOptions,
|
|
// the default path prefix is used, otherwise first prefixOptions will be path prefix.
|
|
func Register(r *gin.Engine, prefixOptions ...string) {
|
|
RouteRegister(&(r.RouterGroup), prefixOptions...)
|
|
}
|
|
|
|
// RouteRegister the standard HandlerFuncs from the net/http/pprof package with
|
|
// the provided gin.GrouterGroup. prefixOptions is a optional. If not prefixOptions,
|
|
// the default path prefix is used, otherwise first prefixOptions will be path prefix.
|
|
func RouteRegister(rg *gin.RouterGroup, prefixOptions ...string) {
|
|
prefix := getPrefix(prefixOptions...)
|
|
|
|
prefixRouter := rg.Group(prefix)
|
|
{
|
|
prefixRouter.GET("/", pprofHandler(pprof.Index))
|
|
prefixRouter.GET("/cmdline", pprofHandler(pprof.Cmdline))
|
|
prefixRouter.GET("/profile", pprofHandler(pprof.Profile))
|
|
prefixRouter.POST("/symbol", pprofHandler(pprof.Symbol))
|
|
prefixRouter.GET("/symbol", pprofHandler(pprof.Symbol))
|
|
prefixRouter.GET("/trace", pprofHandler(pprof.Trace))
|
|
prefixRouter.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP))
|
|
prefixRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP))
|
|
prefixRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP))
|
|
prefixRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP))
|
|
prefixRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP))
|
|
prefixRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP))
|
|
}
|
|
}
|
|
|
|
func pprofHandler(h http.HandlerFunc) gin.HandlerFunc {
|
|
handler := http.HandlerFunc(h)
|
|
return func(c *gin.Context) {
|
|
handler.ServeHTTP(c.Writer, c.Request)
|
|
}
|
|
}
|