From 074e62ddb04009e267daf5f25b3538b568602826 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Tue, 14 Mar 2017 05:31:42 +0000 Subject: [PATCH] Add pprofhandler, fix #235 Similar to expvarhandler but for net/http/pprof --- pprofhandler/pprof.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 pprofhandler/pprof.go diff --git a/pprofhandler/pprof.go b/pprofhandler/pprof.go new file mode 100644 index 0000000..200ae51 --- /dev/null +++ b/pprofhandler/pprof.go @@ -0,0 +1,35 @@ +package pprofhandler + +import ( + "net/http/pprof" + "strings" + + "github.com/valyala/fasthttp" + "github.com/valyala/fasthttp/fasthttpadaptor" +) + +var ( + cmdline = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline) + profile = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile) + symbol = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol) + trace = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace) + index = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index) +) + +// PprofHandler serves server runtime profiling data in the format expected by the pprof visualization tool. +// +// See https://golang.org/pkg/net/http/pprof/ for details. +func PprofHandler(ctx *fasthttp.RequestCtx) { + ctx.Response.Header.Set("Content-Type", "text/html") + if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/cmdline") { + cmdline(ctx) + } else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/profile") { + profile(ctx) + } else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/symbol") { + symbol(ctx) + } else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/trace") { + trace(ctx) + } else { + index(ctx) + } +}