0

I'm quite puzzled by a problem that I can't profile my golang program, I have all other endpoints under /debug/pprof but not /debug/pprof/profile for CPU profiling Have anyone ever stumbled across such issue?

go tool pprof http://localhost:7778/debug/pprof/profile
Fetching profile from http://localhost:7778/debug/pprof/profile
Please wait... (30s)
server response: 404 Not Found

while

/debug/pprof/

profiles:
19  block
31  goroutine
10  heap
0   mutex
11  threadcreate

full goroutine stack dump

I setup profiling in this way

r := http.NewServeMux()
r.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))

What can be the cause?

Updated: running

http.ListenAndServe("localhost:4444", nil)

(i.e. starting default http server) fixes the problem for my custom endpoint

let4be
  • 1,048
  • 11
  • 30

2 Answers2

3

I stumbled the same error as you, I had registered all according to the documentation. In case anyone faced the same problem, it turned out that you need to add http.DefaultServeMux, so the complete lines should be:

  r := mux.NewRouter()

  r.PathPrefix("/debug/").Handler(http.DefaultServeMux)

  r.HandleFunc("/debug/pprof/", pprof.Index)
  r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  r.HandleFunc("/debug/pprof/profile", pprof.Profile)
  r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  r.HandleFunc("/debug/pprof/trace", pprof.Trace)

reference is here

Bondhan Novandy
  • 362
  • 1
  • 4
  • 16
1

Found it, I didn't register all the handlers as done in init here https://golang.org/src/net/http/pprof/pprof.go

let4be
  • 1,048
  • 11
  • 30