1
0
mirror of https://github.com/golang/go synced 2024-11-14 06:30:22 -07:00

net/http/pprof: add tracing support

net/http/pprof part of tracing functionality:
https://docs.google.com/document/u/1/d/1FP5apqzBgr7ahCCgFO-yoVhk4YZrNIDNf9RybngBc14/pub
Full change:
https://codereview.appspot.com/146920043

Change-Id: I9092028fcbd5e8f97a56f2c155889ccdfb494afb
Reviewed-on: https://go-review.googlesource.com/1453
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Dmitry Vyukov 2014-12-12 18:58:09 +01:00
parent 69cd91a598
commit 690db9c89f

View File

@ -34,6 +34,10 @@
// //
// go tool pprof http://localhost:6060/debug/pprof/block // go tool pprof http://localhost:6060/debug/pprof/block
// //
// Or to collect a 5-second execution trace:
//
// wget http://localhost:6060/debug/pprof/trace?seconds=5
//
// To view all available profiles, open http://localhost:6060/debug/pprof/ // To view all available profiles, open http://localhost:6060/debug/pprof/
// in your browser. // in your browser.
// //
@ -64,6 +68,7 @@ func init() {
http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline)) http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile)) http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol)) http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
http.Handle("/debug/pprof/trace", http.HandlerFunc(Trace))
} }
// Cmdline responds with the running program's // Cmdline responds with the running program's
@ -98,6 +103,30 @@ func Profile(w http.ResponseWriter, r *http.Request) {
pprof.StopCPUProfile() pprof.StopCPUProfile()
} }
// Trace responds with the execution trace in binary form.
// Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified.
// The package initialization registers it as /debug/pprof/trace.
func Trace(w http.ResponseWriter, r *http.Request) {
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
if sec == 0 {
sec = 1
}
// Set Content Type assuming StartTrace will work,
// because if it does it starts writing.
w.Header().Set("Content-Type", "application/octet-stream")
if err := pprof.StartTrace(w); err != nil {
// StartTrace failed, so no writes yet.
// Can change header back to text content and send error code.
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not enable tracing: %s\n", err)
return
}
time.Sleep(time.Duration(sec) * time.Second)
pprof.StopTrace()
}
// Symbol looks up the program counters listed in the request, // Symbol looks up the program counters listed in the request,
// responding with a table mapping program counters to function names. // responding with a table mapping program counters to function names.
// The package initialization registers it as /debug/pprof/symbol. // The package initialization registers it as /debug/pprof/symbol.