frostfs-http-gw/pprof.go

44 lines
1.1 KiB
Go
Raw Normal View History

2020-02-28 16:56:50 +00:00
package main
import (
"net/http/pprof"
rtp "runtime/pprof"
2020-03-03 10:35:06 +00:00
"github.com/fasthttp/router"
2020-02-28 16:56:50 +00:00
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
2020-03-03 10:35:06 +00:00
func attachProfiler(r *router.Router) {
r.GET("/debug/pprof/", pprofHandler())
2020-05-12 08:20:00 +00:00
r.GET("/debug/pprof/{name}/", pprofHandler())
2020-03-03 10:35:06 +00:00
}
2020-02-28 16:56:50 +00:00
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)
}
}