forked from hiko1129/echo-pprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprof.go
135 lines (118 loc) · 3.82 KB
/
pprof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package echopprof
import (
"net/http/pprof"
"strings"
"github.com/labstack/echo/v4"
)
// Wrap adds several routes from package `net/http/pprof` to *echo.Echo object.
func Wrap(e *echo.Echo) {
WrapGroup("", e.Group(""))
}
// Wrapper make sure we are backward compatible.
var Wrapper = Wrap
// WrapGroup adds several routes from package `net/http/pprof` to *echo.Group object.
func WrapGroup(prefix string, g *echo.Group) {
routers := []struct {
Method string
Path string
Handler echo.HandlerFunc
}{
{"GET", "/debug/pprof/", IndexHandler()},
{"GET", "/debug/pprof/allocs", AllocsHandler()},
{"GET", "/debug/pprof/heap", HeapHandler()},
{"GET", "/debug/pprof/goroutine", GoroutineHandler()},
{"GET", "/debug/pprof/block", BlockHandler()},
{"GET", "/debug/pprof/threadcreate", ThreadCreateHandler()},
{"GET", "/debug/pprof/cmdline", CmdlineHandler()},
{"GET", "/debug/pprof/profile", ProfileHandler()},
{"GET", "/debug/pprof/symbol", SymbolHandler()},
{"POST", "/debug/pprof/symbol", SymbolHandler()},
{"GET", "/debug/pprof/trace", TraceHandler()},
{"GET", "/debug/pprof/mutex", MutexHandler()},
}
for _, r := range routers {
switch r.Method {
case "GET":
g.GET(strings.TrimPrefix(r.Path, prefix), r.Handler)
case "POST":
g.POST(strings.TrimPrefix(r.Path, prefix), r.Handler)
}
}
}
// IndexHandler will pass the call from /debug/pprof to pprof.
func IndexHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Index(ctx.Response().Writer, ctx.Request())
return nil
}
}
// AllocsHandler will pass the call from /debug/pprof/allocs to pprof.
func AllocsHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("allocs").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// HeapHandler will pass the call from /debug/pprof/heap to pprof.
func HeapHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("heap").ServeHTTP(ctx.Response(), ctx.Request())
return nil
}
}
// GoroutineHandler will pass the call from /debug/pprof/goroutine to pprof.
func GoroutineHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("goroutine").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// BlockHandler will pass the call from /debug/pprof/block to pprof.
func BlockHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("block").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// ThreadCreateHandler will pass the call from /debug/pprof/threadcreate to pprof.
func ThreadCreateHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("threadcreate").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// CmdlineHandler will pass the call from /debug/pprof/cmdline to pprof.
func CmdlineHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Cmdline(ctx.Response().Writer, ctx.Request())
return nil
}
}
// ProfileHandler will pass the call from /debug/pprof/profile to pprof.
func ProfileHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Profile(ctx.Response().Writer, ctx.Request())
return nil
}
}
// SymbolHandler will pass the call from /debug/pprof/symbol to pprof.
func SymbolHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Symbol(ctx.Response().Writer, ctx.Request())
return nil
}
}
// TraceHandler will pass the call from /debug/pprof/trace to pprof.
func TraceHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Trace(ctx.Response().Writer, ctx.Request())
return nil
}
}
// MutexHandler will pass the call from /debug/pprof/mutex to pprof.
func MutexHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("mutex").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}