diff --git a/cmd/config.go b/cmd/config.go index 588bce7b6..0e038f93d 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -16,6 +16,8 @@ import ( type config struct { URI string Log string + Metrics bool + Profile bool Levels map[string]string Interval time.Duration Mqtt provider.MqttConfig diff --git a/cmd/root.go b/cmd/root.go index 5bb27433b..0f7390e19 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,8 @@ package cmd import ( "fmt" + "net/http" + _ "net/http/pprof" // pprof handler "os" "os/signal" "syscall" @@ -11,8 +13,9 @@ import ( "github.com/andig/evcc/server/updater" "github.com/andig/evcc/util" "github.com/andig/evcc/util/pipe" - + "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -74,6 +77,20 @@ func init() { "Update interval", ) bind(rootCmd, "interval") + + rootCmd.PersistentFlags().Bool( + "metrics", + false, + "Expose metrics", + ) + bind(rootCmd, "metrics") + + rootCmd.PersistentFlags().Bool( + "profile", + false, + "Expose pprof profiles", + ) + bind(rootCmd, "profile") } // initConfig reads in config file and ENV variables if set @@ -164,7 +181,15 @@ func run(cmd *cobra.Command, args []string) { httpd := server.NewHTTPd(uri, site, socketHub, cache) // metrics - httpd.Handle("/metrics", promhttp.Handler()) + if viper.GetBool("metrics") { + httpd.Handle("/metrics", promhttp.Handler()) + } + + // pprof + if viper.GetBool("profile") { + router := httpd.Handler.(*mux.Router) + router.PathPrefix("/debug/").Handler(http.DefaultServeMux) + } // start HEMS server if conf.HEMS.Type != "" {