From 2771602e5c31b0f9bc72b74206fcccd2966da47f Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 10 Jan 2024 10:20:16 +0800 Subject: [PATCH] chore: add enable metric flag --- bin/slash/main.go | 14 ++++++++++++-- server/profile/profile.go | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bin/slash/main.go b/bin/slash/main.go index 42e9e4f..760a1c3 100644 --- a/bin/slash/main.go +++ b/bin/slash/main.go @@ -31,6 +31,7 @@ var ( data string driver string dsn string + enableMetric bool rootCmd = &cobra.Command{ Use: "slash", @@ -57,8 +58,10 @@ var ( return } - // nolint - metric.NewMetricClient(s.Secret, *serverProfile) + if serverProfile.Metric { + // nolint + metric.NewMetricClient(s.Secret, *serverProfile) + } c := make(chan os.Signal, 1) // Trigger graceful shutdown on SIGINT or SIGTERM. @@ -100,6 +103,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&data, "data", "d", "", "data directory") rootCmd.PersistentFlags().StringVarP(&driver, "driver", "", "", "database driver") rootCmd.PersistentFlags().StringVarP(&dsn, "dsn", "", "", "database source name(aka. DSN)") + rootCmd.PersistentFlags().BoolVarP(&enableMetric, "metric", "", true, "allow metric collection") err := viper.BindPFlag("mode", rootCmd.PersistentFlags().Lookup("mode")) if err != nil { @@ -121,10 +125,15 @@ func init() { if err != nil { panic(err) } + err = viper.BindPFlag("metric", rootCmd.PersistentFlags().Lookup("metric")) + if err != nil { + panic(err) + } viper.SetDefault("mode", "demo") viper.SetDefault("port", 8082) viper.SetDefault("driver", "sqlite") + viper.SetDefault("metric", true) viper.SetEnvPrefix("slash") } @@ -143,6 +152,7 @@ func initConfig() { println("port:", serverProfile.Port) println("mode:", serverProfile.Mode) println("version:", serverProfile.Version) + println("metric:", serverProfile.Metric) println("---") } diff --git a/server/profile/profile.go b/server/profile/profile.go index ddfa826..55246ec 100644 --- a/server/profile/profile.go +++ b/server/profile/profile.go @@ -28,6 +28,8 @@ type Profile struct { Driver string `json:"-"` // Version is the current version of server Version string `json:"version"` + // Metric indicate the metric collection is enabled or not + Metric bool `json:"-"` } func (p *Profile) IsDev() bool {