Skip to content

Instantly share code, notes, and snippets.

@kamal-github
Last active December 4, 2020 18:10
Show Gist options
  • Save kamal-github/1fb79184208a9434e254776f63bcd0c9 to your computer and use it in GitHub Desktop.
Save kamal-github/1fb79184208a9434e254776f63bcd0c9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
"net/http/pprof"
)
// HTTP request handler
func handler(w http.ResponseWriter, r *http.Request) {
// Does something
}
func main() {
// Create a new HTTP multiplexer
mux := http.NewServeMux()
// Register our handler for the / route
mux.HandleFunc("/", handler)
// Add the pprof routes
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
/*
goroutine - stack traces of all current goroutines
heap - a sampling of memory allocations of live objects
allocs - a sampling of all past memory allocations
threadcreate - stack traces that led to the creation of new OS threads
block - stack traces that led to blocking on synchronization primitives
mutex - stack traces of holders of contended mutexes
*/
mux.Handle("/debug/pprof/block", pprof.Handler("block"))
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
// Start listening on port 8080
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Fatal(fmt.Sprintf("Error when starting or running http server: %v", err))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment