|
| 1 | +package pprof |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/pprof" |
| 6 | + |
| 7 | + "github.com/labstack/echo/v4" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + // DefaultPrefix url prefix of pprof |
| 12 | + DefaultPrefix = "/debug/pprof" |
| 13 | +) |
| 14 | + |
| 15 | +func getPrefix(prefixOptions ...string) string { |
| 16 | + if len(prefixOptions) > 0 { |
| 17 | + return prefixOptions[0] |
| 18 | + } |
| 19 | + return DefaultPrefix |
| 20 | +} |
| 21 | + |
| 22 | +// Register middleware for net/http/pprof |
| 23 | +func Register(e *echo.Echo, prefixOptions ...string) { |
| 24 | + prefix := getPrefix(prefixOptions...) |
| 25 | + |
| 26 | + prefixRouter := e.Group(prefix) |
| 27 | + { |
| 28 | + prefixRouter.GET("/", handler(pprof.Index)) |
| 29 | + prefixRouter.GET("/allocs", handler(pprof.Handler("allocs").ServeHTTP)) |
| 30 | + prefixRouter.GET("/block", handler(pprof.Handler("block").ServeHTTP)) |
| 31 | + prefixRouter.GET("/cmdline", handler(pprof.Cmdline)) |
| 32 | + prefixRouter.GET("/goroutine", handler(pprof.Handler("goroutine").ServeHTTP)) |
| 33 | + prefixRouter.GET("/heap", handler(pprof.Handler("heap").ServeHTTP)) |
| 34 | + prefixRouter.GET("/mutex", handler(pprof.Handler("mutex").ServeHTTP)) |
| 35 | + prefixRouter.GET("/profile", handler(pprof.Profile)) |
| 36 | + prefixRouter.POST("/symbol", handler(pprof.Symbol)) |
| 37 | + prefixRouter.GET("/symbol", handler(pprof.Symbol)) |
| 38 | + prefixRouter.GET("/threadcreate", handler(pprof.Handler("threadcreate").ServeHTTP)) |
| 39 | + prefixRouter.GET("/trace", handler(pprof.Trace)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func handler(h http.HandlerFunc) echo.HandlerFunc { |
| 44 | + return func(c echo.Context) error { |
| 45 | + h.ServeHTTP(c.Response().Writer, c.Request()) |
| 46 | + return nil |
| 47 | + } |
| 48 | +} |
0 commit comments