Skip to content

Instantly share code, notes, and snippets.

@jwhonce
Created March 22, 2023 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwhonce/c0d75530f50fc45f5e3c94b0b3b11d80 to your computer and use it in GitHub Desktop.
Save jwhonce/c0d75530f50fc45f5e3c94b0b3b11d80 to your computer and use it in GitHub Desktop.
Podman API pprof endpoint
// setupPprof enables pprof default endpoints
// Note: These endpoints and the podman flag --cpu-profile are mutually exclusive
//
// Examples:
// #1 go tool pprof -http localhost:8889 localhost:8888/debug/pprof/heap?seconds=120
// Note: web page will only render after a sample has been recorded
// #2 curl http://localhost:8888/debug/pprof/heap > heap.pprof && go tool pprof heap.pprof
func (s *APIServer) setupPprof() {
if s.PProfAddr == "" {
return
}
logrus.Infof("pprof service listening on %q", s.PProfAddr)
go func() {
old := runtime.SetMutexProfileFraction(1)
defer runtime.SetMutexProfileFraction(old)
runtime.SetBlockProfileRate(1)
defer runtime.SetBlockProfileRate(0)
router := mux.NewRouter()
router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
err := http.ListenAndServe(s.PProfAddr, router)
if err != nil && err != http.ErrServerClosed {
logrus.Warnf("pprof service failed: %v", err)
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment