go语言pprof插入到http服务

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)
    })
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

💰

×

Help us with donation