From 1ad96f6daf3ad62e9514d557ae896e9137e5a65d Mon Sep 17 00:00:00 2001 From: Andy Xie Date: Sat, 9 Sep 2017 15:08:17 +0800 Subject: [PATCH] add pprof --- main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d5f55fe13e..ebcedfa85b 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,8 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" + "net/http/pprof" + "k8s.io/kube-state-metrics/collectors" ) @@ -252,15 +254,24 @@ func metricsServer(registry prometheus.Gatherer, port int) { listenAddress := fmt.Sprintf(":%d", port) glog.Infof("Starting metrics server: %s", listenAddress) + + mux := http.NewServeMux() + + mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index)) + mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline)) + mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile)) + mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol)) + mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace)) + // Add metricsPath - http.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) + mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) // Add healthzPath - http.HandleFunc(healthzPath, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc(healthzPath, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Write([]byte("ok")) }) // Add index - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` Kube Metrics Server @@ -272,7 +283,7 @@ func metricsServer(registry prometheus.Gatherer, port int) { `)) }) - log.Fatal(http.ListenAndServe(listenAddress, nil)) + log.Fatal(http.ListenAndServe(listenAddress, mux)) } // registerCollectors creates and starts informers and initializes and