add fasthttp pprof handler
This commit is contained in:
parent
c6a06ca709
commit
437133e280
1 changed files with 37 additions and 0 deletions
37
pprof.go
Normal file
37
pprof.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http/pprof"
|
||||
rtp "runtime/pprof"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fasthttp/fasthttpadaptor"
|
||||
)
|
||||
|
||||
func pprofHandler() fasthttp.RequestHandler {
|
||||
items := rtp.Profiles()
|
||||
|
||||
profiles := map[string]fasthttp.RequestHandler{
|
||||
"": fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index),
|
||||
"cmdline": fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline),
|
||||
"profile": fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile),
|
||||
"symbol": fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol),
|
||||
"trace": fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace),
|
||||
}
|
||||
|
||||
for i := range items {
|
||||
name := items[i].Name()
|
||||
profiles[name] = fasthttpadaptor.NewFastHTTPHandler(pprof.Handler(name))
|
||||
}
|
||||
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
name, _ := ctx.UserValue("name").(string)
|
||||
|
||||
if handler, ok := profiles[name]; ok {
|
||||
handler(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Error("Not found", fasthttp.StatusNotFound)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue