Skip to content

Commit

Permalink
Merge pull request #36 from kpetremann/test_and_fix_config
Browse files Browse the repository at this point in the history
Test and fix config
  • Loading branch information
kpetremann authored Jul 6, 2023
2 parents 351ed1c + c8c68d8 commit 474f8a4
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 29 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ metrics:
- "highstate"
```

**Note: Except for -health-minions, all flags have the priority over the configuration file.**

## Features

Supported tags:
Expand Down
36 changes: 15 additions & 21 deletions cmd/salt-exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)")

Expand All @@ -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
Expand All @@ -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()
Expand All @@ -123,8 +119,6 @@ func getConfig() (Config, error) {
return Config{}, fmt.Errorf("failed to load configuration: %w", err)
}

pp.Println(cfg)

return cfg, nil
}

Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 474f8a4

Please sign in to comment.