-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpprof.go
More file actions
53 lines (42 loc) · 958 Bytes
/
pprof.go
File metadata and controls
53 lines (42 loc) · 958 Bytes
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
package pprof
import (
"net/http"
"net/http/pprof"
runtime_pprof "runtime/pprof"
"strings"
)
type handler struct {
}
var profileHandlers map[string]http.Handler
func init() {
profileHandlers = make(map[string]http.Handler)
for _, profile := range runtime_pprof.Profiles() {
profileHandlers[profile.Name()] = pprof.Handler(profile.Name())
}
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if !strings.HasPrefix(r.URL.Path, "/debug/pprof") {
next(w, r)
return
}
parts := strings.Split(r.URL.Path, "/")
if len(parts) > 4 {
next(w, r)
return
}
if len(parts) == 3 || (len(parts) == 4 && parts[3] == "") {
pprof.Index(w, r)
return
}
handler := profileHandlers[parts[3]]
if handler == nil {
next(w, r)
return
}
handler.ServeHTTP(w, r)
return
}
// Pprof returns a handler which will serve pprof data for the path /debug/pprof
func Pprof() *handler {
return &handler{}
}