Browse code

Wire in pprof handlers.

Based on http://stackoverflow.com/questions/19591065/profiling-go-web-application-built-with-gorillas-mux-with-net-http-pprof

Paul Nasrat authored on 2013/11/20 04:25:17
Showing 1 changed files
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	"mime"
16 16
 	"net"
17 17
 	"net/http"
18
+	"net/http/pprof"
18 19
 	"os"
19 20
 	"os/exec"
20 21
 	"regexp"
... ...
@@ -1037,9 +1038,21 @@ func makeHttpHandler(srv *Server, logging bool, localMethod string, localRoute s
1037 1037
 	}
1038 1038
 }
1039 1039
 
1040
+func AttachProfiler(router *mux.Router) {
1041
+	router.HandleFunc("/debug/pprof/", pprof.Index)
1042
+	router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
1043
+	router.HandleFunc("/debug/pprof/profile", pprof.Profile)
1044
+	router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
1045
+	router.HandleFunc("/debug/pprof/heap", pprof.Handler("heap").ServeHTTP)
1046
+	router.HandleFunc("/debug/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
1047
+	router.HandleFunc("/debug/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
1048
+}
1049
+
1040 1050
 func createRouter(srv *Server, logging bool) (*mux.Router, error) {
1041 1051
 	r := mux.NewRouter()
1042
-
1052
+	if os.Getenv("DEBUG") != "" {
1053
+		AttachProfiler(r)
1054
+	}
1043 1055
 	m := map[string]map[string]HttpApiFunc{
1044 1056
 		"GET": {
1045 1057
 			"/events":                         getEvents,