go语言pprof插入到http服务 - Wed, Apr 7, 2021
go语言pprof插入到http服务
代码很简单
// Pprof pprof check
func Pprof(handler http.Handler) http.Handler {
// return http.FileServer(assetFS())
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Index(r.URL.Path, "/debug/pprof/") == 0 {
path := strings.TrimPrefix(r.URL.Path, "/debug/pprof/")
switch path {
case "":
pprof.Index(w, r)
case "cmdline":
pprof.Cmdline(w, r)
case "profile":
pprof.Profile(w, r)
case "symbol":
pprof.Symbol(w, r)
case "allocs":
pprof.Handler("allocs").ServeHTTP(w, r)
case "block":
pprof.Handler("block").ServeHTTP(w, r)
case "goroutine":
pprof.Handler("goroutine").ServeHTTP(w, r)
case "heap":
pprof.Handler("heap").ServeHTTP(w, r)
case "mutex":
pprof.Handler("mutex").ServeHTTP(w, r)
case "threadcreate":
pprof.Handler("threadcreate").ServeHTTP(w, r)
case "trace":
pprof.Handler("trace").ServeHTTP(w, r)
default:
pprof.Index(w, r)
}
return
}
handler.ServeHTTP(w, r)
})
}