I generate a trace like this:
func main() {
f, err := os.Create("trace.out")
if err != nil {
panic(err)
}
defer f.Close()
err = trace.Start(f)
if err != nil {
panic(err)
}
defer trace.Stop()
//this is my app:
http.HandleFunc("/", someFunc)
log.Fatal(http.ListenAndServe(":5000", nil))
}
Then i run in the CLI:
$ go run main.go
Refresh browser, trace.out is generated, 1.8 MB, then:
$ go tool trace trace.out
018/09/09 13:25:18 Parsing trace...
failed to parse trace: no EvFrequency event
What am I missing here? Thanks.
Trace data can only be viewed after you stopped the trace (i.e. after trace.Stop()
has been called). In the code you supplied http.ListenAndServer(...)
will block forever (unless it runs into an error).
Are you trying to view the trace before the trace has been stopped?
One solution might be to wait for an interrupt signal and then exit the function when received which would cause the tracing to be stopped and written.
func main() {
f, err := os.Create("trace.out")
if err != nil {
panic(err)
}
defer f.Close()
err = trace.Start(f)
if err != nil {
panic(err)
}
defer trace.Stop()
http.HandleFunc("/", someFunc)
go func() {
log.Fatal(http.ListenAndServe(":5000", nil))
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
<-signalChan
}