rules: remove config package dependency

This commit is contained in:
Fabian Reinartz 2017-11-23 15:48:14 +01:00
parent 2d0e3746ac
commit bd9f7460eb
3 changed files with 21 additions and 34 deletions

View File

@ -272,12 +272,14 @@ func main() {
conntrack.DialWithTracing(), conntrack.DialWithTracing(),
) )
reloadables := []Reloadable{ reloaders := []func(cfg *config.Config) error{
remoteStorage, remoteStorage.ApplyConfig,
targetManager, targetManager.ApplyConfig,
ruleManager, webHandler.ApplyConfig,
webHandler, notifier.ApplyConfig,
notifier, func(cfg *config.Config) error {
return ruleManager.Update(time.Duration(cfg.GlobalConfig.EvaluationInterval), cfg.RuleFiles)
},
} }
prometheus.MustRegister(configSuccess) prometheus.MustRegister(configSuccess)
@ -330,11 +332,11 @@ func main() {
for { for {
select { select {
case <-hup: case <-hup:
if err := reloadConfig(cfg.configFile, logger, reloadables...); err != nil { if err := reloadConfig(cfg.configFile, logger, reloaders...); err != nil {
level.Error(logger).Log("msg", "Error reloading config", "err", err) level.Error(logger).Log("msg", "Error reloading config", "err", err)
} }
case rc := <-webHandler.Reload(): case rc := <-webHandler.Reload():
if err := reloadConfig(cfg.configFile, logger, reloadables...); err != nil { if err := reloadConfig(cfg.configFile, logger, reloaders...); err != nil {
level.Error(logger).Log("msg", "Error reloading config", "err", err) level.Error(logger).Log("msg", "Error reloading config", "err", err)
rc <- err rc <- err
} else { } else {
@ -363,7 +365,7 @@ func main() {
return nil return nil
} }
if err := reloadConfig(cfg.configFile, logger, reloadables...); err != nil { if err := reloadConfig(cfg.configFile, logger, reloaders...); err != nil {
return fmt.Errorf("Error loading config %s", err) return fmt.Errorf("Error loading config %s", err)
} }
@ -473,13 +475,7 @@ func main() {
level.Info(logger).Log("msg", "See you next time!") level.Info(logger).Log("msg", "See you next time!")
} }
// Reloadable things can change their internal state to match a new config func reloadConfig(filename string, logger log.Logger, rls ...func(*config.Config) error) (err error) {
// and handle failure gracefully.
type Reloadable interface {
ApplyConfig(*config.Config) error
}
func reloadConfig(filename string, logger log.Logger, rls ...Reloadable) (err error) {
level.Info(logger).Log("msg", "Loading configuration file", "filename", filename) level.Info(logger).Log("msg", "Loading configuration file", "filename", filename)
defer func() { defer func() {
@ -498,7 +494,7 @@ func reloadConfig(filename string, logger log.Logger, rls ...Reloadable) (err er
failed := false failed := false
for _, rl := range rls { for _, rl := range rls {
if err := rl.ApplyConfig(conf); err != nil { if err := rl(conf); err != nil {
level.Error(logger).Log("msg", "Failed to apply configuration", "err", err) level.Error(logger).Log("msg", "Failed to apply configuration", "err", err)
failed = true failed = true
} }

View File

@ -30,7 +30,6 @@ import (
"github.com/go-kit/kit/log/level" "github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/rulefmt" "github.com/prometheus/prometheus/pkg/rulefmt"
@ -505,15 +504,15 @@ func (m *Manager) Stop() {
level.Info(m.logger).Log("msg", "Rule manager stopped") level.Info(m.logger).Log("msg", "Rule manager stopped")
} }
// ApplyConfig updates the rule manager's state as the config requires. If // Update the rule manager's state as the config requires. If
// loading the new rules failed the old rule set is restored. // loading the new rules failed the old rule set is restored.
func (m *Manager) ApplyConfig(conf *config.Config) error { func (m *Manager) Update(interval time.Duration, paths []string) error {
m.mtx.Lock() m.mtx.Lock()
defer m.mtx.Unlock() defer m.mtx.Unlock()
// Get all rule files and load the groups they define. // Get all rule files and load the groups they define.
var files []string var files []string
for _, pat := range conf.RuleFiles { for _, pat := range paths {
fs, err := filepath.Glob(pat) fs, err := filepath.Glob(pat)
if err != nil { if err != nil {
// The only error can be a bad pattern. // The only error can be a bad pattern.
@ -523,7 +522,7 @@ func (m *Manager) ApplyConfig(conf *config.Config) error {
} }
// To be replaced with a configurable per-group interval. // To be replaced with a configurable per-group interval.
groups, errs := m.loadGroups(time.Duration(conf.GlobalConfig.EvaluationInterval), files...) groups, errs := m.loadGroups(interval, files...)
if errs != nil { if errs != nil {
for _, e := range errs { for _, e := range errs {
level.Error(m.logger).Log("msg", "loading groups failed", "err", e) level.Error(m.logger).Log("msg", "loading groups failed", "err", e)

View File

@ -24,7 +24,6 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/pkg/value" "github.com/prometheus/prometheus/pkg/value"
@ -292,17 +291,10 @@ func TestCopyState(t *testing.T) {
testutil.Equals(t, oldGroup.rules[0], newGroup.rules[3]) testutil.Equals(t, oldGroup.rules[0], newGroup.rules[3])
} }
func TestApplyConfig(t *testing.T) { func TestUpdate(t *testing.T) {
expected := map[string]labels.Labels{ expected := map[string]labels.Labels{
"test": labels.Labels{ "test": labels.FromStrings("name", "value"),
labels.Label{
Name: "name",
Value: "value",
},
},
} }
conf, err := config.LoadFile("../config/testdata/conf.good.yml")
testutil.Ok(t, err)
ruleManager := NewManager(&ManagerOptions{ ruleManager := NewManager(&ManagerOptions{
Appendable: nil, Appendable: nil,
Notifier: nil, Notifier: nil,
@ -312,7 +304,7 @@ func TestApplyConfig(t *testing.T) {
}) })
ruleManager.Run() ruleManager.Run()
err = ruleManager.ApplyConfig(conf) err := ruleManager.Update(0, nil)
testutil.Ok(t, err) testutil.Ok(t, err)
for _, g := range ruleManager.groups { for _, g := range ruleManager.groups {
g.seriesInPreviousEval = []map[string]labels.Labels{ g.seriesInPreviousEval = []map[string]labels.Labels{
@ -320,7 +312,7 @@ func TestApplyConfig(t *testing.T) {
} }
} }
err = ruleManager.ApplyConfig(conf) err = ruleManager.Update(0, nil)
testutil.Ok(t, err) testutil.Ok(t, err)
for _, g := range ruleManager.groups { for _, g := range ruleManager.groups {
for _, actual := range g.seriesInPreviousEval { for _, actual := range g.seriesInPreviousEval {