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