reloader

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2019 License: Apache-2.0 Imports: 20 Imported by: 0

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

func ReloadURLFromBase(u *url.URL) *url.URL

ReloadURLFromBase returns the standard Prometheus reload URL from its base URL.

Example
package main

import (
	"fmt"
	"log"
	"net/url"

	"github.com/improbable-eng/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/improbable-eng/thanos/pkg/reloader"
)

func main() {
	u, err := url.Parse("http://localhost:9090")
	if err != nil {
		log.Fatal(err)
	}
	rl := reloader.New(
		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)
		}
		return
	}

	http.HandleFunc("/-/reload", reloadHandler)
	log.Fatal(http.ListenAndServe(":9090", nil))

	cancel()
}
Output:

func New

func New(logger log.Logger, 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

func (r *Reloader) Watch(ctx context.Context) error

Watch starts to watch 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL