Last active
May 22, 2017 16:29
-
-
Save hackintoshrao/1c502d09075b87fc92fefef026dbf88a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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