Created
February 2, 2021 08:30
-
-
Save migue/e2052e5c22d557ddcae3064336b66235 to your computer and use it in GitHub Desktop.
Go profiling server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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