-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from changsongl/develop
Fix fetch interval bug and add pprof
- Loading branch information
Showing
9 changed files
with
153 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ go.sum | |
|
||
# IDE and OS | ||
.idea | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package server | ||
|
||
import ( | ||
"net/http/pprof" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// WrapPProf Wrap adds several routes from package `net/http/pprof` to *gin.Engine object | ||
func WrapPProf(router *gin.Engine) { | ||
WrapGroup(&router.RouterGroup) | ||
} | ||
|
||
// 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters