Skip to content

Commit

Permalink
feat(config): config file is configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kpetremann committed Jun 14, 2024
1 parent 1be764b commit 7bbbd76
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
22 changes: 14 additions & 8 deletions cmd/salt-exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/viper"
)

const defaultConfigFilename = "config.yml"
const defaultLogLevel = "info"
const defaultPort = 2112
const defaultHealthMinion = true
Expand Down Expand Up @@ -48,9 +49,10 @@ type Config struct {
Metrics metrics.Config
}

func parseFlags() bool {
func parseFlags() (string, bool) {
// flags
versionCmd := flag.Bool("version", false, "print version")
configFile := flag.String("config-file", defaultConfigFilename, "config filepath")
flag.String("log-level", defaultLogLevel, "log level (debug, info, warn, error, fatal, panic, disabled)")
flag.String("host", "", "listen address")
flag.Int("port", defaultPort, "listen port")
Expand Down Expand Up @@ -80,7 +82,7 @@ func parseFlags() bool {
os.Exit(0)
}

return *healthMinions
return *configFile, *healthMinions
}

func setDefaults(healthMinions bool) {
Expand Down Expand Up @@ -117,10 +119,14 @@ 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.AddConfigPath(".")
if filepath.IsAbs(configFileName) {
viper.SetConfigFile(configFileName)
} else {
ext := filepath.Ext(configFileName)
viper.SetConfigName(strings.TrimSuffix(configFileName, ext))
viper.SetConfigType(strings.TrimPrefix(ext, "."))
viper.AddConfigPath(".")
}

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "__"))
viper.SetEnvPrefix("SALT")
Expand Down Expand Up @@ -155,10 +161,10 @@ func checkRequirements(cfg Config) error {
return nil
}

func ReadConfig(configFileName string) (Config, error) {
func ReadConfig() (Config, error) {
var err error

healthMinions := parseFlags()
configFileName, healthMinions := parseFlags()

cfg, err := getConfig(configFileName, healthMinions)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/salt-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/rs/zerolog/log"
)

const configFileName = "config.yml"

var (
version = "unknown"
commit = "unknown"
Expand Down Expand Up @@ -107,7 +105,7 @@ func main() {
defer quit()
logging.Configure()

config, err := ReadConfig(configFileName)
config, err := ReadConfig()
if err != nil {
log.Fatal().Err(err).Msg("failed to load settings during initialization") //nolint:gocritic // force exit
}
Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
listen-port: 3333
12 changes: 10 additions & 2 deletions docs/docs/salt-exporter/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The salt-exporter can be configured with flags, environments variables and confi

The exporter is looking for `config.yml`.

Note: You can specify a specific config filepath using `--config-file`, i.e. `--config-file="/srv/salt-exporter/config.yml"`

See below a full example of a configuration file:

``` { .yaml .copy }
Expand Down Expand Up @@ -153,18 +155,22 @@ SALT_METRICS__GLOBAL__FILTERS__IGNORE_TEST=true

```
./salt-exporter -help
-config-file string
config filepath (default "config.yml")
-health-functions-filter string
[DEPRECATED] apply filter on functions to monitor, separated by a comma (default "highstate")
-health-states-filter string
[DEPRECATED] apply filter on states to monitor, separated by a comma (default "highstate")
-health-minions
[DEPRECATED] enable minion metrics (default true)
-health-states-filter string
[DEPRECATED] apply filter on states to monitor, separated by a comma (default "highstate")
-host string
listen address
-ignore-mock
ignore mock=True events
-ignore-test
ignore test=True events
-ipc-file string
file location of the salt-master event bus (default "/var/run/salt/master/master_event_pub.ipc")
-log-level string
log level (debug, info, warn, error, fatal, panic, disabled) (default "info")
-port int
Expand All @@ -175,5 +181,7 @@ SALT_METRICS__GLOBAL__FILTERS__IGNORE_TEST=true
TLS certificated
-tls-key string
TLS private key
-version
print version
```

0 comments on commit 7bbbd76

Please sign in to comment.