Repository: DeanThompson/ginpprof Branch: master Commit: 007b1e56b2e1 Files: 7 Total size: 9.4 KB Directory structure: gitextract_z_jy1e1m/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example/ │ └── example.go ├── pprof.go └── pprof_test.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof ================================================ FILE: .travis.yml ================================================ language: go arch: - AMD64 - ppc64le go: - 1.12 - 1.13 - 1.14 - 1.15 script: - go test ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Yangliang Li Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ ginpprof ======== [![GoDoc](https://godoc.org/github.com/DeanThompson/ginpprof?status.svg)](https://godoc.org/github.com/DeanThompson/ginpprof) [![Build Status](https://travis-ci.org/DeanThompson/ginpprof.svg?branch=master)](https://travis-ci.org/DeanThompson/ginpprof) A wrapper for [golang web framework gin](https://github.com/gin-gonic/gin) to use `net/http/pprof` easily. ## Install First install ginpprof to your GOPATH using `go get`: ```sh go get github.com/DeanThompson/ginpprof ``` ## Usage ```go package main import ( "github.com/gin-gonic/gin" "github.com/DeanThompson/ginpprof" ) func main() { router := gin.Default() router.GET("/ping", func(c *gin.Context) { c.String(200, "pong") }) // automatically add routers for net/http/pprof // e.g. /debug/pprof, /debug/pprof/heap, etc. ginpprof.Wrap(router) // ginpprof also plays well with *gin.RouterGroup // group := router.Group("/debug/pprof") // ginpprof.WrapGroup(group) router.Run(":8080") } ``` Start this server, and you will see such outputs: ```text [GIN-debug] GET /ping --> main.main.func1 (3 handlers) [GIN-debug] GET /debug/pprof/ --> github.com/DeanThompson/ginpprof.IndexHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/heap --> github.com/DeanThompson/ginpprof.HeapHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/goroutine --> github.com/DeanThompson/ginpprof.GoroutineHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/block --> github.com/DeanThompson/ginpprof.BlockHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/threadcreate --> github.com/DeanThompson/ginpprof.ThreadCreateHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/cmdline --> github.com/DeanThompson/ginpprof.CmdlineHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/profile --> github.com/DeanThompson/ginpprof.ProfileHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers) [GIN-debug] POST /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/trace --> github.com/DeanThompson/ginpprof.TraceHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/mutex --> github.com/DeanThompson/ginpprof.MutexHandler.func1 (3 handlers) [GIN-debug] Listening and serving HTTP on :8080 ``` Now visit [http://127.0.0.1:8080/debug/pprof/](http://127.0.0.1:8080/debug/pprof/) and you'll see what you want. Have Fun. ================================================ FILE: example/example.go ================================================ package main import ( "github.com/gin-gonic/gin" "github.com/DeanThompson/ginpprof" ) func main() { router := gin.Default() router.GET("/ping", func(c *gin.Context) { c.String(200, "pong") }) // automatically add routers for net/http/pprof // e.g. /debug/pprof, /debug/pprof/heap, etc. ginpprof.Wrap(router) // ginpprof also plays well with *gin.RouterGroup // group := router.Group("/debug/pprof") // ginpprof.WrapGroup(group) router.Run(":8080") } ================================================ FILE: pprof.go ================================================ package ginpprof import ( "net/http/pprof" "strings" "github.com/gin-gonic/gin" ) // Wrap adds several routes from package `net/http/pprof` to *gin.Engine object func Wrap(router *gin.Engine) { WrapGroup(&router.RouterGroup) } // Wrapper make sure we are backward compatible var Wrapper = Wrap // WrapGroup adds several routes from package `net/http/pprof` to *gin.RouterGroup object func WrapGroup(router *gin.RouterGroup) { routers := []struct { Method string Path string Handler gin.HandlerFunc }{ {"GET", "/debug/pprof/", IndexHandler()}, {"GET", "/debug/pprof/heap", HeapHandler()}, {"GET", "/debug/pprof/goroutine", GoroutineHandler()}, {"GET", "/debug/pprof/allocs", AllocsHandler()}, {"GET", "/debug/pprof/block", BlockHandler()}, {"GET", "/debug/pprof/threadcreate", ThreadCreateHandler()}, {"GET", "/debug/pprof/cmdline", CmdlineHandler()}, {"GET", "/debug/pprof/profile", ProfileHandler()}, {"GET", "/debug/pprof/symbol", SymbolHandler()}, {"POST", "/debug/pprof/symbol", SymbolHandler()}, {"GET", "/debug/pprof/trace", TraceHandler()}, {"GET", "/debug/pprof/mutex", MutexHandler()}, } basePath := strings.TrimSuffix(router.BasePath(), "/") var prefix string switch { case basePath == "": prefix = "" case strings.HasSuffix(basePath, "/debug"): prefix = "/debug" case strings.HasSuffix(basePath, "/debug/pprof"): prefix = "/debug/pprof" } for _, r := range routers { router.Handle(r.Method, strings.TrimPrefix(r.Path, prefix), r.Handler) } } // IndexHandler will pass the call from /debug/pprof to pprof func IndexHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Index(ctx.Writer, ctx.Request) } } // HeapHandler will pass the call from /debug/pprof/heap to pprof func HeapHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Handler("heap").ServeHTTP(ctx.Writer, ctx.Request) } } // GoroutineHandler will pass the call from /debug/pprof/goroutine to pprof func GoroutineHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Handler("goroutine").ServeHTTP(ctx.Writer, ctx.Request) } } // AllocsHandler will pass the call from /debug/pprof/allocs to pprof func AllocsHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Handler("allocs").ServeHTTP(ctx.Writer, ctx.Request) } } // BlockHandler will pass the call from /debug/pprof/block to pprof func BlockHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Handler("block").ServeHTTP(ctx.Writer, ctx.Request) } } // ThreadCreateHandler will pass the call from /debug/pprof/threadcreate to pprof func ThreadCreateHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Handler("threadcreate").ServeHTTP(ctx.Writer, ctx.Request) } } // CmdlineHandler will pass the call from /debug/pprof/cmdline to pprof func CmdlineHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Cmdline(ctx.Writer, ctx.Request) } } // ProfileHandler will pass the call from /debug/pprof/profile to pprof func ProfileHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Profile(ctx.Writer, ctx.Request) } } // SymbolHandler will pass the call from /debug/pprof/symbol to pprof func SymbolHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Symbol(ctx.Writer, ctx.Request) } } // TraceHandler will pass the call from /debug/pprof/trace to pprof func TraceHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Trace(ctx.Writer, ctx.Request) } } // MutexHandler will pass the call from /debug/pprof/mutex to pprof func MutexHandler() gin.HandlerFunc { return func(ctx *gin.Context) { pprof.Handler("mutex").ServeHTTP(ctx.Writer, ctx.Request) } } ================================================ FILE: pprof_test.go ================================================ package ginpprof import ( "strings" "testing" "github.com/gin-gonic/gin" ) func newServer() *gin.Engine { r := gin.Default() return r } func checkRouters(routers gin.RoutesInfo, t *testing.T) { expectedRouters := map[string]string{ "/debug/pprof/": "IndexHandler", "/debug/pprof/heap": "HeapHandler", "/debug/pprof/goroutine": "GoroutineHandler", "/debug/pprof/allocs": "AllocsHandler", "/debug/pprof/block": "BlockHandler", "/debug/pprof/threadcreate": "ThreadCreateHandler", "/debug/pprof/cmdline": "CmdlineHandler", "/debug/pprof/profile": "ProfileHandler", "/debug/pprof/symbol": "SymbolHandler", "/debug/pprof/trace": "TraceHandler", "/debug/pprof/mutex": "MutexHandler", } for _, router := range routers { //fmt.Println(router.Path, router.Method, router.Handler) name, ok := expectedRouters[router.Path] if !ok { t.Errorf("missing router %s", router.Path) } if !strings.Contains(router.Handler, name) { t.Errorf("handler for %s should contain %s, got %s", router.Path, name, router.Handler) } } } func TestWrap(t *testing.T) { r := newServer() Wrap(r) checkRouters(r.Routes(), t) } func TestWrapGroup(t *testing.T) { for _, prefix := range []string{"/debug", "/debug/", "/debug/pprof", "/debug/pprof/"} { r := newServer() g := r.Group(prefix) WrapGroup(g) checkRouters(r.Routes(), t) } }