Documentation ¶
Overview ¶
Package reloader contains helpers to trigger reloads of Prometheus instances on configuration changes and to substitute environment variables in config files.
Reloader type is useful when you want to:
- Watch on changes against certain file e.g (`cfgFile`).
- Optionally, specify different different output file for watched `cfgFile` (`cfgOutputFile`). This will also try decompress the `cfgFile` if needed and substitute ALL the envvars using Kubernetes substitution format: (`$(var)`)
- Watch on changes against certain directories (`ruleDires`).
Once any of those two changes Prometheus on given `reloadURL` will be notified, causing Prometheus to reload configuration and rules.
This and below for reloader:
u, _ := url.Parse("http://localhost:9090") rl := reloader.New( nil, reloader.ReloadURLFromBase(u), "/path/to/cfg", "/path/to/cfg.out", []string{"/path/to/dirs"}, )
The url of reloads can be generated with function ReloadURLFromBase(). It will append the default path of reload into the given url:
u, _ := url.Parse("http://localhost:9090") reloader.ReloadURLFromBase(u) // It will return "http://localhost:9090/-/reload"
Start watching changes and stopped until the context gets canceled:
ctx, cancel := context.WithCancel(context.Background()) go func() { if err := rl.Watch(ctx); err != nil { log.Fatal(err) } }() // ... cancel()
By default, reloader will make a schedule to check the given config files and dirs of sum of hash with the last result, even if it is no changes.
A basic example of configuration template with environment variables:
global: external_labels: replica: '$(HOSTNAME)'
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReloadURLFromBase ¶
ReloadURLFromBase returns the standard Prometheus reload URL from its base URL.
Example ¶
package main import ( "fmt" "log" "net/url" "github.com/thanos-io/thanos/pkg/reloader" ) func main() { u, err := url.Parse("http://localhost:9090") if err != nil { log.Fatal(err) } fmt.Println(reloader.ReloadURLFromBase(u)) }
Output: http://localhost:9090/-/reload
Types ¶
type Reloader ¶
type Reloader struct {
// contains filtered or unexported fields
}
Reloader can watch config files and trigger reloads of a Prometheus server. It optionally substitutes environment variables in the configuration. Referenced environment variables must be of the form `$(var)` (not `$var` or `${var}`).
Example ¶
package main import ( "context" "io" "log" "net/http" "net/url" "github.com/thanos-io/thanos/pkg/reloader" ) func main() { u, err := url.Parse("http://localhost:9090") if err != nil { log.Fatal(err) } rl := reloader.New( nil, nil, reloader.ReloadURLFromBase(u), "/path/to/cfg", "/path/to/cfg.out", []string{"/path/to/dirs"}, ) ctx, cancel := context.WithCancel(context.Background()) go func() { if err := rl.Watch(ctx); err != nil { log.Fatal(err) } }() reloadHandler := func(w http.ResponseWriter, req *http.Request) { if _, err := io.WriteString(w, "Reloaded\n"); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } http.HandleFunc("/-/reload", reloadHandler) log.Fatal(http.ListenAndServe(":9090", nil)) cancel() }
Output:
func New ¶
func New(logger log.Logger, reg prometheus.Registerer, reloadURL *url.URL, cfgFile string, cfgOutputFile string, ruleDirs []string) *Reloader
New creates a new reloader that watches the given config file and rule directory and triggers a Prometheus reload upon changes. If cfgOutputFile is not empty the config file will be decompressed if needed, environment variables will be substituted and the output written into the given path. Prometheus should then use cfgOutputFile as its config file path.
func (*Reloader) Watch ¶
Watch starts to watch periodically the config file and rules and process them until the context gets canceled. Config file gets env expanded if cfgOutputFile is specified and reload is trigger if config or rules changed. Watch watchers periodically based on r.watchInterval. For config file it watches it directly as well via fsnotify. It watches rule dirs as well, but lot's of edge cases are missing, so rely on interval mostly.
func (*Reloader) WithWatchInterval ¶ added in v0.7.0
We cannot detect everything via watch. Watch interval controls how often we re-read given dirs non-recursively.