julienschmidt / httprouter

A high performance HTTP request router that scales well
https://pkg.go.dev/github.com/julienschmidt/httprouter
BSD 3-Clause "New" or "Revised" License
16.56k stars 1.47k forks source link

pprof issue with httprouter #236

Open Vikash082 opened 6 years ago

Vikash082 commented 6 years ago

I am using httprouter for my server. I have registered the go profilers like below:

router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
router.Handler(http.MethodGet, "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handler(http.MethodGet, "/debug/pprof/block", pprof.Handler("block"))

For /pprof/profile, I get below output:

go tool pprof  http://localhost:8000/debug/pprof/profile
Fetching profile from http://localhost:8000/debug/pprof/profile
Please wait... (30s)
http fetch http://localhost:8000/debug/pprof/profile: Get http://localhost:8000/debug/pprof/profile: EOF

However, for heap profiling I get output like this

Fetching profile from http://localhost:8000/debug/pprof/heap
Saved profile in /home/xxxx/pprof/pprof.main.localhost:8000.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) 
(pprof) 
(pprof) 
(pprof) 
(pprof) 
(pprof) top
1451.43kB of 1451.43kB total (  100%)
      flat  flat%   sum%        cum   cum%
  902.59kB 62.19% 62.19%  1451.43kB   100%  compress/flate.NewWriter
  548.84kB 37.81%   100%   548.84kB 37.81%  compress/flate.(*compressor).init
         0     0%   100%  1451.43kB   100%  compress/gzip.(*Writer).Write
         0     0%   100%  1451.43kB   100%  internal/pprof/profile.(*Profile).Write
         0     0%   100%  1451.43kB   100%  runtime.goexit
         0     0%   100%  1451.43kB   100%  runtime/pprof.profileWriter

For symbol,

go tool pprof  http://localhost:8000/debug/pprof/symbol 
Fetching profile from http://localhost:8000/debug/pprof/symbol
parsing profile: unrecognized profile format

Is there something need to be done to correctly profile my server ?

Coccodrillo commented 6 years ago

@Vikash082 This happened to me and it was because conflicting read timeouts - you can check a longer description of it here and see it that helps?

Vikash082 commented 6 years ago

@Coccodrillo I changed the write timeout to 31, as suggested in the link you posted. But, it says profile is empty

(pprof) tree
profile is empty
(pprof) 
mihaitodor commented 6 years ago

Slightly off topic: I believe you can avoid hardcoding all those handlers by using this: router.Handler(http.MethodGet, "/debug/pprof/:item", http.DefaultServeMux) (where :item serves as a dummy placeholder variable which pprof will ignore).

Update (2020): The above suggestion doesn't seem to work any more with the latest version of this package. I was, however, able to get it to work again like so: router.Handler(http.MethodGet, "/debug/pprof/*item", http.DefaultServeMux).