Skip to content

Commit

Permalink
fix: config default and binding
Browse files Browse the repository at this point in the history
  • Loading branch information
kpetremann committed Jun 27, 2023
1 parent f6f0b71 commit 6ee7e34
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
41 changes: 31 additions & 10 deletions cmd/salt-exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ import (
"github.com/spf13/viper"
)

const defaultLogLevel = "info"
const defaultPort = 2112
const defaultHealthMinion = true
const defaultHealthFunctionsFilter = "state.highstate"
const defaultHealthStatesFilter = "highstate"

var flagConfigMapping = map[string]string{
"host": "listen-address",
"port": "listen-port",
"tls": "tls.enabled",
"tls-cert": "tls.certificate",
"tls-key": "tls.key",
"host": "listen-address",
"port": "listen-port",
"tls": "tls.enabled",
"tls-cert": "tls.certificate",
"tls-key": "tls.key",
"ignore-test": "metrics.global.filters.ignore-test",
"ignore-mock": "metrics.global.filters.ignore-mock",
"health-minions": "metrics.health-minions",
"health-functions-filter": "metrics.salt_function_status.filters.functions",
"health-states-filter": "metrics.salt_function_status.filters.states",
}

type Config struct {
Expand All @@ -33,26 +44,36 @@ type Config struct {

func parseFlags() {
// flags
flag.String("log-level", "info", "log level (debug, info, warn, error, fatal, panic, disabled)")
flag.String("log-level", defaultLogLevel, "log level (debug, info, warn, error, fatal, panic, disabled)")

flag.String("host", "", "listen address")
flag.Int("port", 2112, "listen port")
flag.Int("port", defaultPort, "listen port")
flag.Bool("tls", false, "enable TLS")
flag.String("tls-cert", "", "TLS certificated")
flag.String("tls-key", "", "TLS private key")

flag.Bool("ignore-test", false, "ignore test=True events")
flag.Bool("ignore-mock", false, "ignore mock=True events")

flag.Bool("health-minions", true, "enable minion metrics")
flag.String("health-functions-filter", "state.highstate",
flag.Bool("health-minions", defaultHealthMinion, "enable minion metrics")
flag.String("health-functions-filter", defaultHealthStatesFilter,
"apply filter on functions to monitor, separated by a comma")
flag.String("health-states-filter", "highstate",
flag.String("health-states-filter", defaultHealthStatesFilter,
"apply filter on states to monitor, separated by a comma")
flag.Parse()
}

func setDefaults() {
viper.SetDefault("log-level", defaultLogLevel)
viper.SetDefault("listen-port", defaultPort)
viper.SetDefault("metrics.health-minions", defaultHealthMinion)
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()

// bind flags
var allFlags []viperFlag
flag.Visit(func(f *flag.Flag) {
Expand Down
2 changes: 0 additions & 2 deletions cmd/salt-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ func start(config Config) {

func main() {
defer quit()

// TODO: refacto > configure()
logging.Configure()

config, err := ReadConfig()
Expand Down
1 change: 1 addition & 0 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Configure() {
// logLevel: The log level to use, in zerolog format
func SetLevel(logLevel string) {
level, err := zerolog.ParseLevel(logLevel)
fmt.Println(logLevel)
if err != nil {
fmt.Println("Failed to parse log level")
os.Exit(1)
Expand Down

0 comments on commit 6ee7e34

Please sign in to comment.