Skip to content

Commit

Permalink
feat: configuration via environment variables
Browse files Browse the repository at this point in the history
All settings available in the configuration file can be set as environment variables, but:

- all variables must be prefixed by `SALT_`
- uppercase only
- `-` in the configuration file becomes a `_`
- `__` is the level separator

The precedence order is the one coming from viper:
- flags
- environment variables
- configuration file
  • Loading branch information
kpetremann committed Jul 8, 2023
1 parent 6cffd35 commit 2d2079d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ metrics:
## Usage
### Run
Simply run:
```./salt-exporter```

The exporter can be configured using flags:
The exporter can be configured in different ways, with the following precedence order:
* flags
* environment variables
* configuration file (config.yml)

### Flags

```
./salt-exporter -help
-health-functions-filter string
Expand All @@ -83,9 +91,41 @@ The exporter can be configured using flags:
TLS private key
```
It can also be configured via a `config.yml` file, which provides more customization.
### Environment variables
All settings available in the configuration file can be set as environment variables, but:
* all variables must be prefixed by `SALT_`
* uppercase only
* `-` in the configuration file becomes a `_`
* `__` is the level separator
For example, the equivalent of this config file:
```yaml
log-level: "info"
tls:
enabled: true
metrics:
global:
filters:
ignore-test: true
```

is:

```
SALT_LOG_LEVEL="info"
SALT_TLS__ENABLED=true
SALT_METRICS__GLOBAL__FILTERS__IGNORE_TEST=true
```

### Configuration file

The exporter is looking for `config.yml`.

See below a full example of a configuration file:

The default settings are:
```yaml
listen-address: ""
listen-port: 2112
Expand Down Expand Up @@ -129,8 +169,6 @@ metrics:
- "highstate"
```
**Note: Except for -health-minions, all flags have the priority over the configuration file.**

## Features
Supported tags:
Expand Down
4 changes: 4 additions & 0 deletions cmd/salt-exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func getConfig(configFileName string, healthMinions bool) (Config, error) {
viper.SetConfigType(strings.TrimPrefix(ext, "."))
viper.AddConfigPath(".")

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "__"))
viper.SetEnvPrefix("SALT")
viper.AutomaticEnv()

err := viper.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
Expand Down

0 comments on commit 2d2079d

Please sign in to comment.