1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-25 02:24:24 +00:00

66 lines
2.2 KiB
Go
Raw Normal View History

2020-09-13 11:20:11 +02:00
package pprof
2020-07-23 06:06:12 +02:00
import (
"net/http/pprof"
"strings"
2020-09-13 11:20:11 +02:00
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp/fasthttpadaptor"
2020-07-23 06:06:12 +02:00
)
// Set pprof adaptors
var (
pprofIndex = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index)
pprofCmdline = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline)
pprofProfile = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile)
pprofSymbol = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol)
pprofTrace = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace)
pprofAllocs = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("allocs").ServeHTTP)
pprofBlock = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("block").ServeHTTP)
pprofGoroutine = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("goroutine").ServeHTTP)
pprofHeap = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("heap").ServeHTTP)
pprofMutex = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("mutex").ServeHTTP)
pprofThreadcreate = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("threadcreate").ServeHTTP)
)
2020-09-13 11:20:11 +02:00
// New creates a new middleware handler
func New() fiber.Handler {
// Return new handler
return func(c *fiber.Ctx) error {
2020-07-23 06:06:12 +02:00
path := c.Path()
// We are only interested in /debug/pprof routes
if len(path) < 12 || !strings.HasPrefix(path, "/debug/pprof") {
2020-09-13 11:20:11 +02:00
return c.Next()
2020-07-23 06:06:12 +02:00
}
// Switch to original path without stripped slashes
switch path {
case "/debug/pprof/":
2020-09-13 11:20:11 +02:00
pprofIndex(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/cmdline":
2020-09-13 11:20:11 +02:00
pprofCmdline(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/profile":
2020-09-13 11:20:11 +02:00
pprofProfile(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/symbol":
2020-09-13 11:20:11 +02:00
pprofSymbol(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/trace":
2020-09-13 11:20:11 +02:00
pprofTrace(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/allocs":
2020-09-13 11:20:11 +02:00
pprofAllocs(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/block":
2020-09-13 11:20:11 +02:00
pprofBlock(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/goroutine":
2020-09-13 11:20:11 +02:00
pprofGoroutine(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/heap":
2020-09-13 11:20:11 +02:00
pprofHeap(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/mutex":
2020-09-13 11:20:11 +02:00
pprofMutex(c.Context())
2020-07-23 06:06:12 +02:00
case "/debug/pprof/threadcreate":
2020-09-13 11:20:11 +02:00
pprofThreadcreate(c.Context())
2020-07-23 06:06:12 +02:00
default:
// pprof index only works with trailing slash
2020-09-13 11:20:11 +02:00
return c.Redirect("/debug/pprof/", 302)
2020-07-23 06:06:12 +02:00
}
2020-09-13 11:20:11 +02:00
return nil
2020-07-23 06:06:12 +02:00
}
}