From 437133e280a2f2cd7b5bc6ccfff3ed5283fd5f4b Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Fri, 28 Feb 2020 19:56:50 +0300 Subject: [PATCH] add fasthttp pprof handler --- pprof.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 pprof.go diff --git a/pprof.go b/pprof.go new file mode 100644 index 0000000..97b6424 --- /dev/null +++ b/pprof.go @@ -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) + } +}