|
| 1 | +package pprof |
| 2 | + |
| 3 | +import ( |
| 4 | + "expvar" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/pprof" |
| 8 | + |
| 9 | + "github.com/Sirupsen/logrus" |
| 10 | +) |
| 11 | + |
| 12 | +func Enable(address string) { |
| 13 | + http.Handle("/", http.RedirectHandler("/debug/pprof", http.StatusMovedPermanently)) |
| 14 | + |
| 15 | + http.Handle("/debug/vars", http.HandlerFunc(expVars)) |
| 16 | + http.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index)) |
| 17 | + http.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline)) |
| 18 | + http.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile)) |
| 19 | + http.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol)) |
| 20 | + http.Handle("/debug/pprof/block", pprof.Handler("block")) |
| 21 | + http.Handle("/debug/pprof/heap", pprof.Handler("heap")) |
| 22 | + http.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) |
| 23 | + http.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) |
| 24 | + |
| 25 | + go http.ListenAndServe(address, nil) |
| 26 | + logrus.Debug("pprof listening in address %s", address) |
| 27 | +} |
| 28 | + |
| 29 | +// Replicated from expvar.go as not public. |
| 30 | +func expVars(w http.ResponseWriter, r *http.Request) { |
| 31 | + first := true |
| 32 | + w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 33 | + fmt.Fprintf(w, "{\n") |
| 34 | + expvar.Do(func(kv expvar.KeyValue) { |
| 35 | + if !first { |
| 36 | + fmt.Fprintf(w, ",\n") |
| 37 | + } |
| 38 | + first = false |
| 39 | + fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value) |
| 40 | + }) |
| 41 | + fmt.Fprintf(w, "\n}\n") |
| 42 | +} |
0 commit comments