Skip to content

Instantly share code, notes, and snippets.

@migue
Created February 2, 2021 08:30
Show Gist options
  • Save migue/e2052e5c22d557ddcae3064336b66235 to your computer and use it in GitHub Desktop.
Save migue/e2052e5c22d557ddcae3064336b66235 to your computer and use it in GitHub Desktop.
Go profiling server
func SetupProfiler() (*http.Server, chan error, error) {
profilerRouter := chi.NewRouter()
profilerRouter.Mount("/profile", Profiler())
server := http.Server{
Addr: fmt.Sprintf(":%v", 8080),
Handler: profilerRouter,
}
errChannel := make(chan error)
go func() {
err := http.ListenAndServe(":8080", profilerRouter)
if err != nil {
deltago.Error("Error running the Profiler HTTP Server: %+v", err)
errChannel <- err
}
}()
return &server, errChannel, nil
}
func Profiler() http.Handler {
r := NewRouter()
r.Use(NoCache)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.RequestURI + "/pprof/", 301)
})
r.HandleFunc("/pprof", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.RequestURI + "/", 301)
})
r.HandleFunc("/pprof/", pprof.Index)
r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/pprof/profile", pprof.Profile)
r.HandleFunc("/pprof/symbol", pprof.Symbol)
r.Handle("/pprof/block", pprof.Handler("block"))
r.Handle("/pprof/heap", pprof.Handler("heap"))
r.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
r.Handle("/pprof/threadcreate", pprof.Handler("threadcreate"))
r.HandleFunc("/vars", expVars)
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment