frostfs-http-gw/pprof.go
Denis Kirillov 6265fcb26a [#105] Add newline to errors
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
2021-11-12 15:32:18 +03:00

44 lines
1.1 KiB
Go

package main
import (
"net/http/pprof"
rtp "runtime/pprof"
"github.com/fasthttp/router"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
func attachProfiler(r *router.Router) {
r.GET("/debug/pprof/", pprofHandler())
r.GET("/debug/pprof/{name}/", pprofHandler())
}
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
}
response.Error(ctx, "Not found", fasthttp.StatusNotFound)
}
}