From 7e11e4e5a6da015552cad9f34cde483286b3c23c Mon Sep 17 00:00:00 2001 From: Kevin Petremann Date: Thu, 6 Jul 2023 00:14:08 +0200 Subject: [PATCH 1/5] tests(config): test all configuration scenarios It checks flags, config files, and the precedence. --- README.md | 2 + cmd/salt-exporter/config.go | 36 ++- cmd/salt-exporter/config_test.go | 412 ++++++++++++++++++++++++++++++ cmd/salt-exporter/config_test.yml | 40 +++ cmd/salt-exporter/main.go | 4 +- go.mod | 1 - go.sum | 8 +- 7 files changed, 474 insertions(+), 29 deletions(-) create mode 100644 cmd/salt-exporter/config_test.go create mode 100644 cmd/salt-exporter/config_test.yml diff --git a/README.md b/README.md index e1e46ef..befa521 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,8 @@ metrics: - "highstate" ``` +**Note: Except for -health-minions, all flag have the priority over the configuration file.** + ## Features Supported tags: diff --git a/cmd/salt-exporter/config.go b/cmd/salt-exporter/config.go index 0cda0ee..ad127f5 100644 --- a/cmd/salt-exporter/config.go +++ b/cmd/salt-exporter/config.go @@ -4,8 +4,9 @@ import ( "errors" "flag" "fmt" + "path/filepath" + "strings" - "github.com/k0kubun/pp/v3" "github.com/kpetremann/salt-exporter/internal/metrics" "github.com/spf13/viper" ) @@ -43,7 +44,7 @@ type Config struct { Metrics metrics.Config } -func parseFlags() { +func parseFlags() bool { // flags flag.String("log-level", defaultLogLevel, "log level (debug, info, warn, error, fatal, panic, disabled)") @@ -64,32 +65,26 @@ func parseFlags() { "[DEPRECATED] apply filter on states to monitor, separated by a comma") flag.Parse() - // ensure compatibility with deprecated health-minions flag - if !*healthMinions { - viper.SetDefault("metrics.salt_function_status.enabled", false) - viper.SetDefault("metrics.salt_responses_total.enabled", false) - } + return *healthMinions } -func setDefaults() { +func setDefaults(healthMinions bool) { viper.SetDefault("log-level", defaultLogLevel) viper.SetDefault("listen-port", defaultPort) viper.SetDefault("metrics.health-minions", defaultHealthMinion) - viper.SetDefault("metrics.salt_new_job_total.enabled", true) viper.SetDefault("metrics.salt_expected_responses_total.enabled", true) viper.SetDefault("metrics.salt_function_responses_total.enabled", true) viper.SetDefault("metrics.salt_scheduled_job_return_total.enabled", true) - viper.SetDefault("metrics.salt_responses_total.enabled", true) - viper.SetDefault("metrics.salt_function_status.enabled", true) - + viper.SetDefault("metrics.salt_function_status.enabled", healthMinions) // TODO: true once health-minions will be removed + viper.SetDefault("metrics.salt_responses_total.enabled", healthMinions) // TODO: true once health-minions will be removed viper.SetDefault("metrics.salt_function_status.filters.functions", []string{defaultHealthFunctionsFilter}) viper.SetDefault("metrics.salt_function_status.filters.states", []string{defaultHealthStatesFilter}) } -func getConfig() (Config, error) { - setDefaults() +func getConfig(configFileName string, healthMinions bool) (Config, error) { + setDefaults(healthMinions) // bind flags var allFlags []viperFlag @@ -106,8 +101,9 @@ func getConfig() (Config, error) { } // bind configuration file - viper.SetConfigName("config") - viper.SetConfigType("yml") + ext := filepath.Ext(configFileName) + viper.SetConfigName(strings.TrimSuffix(configFileName, ext)) + viper.SetConfigType(strings.TrimPrefix(ext, ".")) viper.AddConfigPath(".") err := viper.ReadInConfig() @@ -123,8 +119,6 @@ func getConfig() (Config, error) { return Config{}, fmt.Errorf("failed to load configuration: %w", err) } - pp.Println(cfg) - return cfg, nil } @@ -141,12 +135,12 @@ func checkRequirements(cfg Config) error { return nil } -func ReadConfig() (Config, error) { +func ReadConfig(configFileName string) (Config, error) { var err error - parseFlags() + healthMinions := parseFlags() - cfg, err := getConfig() + cfg, err := getConfig(configFileName, healthMinions) if err != nil { return Config{}, err } diff --git a/cmd/salt-exporter/config_test.go b/cmd/salt-exporter/config_test.go new file mode 100644 index 0000000..b7a18e9 --- /dev/null +++ b/cmd/salt-exporter/config_test.go @@ -0,0 +1,412 @@ +package main + +import ( + "flag" + "os" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/kpetremann/salt-exporter/internal/metrics" + "github.com/spf13/viper" +) + +func TestReadConfigFlagOnly(t *testing.T) { + tests := []struct { + name string + flags []string + want Config + }{ + { + name: "simple config, flags only", + flags: []string{ + "-host=127.0.0.1", + "-port=8080", + }, + want: Config{ + LogLevel: defaultLogLevel, + ListenAddress: "127.0.0.1", + ListenPort: 8080, + TLS: struct { + Enabled bool + Key string + Certificate string + }{ + Enabled: false, + Key: "", + Certificate: "", + }, + Metrics: metrics.Config{ + HealthMinions: true, + Global: struct { + Filters struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + } + }{ + Filters: struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + }{ + IgnoreTest: false, + IgnoreMock: false, + }, + }, + SaltNewJobTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltExpectedResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionResponsesTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: false, + }, + SaltScheduledJobReturnTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: false, + }, + SaltResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionStatus: struct { + Enabled bool + Filters struct { + Functions []string + States []string + } + }{ + Enabled: true, + Filters: struct { + Functions []string + States []string + }{ + Functions: []string{ + "state.highstate", + }, + States: []string{ + "highstate", + }, + }, + }, + }, + }, + }, + { + name: "advanced config, flags only", + flags: []string{ + "-host=127.0.0.1", + "-port=8080", + "-health-minions=false", + "-health-functions-filter=test.sls", + "-health-states-filter=nop", + "-ignore-test", + "-ignore-mock", + "-tls", + "-tls-cert=./cert", + "-tls-key=./key", + }, + want: Config{ + LogLevel: defaultLogLevel, + ListenAddress: "127.0.0.1", + ListenPort: 8080, + TLS: struct { + Enabled bool + Key string + Certificate string + }{ + Enabled: true, + Key: "./key", + Certificate: "./cert", + }, + Metrics: metrics.Config{ + HealthMinions: false, + Global: struct { + Filters struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + } + }{ + Filters: struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + }{ + IgnoreTest: true, + IgnoreMock: true, + }, + }, + SaltNewJobTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltExpectedResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionResponsesTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: false, + }, + SaltScheduledJobReturnTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: false, + }, + SaltResponsesTotal: struct{ Enabled bool }{ + Enabled: false, + }, + SaltFunctionStatus: struct { + Enabled bool + Filters struct { + Functions []string + States []string + } + }{ + Enabled: false, + Filters: struct { + Functions []string + States []string + }{ + Functions: []string{ + "test.sls", + }, + States: []string{ + "nop", + }, + }, + }, + }, + }, + }, + } + + name := os.Args[0] + backupArgs := os.Args + backupCommandLine := flag.CommandLine + defer func() { + flag.CommandLine = backupCommandLine + os.Args = backupArgs + viper.Reset() + }() + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + os.Args = append([]string{name}, test.flags...) + flag.CommandLine = flag.NewFlagSet(name, flag.ContinueOnError) + viper.Reset() + + cfg, err := ReadConfig("nofiles.yml") + + if diff := cmp.Diff(cfg, test.want); diff != "" { + t.Errorf("Mismatch for '%s' test:\n%s", test.name, diff) + } + + if err != nil { + t.Errorf("Unexpected error for '%s': '%s'", test.name, err) + } + }) + } +} + +func TestConfigFileOnly(t *testing.T) { + defer viper.Reset() + cfg, err := ReadConfig("config_test.yml") + want := Config{ + LogLevel: "info", + ListenAddress: "127.0.0.1", + ListenPort: 2113, + TLS: struct { + Enabled bool + Key string + Certificate string + }{ + Enabled: true, + Key: "/path/to/key", + Certificate: "/path/to/certificate", + }, + Metrics: metrics.Config{ + HealthMinions: true, + Global: struct { + Filters struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + } + }{ + Filters: struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + }{ + IgnoreTest: true, + IgnoreMock: false, + }, + }, + SaltNewJobTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltExpectedResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionResponsesTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: true, + }, + SaltScheduledJobReturnTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: true, + }, + SaltResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionStatus: struct { + Enabled bool + Filters struct { + Functions []string + States []string + } + }{ + Enabled: true, + Filters: struct { + Functions []string + States []string + }{ + Functions: []string{ + "state.sls", + }, + States: []string{ + "test", + }, + }, + }, + }, + } + + if diff := cmp.Diff(cfg, want); diff != "" { + t.Errorf("Mismatch:\n%s", diff) + } + + if err != nil { + t.Errorf("Unexpected error: '%s'", err) + } +} + +func TestConfigFileWithFlags(t *testing.T) { + name := os.Args[0] + backupArgs := os.Args + backupCommandLine := flag.CommandLine + defer func() { + flag.CommandLine = backupCommandLine + os.Args = backupArgs + viper.Reset() + }() + + flags := []string{ + "-host=127.0.0.1", + "-port=8080", + "-health-minions=false", + "-health-functions-filter=test.sls", + "-health-states-filter=nop", + "-ignore-mock", + } + + os.Args = append([]string{name}, flags...) + flag.CommandLine = flag.NewFlagSet(name, flag.ContinueOnError) + viper.Reset() + + cfg, err := ReadConfig("config_test.yml") + want := Config{ + LogLevel: "info", + ListenAddress: "127.0.0.1", + ListenPort: 8080, + TLS: struct { + Enabled bool + Key string + Certificate string + }{ + Enabled: true, + Key: "/path/to/key", + Certificate: "/path/to/certificate", + }, + Metrics: metrics.Config{ + HealthMinions: false, + Global: struct { + Filters struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + } + }{ + Filters: struct { + IgnoreTest bool `mapstructure:"ignore-test"` + IgnoreMock bool `mapstructure:"ignore-mock"` + }{ + IgnoreTest: true, + IgnoreMock: true, + }, + }, + SaltNewJobTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltExpectedResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionResponsesTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: true, + }, + SaltScheduledJobReturnTotal: struct { + Enabled bool + AddMinionLabel bool `mapstructure:"add-minion-label"` + }{ + Enabled: true, + AddMinionLabel: true, + }, + SaltResponsesTotal: struct{ Enabled bool }{ + Enabled: true, + }, + SaltFunctionStatus: struct { + Enabled bool + Filters struct { + Functions []string + States []string + } + }{ + Enabled: true, + Filters: struct { + Functions []string + States []string + }{ + Functions: []string{ + "test.sls", + }, + States: []string{ + "nop", + }, + }, + }, + }, + } + + if diff := cmp.Diff(cfg, want); diff != "" { + t.Errorf("Mismatch:\n%s", diff) + } + + if err != nil { + t.Errorf("Unexpected error: '%s'", err) + } +} diff --git a/cmd/salt-exporter/config_test.yml b/cmd/salt-exporter/config_test.yml new file mode 100644 index 0000000..a5f7d62 --- /dev/null +++ b/cmd/salt-exporter/config_test.yml @@ -0,0 +1,40 @@ +listen-address: "127.0.0.1" +listen-port: 2113 + +log-level: "info" +tls: + enabled: true + key: "/path/to/key" + certificate: "/path/to/certificate" + + +metrics: + global: + filters: + ignore-test: true + ignore-mock: false + + salt_new_job_total: + enabled: true + + salt_expected_responses_total: + enabled: true + + salt_function_responses_total: + enabled: true + add-minion-label: true # not recommended in production + + salt_scheduled_job_return_total: + enabled: true + add-minion-label: true # not recommended in production + + salt_responses_total: + enabled: true + + salt_function_status: + enabled: true + filters: + functions: + - "state.sls" + states: + - "test" \ No newline at end of file diff --git a/cmd/salt-exporter/main.go b/cmd/salt-exporter/main.go index 48bede9..076b071 100644 --- a/cmd/salt-exporter/main.go +++ b/cmd/salt-exporter/main.go @@ -17,6 +17,8 @@ import ( "github.com/rs/zerolog/log" ) +const configFileName = "config.yml" + var ( version = "" commit = "" @@ -94,7 +96,7 @@ func main() { defer quit() logging.Configure() - config, err := ReadConfig() + config, err := ReadConfig(configFileName) if err != nil { log.Fatal().Err(err).Msg("failed to load settings during initialization") } diff --git a/go.mod b/go.mod index fa17c64..c6e1083 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/kr/text v0.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index b93a0b5..7df614a 100644 --- a/go.sum +++ b/go.sum @@ -70,7 +70,6 @@ github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkX github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -82,6 +81,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -165,7 +165,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= @@ -246,8 +245,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= @@ -400,7 +399,6 @@ golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -416,8 +414,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= From 455fb6ca0eb170696e15e7ba5c82a84dd0c39ad8 Mon Sep 17 00:00:00 2001 From: Kevin Petremann Date: Thu, 6 Jul 2023 00:19:05 +0200 Subject: [PATCH 2/5] fix: remove config print --- cmd/salt-exporter/config.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/cmd/salt-exporter/config.go b/cmd/salt-exporter/config.go index ad127f5..03d6f33 100644 --- a/cmd/salt-exporter/config.go +++ b/cmd/salt-exporter/config.go @@ -4,8 +4,6 @@ import ( "errors" "flag" "fmt" - "path/filepath" - "strings" "github.com/kpetremann/salt-exporter/internal/metrics" "github.com/spf13/viper" @@ -44,7 +42,7 @@ type Config struct { Metrics metrics.Config } -func parseFlags() bool { +func parseFlags() { // flags flag.String("log-level", defaultLogLevel, "log level (debug, info, warn, error, fatal, panic, disabled)") @@ -65,26 +63,32 @@ func parseFlags() bool { "[DEPRECATED] apply filter on states to monitor, separated by a comma") flag.Parse() - return *healthMinions + // ensure compatibility with deprecated health-minions flag + if !*healthMinions { + viper.SetDefault("metrics.salt_function_status.enabled", false) + viper.SetDefault("metrics.salt_responses_total.enabled", false) + } } -func setDefaults(healthMinions bool) { +func setDefaults() { viper.SetDefault("log-level", defaultLogLevel) viper.SetDefault("listen-port", defaultPort) viper.SetDefault("metrics.health-minions", defaultHealthMinion) + viper.SetDefault("metrics.salt_new_job_total.enabled", true) viper.SetDefault("metrics.salt_expected_responses_total.enabled", true) viper.SetDefault("metrics.salt_function_responses_total.enabled", true) viper.SetDefault("metrics.salt_scheduled_job_return_total.enabled", true) - viper.SetDefault("metrics.salt_function_status.enabled", healthMinions) // TODO: true once health-minions will be removed - viper.SetDefault("metrics.salt_responses_total.enabled", healthMinions) // TODO: true once health-minions will be removed + viper.SetDefault("metrics.salt_responses_total.enabled", true) + viper.SetDefault("metrics.salt_function_status.enabled", true) + viper.SetDefault("metrics.salt_function_status.filters.functions", []string{defaultHealthFunctionsFilter}) viper.SetDefault("metrics.salt_function_status.filters.states", []string{defaultHealthStatesFilter}) } -func getConfig(configFileName string, healthMinions bool) (Config, error) { - setDefaults(healthMinions) +func getConfig() (Config, error) { + setDefaults() // bind flags var allFlags []viperFlag @@ -101,9 +105,8 @@ func getConfig(configFileName string, healthMinions bool) (Config, error) { } // bind configuration file - ext := filepath.Ext(configFileName) - viper.SetConfigName(strings.TrimSuffix(configFileName, ext)) - viper.SetConfigType(strings.TrimPrefix(ext, ".")) + viper.SetConfigName("config") + viper.SetConfigType("yml") viper.AddConfigPath(".") err := viper.ReadInConfig() @@ -135,12 +138,12 @@ func checkRequirements(cfg Config) error { return nil } -func ReadConfig(configFileName string) (Config, error) { +func ReadConfig() (Config, error) { var err error - healthMinions := parseFlags() + parseFlags() - cfg, err := getConfig(configFileName, healthMinions) + cfg, err := getConfig() if err != nil { return Config{}, err } From 430b6f9a28c871b09e5e5b0f28e1d92a625c4393 Mon Sep 17 00:00:00 2001 From: Kevin Petremann Date: Thu, 6 Jul 2023 00:20:04 +0200 Subject: [PATCH 3/5] fix(config): health-minions not working --- cmd/salt-exporter/config.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/cmd/salt-exporter/config.go b/cmd/salt-exporter/config.go index 03d6f33..e2b61cc 100644 --- a/cmd/salt-exporter/config.go +++ b/cmd/salt-exporter/config.go @@ -42,7 +42,7 @@ type Config struct { Metrics metrics.Config } -func parseFlags() { +func parseFlags() bool { // flags flag.String("log-level", defaultLogLevel, "log level (debug, info, warn, error, fatal, panic, disabled)") @@ -63,32 +63,26 @@ func parseFlags() { "[DEPRECATED] apply filter on states to monitor, separated by a comma") flag.Parse() - // ensure compatibility with deprecated health-minions flag - if !*healthMinions { - viper.SetDefault("metrics.salt_function_status.enabled", false) - viper.SetDefault("metrics.salt_responses_total.enabled", false) - } + return *healthMinions } -func setDefaults() { +func setDefaults(healthMinions bool) { viper.SetDefault("log-level", defaultLogLevel) viper.SetDefault("listen-port", defaultPort) viper.SetDefault("metrics.health-minions", defaultHealthMinion) - viper.SetDefault("metrics.salt_new_job_total.enabled", true) viper.SetDefault("metrics.salt_expected_responses_total.enabled", true) viper.SetDefault("metrics.salt_function_responses_total.enabled", true) viper.SetDefault("metrics.salt_scheduled_job_return_total.enabled", true) - viper.SetDefault("metrics.salt_responses_total.enabled", true) - viper.SetDefault("metrics.salt_function_status.enabled", true) - + viper.SetDefault("metrics.salt_function_status.enabled", healthMinions) // TODO: true once health-minions will be removed + viper.SetDefault("metrics.salt_responses_total.enabled", healthMinions) // TODO: true once health-minions will be removed viper.SetDefault("metrics.salt_function_status.filters.functions", []string{defaultHealthFunctionsFilter}) viper.SetDefault("metrics.salt_function_status.filters.states", []string{defaultHealthStatesFilter}) } -func getConfig() (Config, error) { - setDefaults() +func getConfig(healthMinions bool) (Config, error) { + setDefaults(healthMinions) // bind flags var allFlags []viperFlag @@ -138,12 +132,12 @@ func checkRequirements(cfg Config) error { return nil } -func ReadConfig() (Config, error) { +func ReadConfig(configFileName string) (Config, error) { var err error - parseFlags() + healthMinions := parseFlags() - cfg, err := getConfig() + cfg, err := getConfig(healthMinions) if err != nil { return Config{}, err } From 818bdf7f2d589598dee19b80b062fdf304b9114d Mon Sep 17 00:00:00 2001 From: Kevin Petremann Date: Thu, 6 Jul 2023 00:21:01 +0200 Subject: [PATCH 4/5] refactor: configuration filename configurable for tests --- cmd/salt-exporter/config.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/salt-exporter/config.go b/cmd/salt-exporter/config.go index e2b61cc..ad127f5 100644 --- a/cmd/salt-exporter/config.go +++ b/cmd/salt-exporter/config.go @@ -4,6 +4,8 @@ import ( "errors" "flag" "fmt" + "path/filepath" + "strings" "github.com/kpetremann/salt-exporter/internal/metrics" "github.com/spf13/viper" @@ -81,7 +83,7 @@ func setDefaults(healthMinions bool) { } -func getConfig(healthMinions bool) (Config, error) { +func getConfig(configFileName string, healthMinions bool) (Config, error) { setDefaults(healthMinions) // bind flags @@ -99,8 +101,9 @@ func getConfig(healthMinions bool) (Config, error) { } // bind configuration file - viper.SetConfigName("config") - viper.SetConfigType("yml") + ext := filepath.Ext(configFileName) + viper.SetConfigName(strings.TrimSuffix(configFileName, ext)) + viper.SetConfigType(strings.TrimPrefix(ext, ".")) viper.AddConfigPath(".") err := viper.ReadInConfig() @@ -137,7 +140,7 @@ func ReadConfig(configFileName string) (Config, error) { healthMinions := parseFlags() - cfg, err := getConfig(healthMinions) + cfg, err := getConfig(configFileName, healthMinions) if err != nil { return Config{}, err } From c8c68d8fb827e0a8603e16d4d752ff11e78f8046 Mon Sep 17 00:00:00 2001 From: Kevin Petremann <13746171+kpetremann@users.noreply.github.com> Date: Thu, 6 Jul 2023 18:04:55 +0200 Subject: [PATCH 5/5] docs: fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index befa521..279c5e2 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ metrics: - "highstate" ``` -**Note: Except for -health-minions, all flag have the priority over the configuration file.** +**Note: Except for -health-minions, all flags have the priority over the configuration file.** ## Features