Skip to content

Commit

Permalink
Add pprofhandler, fix valyala#235
Browse files Browse the repository at this point in the history
Similar to expvarhandler but for net/http/pprof
  • Loading branch information
erikdubbelboer committed Apr 3, 2017
1 parent bcb67ca commit 45018c4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pprofhandler/pprof.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

1 comment on commit 45018c4

@nikunjy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @erikdubbelboer

Please sign in to comment.