Skip to content

Commit

Permalink
Define Additional Scripts via Separate Config Files (#116)
Browse files Browse the repository at this point in the history
It is now possible to define additional scripts for the exporter via
separate config files. For that a new option `scripts_configs` was
added, which can be used to set a list of files, which are containing
additional script configuration.

The configuration must look as follows:

```
script_configs:
  - /path/to/file.yaml
```

The content of the additional scripts configuration file, can only
contain a list of scripts, e.g.

```yaml
- name: test
  command: ./examples/test.sh
```
  • Loading branch information
ricoberger authored Feb 10, 2024
1 parent 44ddcaf commit ebeaf94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ scripts:
prefix: <string>
scrape_interval: <duration>
scrape_timeout: <duration>

scripts_configs:
- <string>
```
The `name` of the script must be a valid Prometheus label value. The `command` string is the script which is executed with all arguments specified in `args`. To add dynamic arguments you can pass the `params` query parameter with a list of query parameters which values should be added as argument. The program will be executed directly, without a shell being invoked, and it is recommended that it be specified by path instead of relying on ``$PATH``.
Expand Down Expand Up @@ -146,7 +149,7 @@ For testing purposes, the timeout can be specified directly as a URL parameter (

The `cacheDuration` config can be used to cache the results from an execution of the script for the provided time. The provided duration must be parsable by the [`time.ParseDuration`](https://pkg.go.dev/time#ParseDuration) function. If no cache duration is provided or the provided cache duration can not be parsed, the output of an script will not be cached.

You can fine tune the script discovery options via optional script `discovery`. All these options will go through prometheus configuration where you can change them via relabel mechanism.
You can fine tune the script discovery options via optional script `discovery`. All these options will go through prometheus configuration where you can change them via relabel mechanism.
There are `params` to define dynamic script parameters (with reserved keys: `params`, `prefix`, `script` and `timeout`) where only value will be used during script invoking (similar to `args`), `prefix` to define prefix for all script metrics, `scrape_interval` to define how often the script scrape should run and `scrape_timeout` to define the scrape timeout for prometheus (similar to `timeout`).

The global `discovery` configures the main discovery parameters. If not defined, the exporter will use `Host:` header from the request to decide how to present a `target` to prometheus.
Expand Down
5 changes: 3 additions & 2 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ discovery:
# path: /prefix-path

scripts:
- name: test
command: ./examples/test.sh
- name: ping
command: ./examples/ping.sh
cacheDuration: 1m
Expand All @@ -45,3 +43,6 @@ scripts:
args:
- test1
- test2

scripts_configs:
- ./examples/scripts_*.yaml
2 changes: 2 additions & 0 deletions examples/scripts_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- name: test
command: ./examples/test.sh
29 changes: 29 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -47,6 +48,8 @@ type Config struct {

Scripts []ScriptConfig `yaml:"scripts"`

ScriptsConfigs []string `yaml:"scripts_configs"`

Discovery struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Expand Down Expand Up @@ -82,6 +85,32 @@ func (c *Config) LoadConfig(file string) error {
return err
}

// Additional scripts can also be defined via the `scripts_configs` field. This field can contain a list of glob
// patterns that will be expanded to a list of files. Each file contains a list of additional script configurations.
for _, scriptsConfig := range c.ScriptsConfigs {
files, err := filepath.Glob(scriptsConfig)
if err != nil {
return err
}

for _, file := range files {
data, err := os.ReadFile(file)
if err != nil {
return err
}

data = []byte(expandEnv(string(data)))

var additionalScriptConfigs []ScriptConfig
err = yaml.Unmarshal(data, &additionalScriptConfigs)
if err != nil {
return err
}

c.Scripts = append(c.Scripts, additionalScriptConfigs...)
}
}

return nil
}

Expand Down

0 comments on commit ebeaf94

Please sign in to comment.