|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http/pprof" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/gin-gonic/gin" |
| 8 | +) |
| 9 | + |
| 10 | +// WrapPProf Wrap adds several routes from package `net/http/pprof` to *gin.Engine object |
| 11 | +func WrapPProf(router *gin.Engine) { |
| 12 | + WrapGroup(&router.RouterGroup) |
| 13 | +} |
| 14 | + |
| 15 | +// WrapGroup adds several routes from package `net/http/pprof` to *gin.RouterGroup object |
| 16 | +func WrapGroup(router *gin.RouterGroup) { |
| 17 | + routers := []struct { |
| 18 | + Method string |
| 19 | + Path string |
| 20 | + Handler gin.HandlerFunc |
| 21 | + }{ |
| 22 | + {"GET", "/debug/pprof/", IndexHandler()}, |
| 23 | + {"GET", "/debug/pprof/heap", HeapHandler()}, |
| 24 | + {"GET", "/debug/pprof/goroutine", GoroutineHandler()}, |
| 25 | + {"GET", "/debug/pprof/allocs", AllocsHandler()}, |
| 26 | + {"GET", "/debug/pprof/block", BlockHandler()}, |
| 27 | + {"GET", "/debug/pprof/threadcreate", ThreadCreateHandler()}, |
| 28 | + {"GET", "/debug/pprof/cmdline", CmdlineHandler()}, |
| 29 | + {"GET", "/debug/pprof/profile", ProfileHandler()}, |
| 30 | + {"GET", "/debug/pprof/symbol", SymbolHandler()}, |
| 31 | + {"POST", "/debug/pprof/symbol", SymbolHandler()}, |
| 32 | + {"GET", "/debug/pprof/trace", TraceHandler()}, |
| 33 | + {"GET", "/debug/pprof/mutex", MutexHandler()}, |
| 34 | + } |
| 35 | + |
| 36 | + basePath := strings.TrimSuffix(router.BasePath(), "/") |
| 37 | + var prefix string |
| 38 | + |
| 39 | + switch { |
| 40 | + case basePath == "": |
| 41 | + prefix = "" |
| 42 | + case strings.HasSuffix(basePath, "/debug"): |
| 43 | + prefix = "/debug" |
| 44 | + case strings.HasSuffix(basePath, "/debug/pprof"): |
| 45 | + prefix = "/debug/pprof" |
| 46 | + } |
| 47 | + |
| 48 | + for _, r := range routers { |
| 49 | + router.Handle(r.Method, strings.TrimPrefix(r.Path, prefix), r.Handler) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// IndexHandler will pass the call from /debug/pprof to pprof |
| 54 | +func IndexHandler() gin.HandlerFunc { |
| 55 | + return func(ctx *gin.Context) { |
| 56 | + pprof.Index(ctx.Writer, ctx.Request) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// HeapHandler will pass the call from /debug/pprof/heap to pprof |
| 61 | +func HeapHandler() gin.HandlerFunc { |
| 62 | + return func(ctx *gin.Context) { |
| 63 | + pprof.Handler("heap").ServeHTTP(ctx.Writer, ctx.Request) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// GoroutineHandler will pass the call from /debug/pprof/goroutine to pprof |
| 68 | +func GoroutineHandler() gin.HandlerFunc { |
| 69 | + return func(ctx *gin.Context) { |
| 70 | + pprof.Handler("goroutine").ServeHTTP(ctx.Writer, ctx.Request) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// AllocsHandler will pass the call from /debug/pprof/allocs to pprof |
| 75 | +func AllocsHandler() gin.HandlerFunc { |
| 76 | + return func(ctx *gin.Context) { |
| 77 | + pprof.Handler("allocs").ServeHTTP(ctx.Writer, ctx.Request) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// BlockHandler will pass the call from /debug/pprof/block to pprof |
| 82 | +func BlockHandler() gin.HandlerFunc { |
| 83 | + return func(ctx *gin.Context) { |
| 84 | + pprof.Handler("block").ServeHTTP(ctx.Writer, ctx.Request) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// ThreadCreateHandler will pass the call from /debug/pprof/threadcreate to pprof |
| 89 | +func ThreadCreateHandler() gin.HandlerFunc { |
| 90 | + return func(ctx *gin.Context) { |
| 91 | + pprof.Handler("threadcreate").ServeHTTP(ctx.Writer, ctx.Request) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// CmdlineHandler will pass the call from /debug/pprof/cmdline to pprof |
| 96 | +func CmdlineHandler() gin.HandlerFunc { |
| 97 | + return func(ctx *gin.Context) { |
| 98 | + pprof.Cmdline(ctx.Writer, ctx.Request) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// ProfileHandler will pass the call from /debug/pprof/profile to pprof |
| 103 | +func ProfileHandler() gin.HandlerFunc { |
| 104 | + return func(ctx *gin.Context) { |
| 105 | + pprof.Profile(ctx.Writer, ctx.Request) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// SymbolHandler will pass the call from /debug/pprof/symbol to pprof |
| 110 | +func SymbolHandler() gin.HandlerFunc { |
| 111 | + return func(ctx *gin.Context) { |
| 112 | + pprof.Symbol(ctx.Writer, ctx.Request) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TraceHandler will pass the call from /debug/pprof/trace to pprof |
| 117 | +func TraceHandler() gin.HandlerFunc { |
| 118 | + return func(ctx *gin.Context) { |
| 119 | + pprof.Trace(ctx.Writer, ctx.Request) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// MutexHandler will pass the call from /debug/pprof/mutex to pprof |
| 124 | +func MutexHandler() gin.HandlerFunc { |
| 125 | + return func(ctx *gin.Context) { |
| 126 | + pprof.Handler("mutex").ServeHTTP(ctx.Writer, ctx.Request) |
| 127 | + } |
| 128 | +} |
0 commit comments