Documentation ¶
Overview ¶
pprofhijackmiddleware provides pprof cpu/mem profile gathering as a net/http middleware.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CPUProfile ¶
CPUProfile gathers a pprof CPU profile during the lifecycle of the passed HTTP request handler. It hijacks the http.ResponseWriter to return a pprof compatible gzipped file as the HTTP response body.
Example ¶
package main import ( "fmt" "log" "net/http" pprofhijackmiddleware "github.com/miguelcnf/go-pprof-hijack-middleware" ) var ( customHandler = func(w http.ResponseWriter, r *http.Request) {} helloHandler = func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprintf(w, "hello") } ) func main() { mux := http.NewServeMux() // regular handler, not hijacked mux.Handle("/hello", http.HandlerFunc(helloHandler)) // hijack a single handler cpuProfileHijackHandler := pprofhijackmiddleware.CPUProfile(http.HandlerFunc(customHandler)) mux.Handle("/custom", cpuProfileHijackHandler) err := http.ListenAndServe(":8080", mux) if err != http.ErrServerClosed { log.Fatalf("server closed unexpectedly: %v", err) } }
Output:
func MemProfile ¶
MemProfile gathers a pprof Heap profile after the HTTP request handler is executed and a forced GC is executed. It hijacks the http.ResponseWriter to return a pprof compatible gzipped file as the HTTP response body.
Example ¶
package main import ( "fmt" "log" "net/http" pprofhijackmiddleware "github.com/miguelcnf/go-pprof-hijack-middleware" ) var ( customHandler = func(w http.ResponseWriter, r *http.Request) {} helloHandler = func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprintf(w, "hello") } ) func main() { mux := http.NewServeMux() mux.Handle("/hello", http.HandlerFunc(helloHandler)) mux.Handle("/custom", http.HandlerFunc(customHandler)) // hijack all registered handlers err := http.ListenAndServe(":8080", pprofhijackmiddleware.MemProfile(mux)) if err != http.ErrServerClosed { log.Fatalf("server closed unexpectedly: %v", err) } }
Output:
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.