I have a simple golang program:
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os/exec"
)
func bar() {
// wait 5 seconds
cmd := exec.Command("sleep", "5")
err := cmd.Run()
if err != nil {
panic(err)
}
}
func fooHandler(w http.ResponseWriter, r *http.Request) {
bar()
fmt.Fprintf(w, "Hello, world!")
}
func main() {
http.HandleFunc("/foo", fooHandler)
http.ListenAndServe(":8080", nil)
}
All I want to do is using golang's trace feature to pinpoint the function causing the 5 second delay. However, I can't figure out how to do this.
I've run
curl -o trace.out http://localhost:8080/debug/pprof/trace?seconds=20
And then hit the /foo endpoint. That generates me a trace.out which I can view with go tool trace trace.out. I can see this:
And indeed around the 9 second mark I can see in the visualizer that a thread took 5 seconds to complete, but I cannot seem to find how to get a stack trace or something that could somehow point me to the fact that bar() is the culprit using up all the time. Am I using the wrong tool here?




You have a couple options:
runtime/tracegolang.org/x/net/traceand use the real-time visualizerOption 1 is best if you want to use the de facto
go tool traceor/dominikh/gotraceuimethods of digging into traces.Option 2 is quick to start but I've not used it outside of sample programs. Here's how we can bolt it onto your example:
After issuing a request to
/foo, we can openlocalhost:8080/debug/requestsin the browser to see the following: