|
| 1 | +// Package rdb provide an `http.Handler` that can be registered on an |
| 2 | +// `http.ServeMux` of your choice to access remote profiling. |
| 3 | +// |
| 4 | +// rdb provides some advantages over the `/net/http/pprof` and |
| 5 | +// `/runtime/pprof` packages: |
| 6 | +// 1. You can register the handler under an arbitrary route-prefix. |
| 7 | +// A use-case might be to have a secret endpoint for keeping this |
| 8 | +// information hidden from prying eyes on production boxes; |
| 9 | +// 2. It pulls together all the handlers from `/net/http/pprof` and |
| 10 | +// `runtime/pprof` into a single index page, for when you can't |
| 11 | +// quite remember the URL for the profile you want; and |
| 12 | +// 3. You can register the handlers onto `http.ServeMux`'s that |
| 13 | +// aren't `http.DefaultServeMux`. |
| 14 | +// |
| 15 | +// |
| 16 | +// The simplest integration of `rdb` looks like: |
| 17 | +// |
| 18 | +// package main |
| 19 | +// |
| 20 | +// import ( |
| 21 | +// "log" |
| 22 | +// "net/http" |
| 23 | +// |
| 24 | +// "github.com/e-dard/remote-debug" |
| 25 | +// ) |
| 26 | +// |
| 27 | +// func main() { |
| 28 | +// r := http.NewServeMux() |
| 29 | +// rdb.Register("/some-prefix/", r) |
| 30 | +// if err := http.ListenAndServe(":8080", r); err != nil { |
| 31 | +// log.Fatal(err) |
| 32 | +// } |
| 33 | +// } |
| 34 | +// |
| 35 | +package rdb |
| 36 | + |
| 37 | +import ( |
| 38 | + "net/http" |
| 39 | + nhpprof "net/http/pprof" |
| 40 | + "runtime/pprof" |
| 41 | + "strings" |
| 42 | + "text/template" |
| 43 | +) |
| 44 | + |
| 45 | +// Register registers all the available handlers in `/net/http/pprof` on |
| 46 | +// the provided `http.ServeMux`, ensuring that the routes are prefixed |
| 47 | +// with the provided prefix argument. |
| 48 | +// |
| 49 | +// The provided prefix needs to have a trailing slash. The full list of |
| 50 | +// routes registered can be seen by visiting the index page. |
| 51 | +func Register(prefix string, mux *http.ServeMux) { |
| 52 | + tmpInfo := struct { |
| 53 | + Profiles []*pprof.Profile |
| 54 | + Info []string |
| 55 | + Prefix string |
| 56 | + }{ |
| 57 | + |
| 58 | + Profiles: pprof.Profiles(), |
| 59 | + Info: []string{"cmdline", "symbol"}, |
| 60 | + Prefix: prefix, |
| 61 | + } |
| 62 | + |
| 63 | + h := func(w http.ResponseWriter, r *http.Request) { |
| 64 | + name := strings.TrimPrefix(r.URL.Path, prefix+"debug/pprof/") |
| 65 | + switch name { |
| 66 | + case "": |
| 67 | + // Index page. |
| 68 | + indexTmpl.Execute(w, tmpInfo) |
| 69 | + case "cmdline": |
| 70 | + nhpprof.Cmdline(w, r) |
| 71 | + case "profile": |
| 72 | + nhpprof.Profile(w, r) |
| 73 | + case "symbol": |
| 74 | + nhpprof.Symbol(w, r) |
| 75 | + default: |
| 76 | + // Provides access to all profiles under runtime/pprof |
| 77 | + nhpprof.Handler(name).ServeHTTP(w, r) |
| 78 | + } |
| 79 | + } |
| 80 | + mux.HandleFunc(prefix, h) |
| 81 | +} |
| 82 | + |
| 83 | +var indexTmpl = template.Must(template.New("index").Parse(`<html> |
| 84 | + <head> |
| 85 | + <title>{{.Prefix}}debug/pprof/</title> |
| 86 | + </head> |
| 87 | + {{.Prefix}}debug/pprof/<br> |
| 88 | + <br> |
| 89 | + <body> |
| 90 | + profiles:<br> |
| 91 | + <table> |
| 92 | + {{range .Profiles}} |
| 93 | + <tr><td align=right>{{.Count}}<td><a href="{{$.Prefix}}debug/pprof/{{.Name}}?debug=1">{{.Name}}</a> |
| 94 | + {{end}} |
| 95 | + <tr><td align=right><td><a href="{{$.Prefix}}debug/pprof/profile">CPU</a> |
| 96 | + </table> |
| 97 | + <br> |
| 98 | + debug information:<br> |
| 99 | + <table> |
| 100 | + {{range .Info}} |
| 101 | + <tr><td align=right><td><a href="{{$.Prefix}}debug/pprof/{{.}}">{{.}}</a> |
| 102 | + {{end}} |
| 103 | + <tr><td align=right><td><a href="{{.Prefix}}debug/pprof/goroutine?debug=2">full goroutine stack dump</a><br> |
| 104 | + <table> |
| 105 | + </body> |
| 106 | +</html>`)) |
0 commit comments