Skip to content

Instantly share code, notes, and snippets.

@hackintoshrao
Last active May 22, 2017 16:29
Show Gist options
  • Select an option

  • Save hackintoshrao/1c502d09075b87fc92fefef026dbf88a to your computer and use it in GitHub Desktop.

Select an option

Save hackintoshrao/1c502d09075b87fc92fefef026dbf88a to your computer and use it in GitHub Desktop.
import (
"github.com/gorilla/mux"
"net/http/pprof"
"runtime/debug"
"runtime"
runtimepprof "runtime/pprof"
)
const (
debugPath = "/debug"
)
func RegisterDebugInfo(router *mux.Router, prefixRoute string) {
subRoute := router.NewRoute().PathPrefix(prefixRoute + debugPath ).Subrouter()
subRoute.HandleFunc("/pprof/heap",getHeapProfile)
subRoute.HandleFunc("/pprof/cmdline",pprof.Cmdline)
subRoute.HandleFunc("/pprof/profile",pprof.Profile)
subRoute.HandleFunc("/goroutines",getGoroutinesCountHandler)
subRoute.HandleFunc("/stacktrace",getStackTraceHandler)
}
// get the count of number of go routines in the system.
func countGoRoutines() int {
return runtime.NumGoroutine()
}
func getGoroutinesCountHandler(w http.ResponseWriter, r *http.Request) {
// Get the count of number of go routines running.
count := countGoRoutines()
w.Write([]byte(strconv.Itoa(count)))
}
func getHeapProfile(w http.ResponseWriter, r *http.Request) {
runtimepprof.WriteHeapProfile(w)
}
func getStackTraceHandler(w http.ResponseWriter, r *http.Request) {
stack := debug.Stack()
w.Write(stack)
runtimepprof.Lookup("goroutine").WriteTo(w, 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment