From c54f9d37588f214f0d5abdabc4eccb1bd1c66263 Mon Sep 17 00:00:00 2001 From: Lei Xue Date: Fri, 4 Dec 2015 18:35:05 +0800 Subject: [PATCH] make profiling tunable --- cadvisor.go | 15 ++++++++++++--- docs/runtime_options.md | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cadvisor.go b/cadvisor.go index 08c4751f..7503be3e 100644 --- a/cadvisor.go +++ b/cadvisor.go @@ -18,7 +18,7 @@ import ( "flag" "fmt" "net/http" - _ "net/http/pprof" + "net/http/pprof" "os" "os/signal" "runtime" @@ -50,6 +50,8 @@ var prometheusEndpoint = flag.String("prometheus_endpoint", "/metrics", "Endpoin var maxHousekeepingInterval = flag.Duration("max_housekeeping_interval", 60*time.Second, "Largest interval to allow between container housekeepings") var allowDynamicHousekeeping = flag.Bool("allow_dynamic_housekeeping", true, "Whether to allow the housekeeping interval to be dynamic") +var enableProfiling = flag.Bool("profiling", false, "Enable profiling via web interface host:port/debug/pprof/") + func main() { defer glog.Flush() flag.Parse() @@ -76,7 +78,14 @@ func main() { glog.Fatalf("Failed to create a Container Manager: %s", err) } - mux := http.DefaultServeMux + mux := http.NewServeMux() + + if *enableProfiling { + 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) + } // Register all HTTP handlers. err = cadvisorhttp.RegisterHandlers(mux, containerManager, *httpAuthFile, *httpAuthRealm, *httpDigestFile, *httpDigestRealm) @@ -97,7 +106,7 @@ func main() { glog.Infof("Starting cAdvisor version: %s-%s on port %d", version.Info["version"], version.Info["revision"], *argPort) addr := fmt.Sprintf("%s:%d", *argIp, *argPort) - glog.Fatal(http.ListenAndServe(addr, nil)) + glog.Fatal(http.ListenAndServe(addr, mux)) } func setMaxProcs() { diff --git a/docs/runtime_options.md b/docs/runtime_options.md index 3a332cb8..ae07cee4 100644 --- a/docs/runtime_options.md +++ b/docs/runtime_options.md @@ -62,6 +62,7 @@ cAdvisor-native flags that help in debugging: ``` --log_cadvisor_usage=false: Whether to log the usage of the cAdvisor container --version=false: print cAdvisor version and exit +--profiling=false: Enable profiling via web interface host:port/debug/pprof/ ``` From [glog](https://github.com/golang/glog) here are some flags we find useful: