in service/service.go [89:131]
func BeforeRun() {
logger := logp.NewLogger("service")
if withCPUProfile() {
cpuOut, err := os.Create(*cpuprofile)
if err != nil {
logger.Errorf("Failed to create CPU profile: %v", err)
os.Exit(1)
}
err = pprof.StartCPUProfile(cpuOut)
if err != nil {
logger.Errorf("Failed to start CPU profiler: %v", err)
os.Exit(1)
}
}
if *httpprof == "" {
return
}
logger.Info("Start pprof endpoint")
mux := http.NewServeMux()
// Register pprof handler
mux.HandleFunc("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) {
http.DefaultServeMux.ServeHTTP(w, r)
})
// Register metrics handler
mux.HandleFunc("/debug/vars", metricsHandler)
// Ensure we are listening before returning
listener, err := net.Listen("tcp", *httpprof)
if err != nil {
logger.Errorf("Failed to start pprof listener: %v", err)
os.Exit(1)
}
go func() {
// Serve returns always a non-nil error
err := http.Serve(listener, mux)
logger.Infof("Finished pprof endpoint: %v", err)
}()
}