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 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 (`watchedDirs`).
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, nil, &reloader.Options{
ReloadURL: reloader.ReloadURLFromBase(u),
CfgFile: "/path/to/cfg",
CfgOutputFile: "/path/to/cfg.out",
WatchedDirs: []string{"/path/to/dirs"},
WatchInterval: 3 * time.Minute,
RetryInterval: 5 * time.Second,
})
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()
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 CfgDirOption ¶ added in v0.35.0
type CfgDirOption struct {
// Dir is the path containing the Prometheus configurations to watch.
Dir string
// OutputDir is a directory path to output configurations. If OutputDir is not empty,
// then all config files in the Dir directory are decompressed if needed, environment
// variables will be substituted and the output written into the given path. Prometheus
// should then use OutputDir as its config path.
OutputDir string
}
CfgDirOption contains options for watching directories containing configurations. For example, a directory could contain additional scrape config files or rule files listed in the main Prometheus configuration. Sub-directories are ignored.
type HTTPReloader ¶ added in v0.34.0
type HTTPReloader struct {
// contains filtered or unexported fields
}
func NewHTTPReloader ¶ added in v0.34.0
func (*HTTPReloader) TriggerReload ¶ added in v0.34.0
func (hr *HTTPReloader) TriggerReload(ctx context.Context) error
type Options ¶ added in v0.15.0
type Options struct {
// ReloadURL is the Prometheus URL to trigger reloads.
ReloadURL *url.URL
// HTTP client used to connect to the web server.
HTTPClient http.Client
// ProcessName is the process executable name to trigger reloads. If not
// empty, the reloader sends a SIGHUP signal to the matching process ID
// instead of using the HTTP reload endpoint.
ProcessName string
// RuntimeInfoURL is the Prometheus URL returning runtime information
// including the last configuration status (e.g. `/api/v1/status/runtimeinfo`).
// It is only relevant for signal-based reloads.
// If empty, the reloader will not be able to assess that the reloading is
// successful.
RuntimeInfoURL *url.URL
// CfgFile is a path to the Prometheus config file to watch.
CfgFile string
// CfgOutputFile is a path for the output config file.
// 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.
CfgOutputFile string
// CfgDirs is an array of paths to directories containing Prometheus configs to watch.
CfgDirs []CfgDirOption
// WatchedDirs is a collection of paths for the reloader to watch over.
WatchedDirs []string
// DelayInterval controls how long the reloader will wait without receiving
// new file-system events before it applies the reload.
DelayInterval time.Duration
// WatchInterval controls how often reloader re-reads config and directories.
WatchInterval time.Duration
// RetryInterval controls how often the reloader retries a reloading of the
// configuration in case the reload operation returned an error.
RetryInterval time.Duration
// TolerateEnvVarExpansionErrors suppresses errors when expanding environment variables in the config file, and
// leaves the unset variables as is. All found environment variables are still expanded.
TolerateEnvVarExpansionErrors bool
}
Options bundles options for the Reloader.
type PIDReloader ¶ added in v0.34.0
type PIDReloader struct {
// contains filtered or unexported fields
}
func NewPIDReloader ¶ added in v0.34.0
func (*PIDReloader) TriggerReload ¶ added in v0.34.0
func (pr *PIDReloader) TriggerReload(ctx context.Context) error
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"
"time"
"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.Options{
ReloadURL: reloader.ReloadURLFromBase(u),
CfgFile: "/path/to/cfg",
CfgOutputFile: "/path/to/cfg.out",
WatchedDirs: []string{"/path/to/dirs"},
WatchInterval: 3 * time.Minute,
RetryInterval: 5 * time.Second,
DelayInterval: 1 * time.Second,
})
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()
}
func New ¶
func New(logger log.Logger, reg prometheus.Registerer, o *Options) *Reloader
New creates a new reloader that watches the given config file and directories and triggers a Prometheus reload upon changes.
func (*Reloader) Watch ¶
Watch detects any change made to the watched config file and directories. It returns when the context is canceled. Whenever a filesystem change is detected or the watch interval has elapsed, the reloader expands the config file (if cfgOutputFile is specified) and triggers a reload if the configuration file or files in the watched directories have changed. Because some edge cases might be missing, the reloader also relies on the watch interval.
type TriggerReloader ¶ added in v0.34.0
TriggerReloader reloads the configuration of the process.