This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
/
debug.go
75 lines (68 loc) · 1.6 KB
/
debug.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
package httphandlers
import (
"fmt"
"log"
"net/http"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"text/template"
)
// DebugIndexHandler serves pprof profiles under a defined Path.
// Path must end with the slash "/".
type DebugIndexHandler struct {
Path string
}
// ServeHTTP serves http request.
func (h DebugIndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, h.Path) {
name := strings.TrimPrefix(r.URL.Path, h.Path)
if name != "" {
debugHandler(name).ServeHTTP(w, r)
return
}
}
profiles := pprof.Profiles()
if err := debugIndexTmpl.Execute(w, struct {
Profiles []*pprof.Profile
Path string
}{
Profiles: profiles,
Path: h.Path,
}); err != nil {
log.Printf("debug intex handler: %s", err)
}
}
var debugIndexTmpl = template.Must(template.New("index").Parse(`<html>
<head>
<title>{{.Path}}</title>
</head>
<body>
profiles:<br>
<table>
{{range .Profiles}}
<tr><td align=right>{{.Count}}<td><a href="{{$.Path}}{{.Name}}?debug=1">{{.Name}}</a>
{{end}}
</table>
<br>
<a href="{{.Path}}goroutine?debug=2">full goroutine stack dump</a><br>
</body>
</html>`))
type debugHandler string
func (name debugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
debug, _ := strconv.Atoi(r.FormValue("debug"))
p := pprof.Lookup(string(name))
if p == nil {
w.WriteHeader(404)
fmt.Fprintf(w, "Unknown profile: %s\n", name)
return
}
gc, _ := strconv.Atoi(r.FormValue("gc"))
if name == "heap" && gc > 0 {
runtime.GC()
}
p.WriteTo(w, debug)
return
}