From bd9f7460eb6fac35a103036d8fc5cd63c42ddd63 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 23 Nov 2017 15:48:14 +0100 Subject: [PATCH] rules: remove config package dependency --- cmd/prometheus/main.go | 30 +++++++++++++----------------- rules/manager.go | 9 ++++----- rules/manager_test.go | 16 ++++------------ 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 4b19c9b61..f696d739d 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -272,12 +272,14 @@ func main() { conntrack.DialWithTracing(), ) - reloadables := []Reloadable{ - remoteStorage, - targetManager, - ruleManager, - webHandler, - notifier, + reloaders := []func(cfg *config.Config) error{ + remoteStorage.ApplyConfig, + targetManager.ApplyConfig, + webHandler.ApplyConfig, + notifier.ApplyConfig, + func(cfg *config.Config) error { + return ruleManager.Update(time.Duration(cfg.GlobalConfig.EvaluationInterval), cfg.RuleFiles) + }, } prometheus.MustRegister(configSuccess) @@ -330,11 +332,11 @@ func main() { for { select { 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) } 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) rc <- err } else { @@ -363,7 +365,7 @@ func main() { 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) } @@ -473,13 +475,7 @@ func main() { level.Info(logger).Log("msg", "See you next time!") } -// Reloadable things can change their internal state to match a new config -// and handle failure gracefully. -type Reloadable interface { - ApplyConfig(*config.Config) error -} - -func reloadConfig(filename string, logger log.Logger, rls ...Reloadable) (err error) { +func reloadConfig(filename string, logger log.Logger, rls ...func(*config.Config) error) (err error) { level.Info(logger).Log("msg", "Loading configuration file", "filename", filename) defer func() { @@ -498,7 +494,7 @@ func reloadConfig(filename string, logger log.Logger, rls ...Reloadable) (err er failed := false 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) failed = true } diff --git a/rules/manager.go b/rules/manager.go index 3d7107036..4c5ef9382 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -30,7 +30,6 @@ import ( "github.com/go-kit/kit/log/level" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/rulefmt" @@ -505,15 +504,15 @@ func (m *Manager) Stop() { 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. -func (m *Manager) ApplyConfig(conf *config.Config) error { +func (m *Manager) Update(interval time.Duration, paths []string) error { m.mtx.Lock() defer m.mtx.Unlock() // Get all rule files and load the groups they define. var files []string - for _, pat := range conf.RuleFiles { + for _, pat := range paths { fs, err := filepath.Glob(pat) if err != nil { // 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. - groups, errs := m.loadGroups(time.Duration(conf.GlobalConfig.EvaluationInterval), files...) + groups, errs := m.loadGroups(interval, files...) if errs != nil { for _, e := range errs { level.Error(m.logger).Log("msg", "loading groups failed", "err", e) diff --git a/rules/manager_test.go b/rules/manager_test.go index b9cb2ba49..33ad84fe8 100644 --- a/rules/manager_test.go +++ b/rules/manager_test.go @@ -24,7 +24,6 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/value" @@ -292,17 +291,10 @@ func TestCopyState(t *testing.T) { testutil.Equals(t, oldGroup.rules[0], newGroup.rules[3]) } -func TestApplyConfig(t *testing.T) { +func TestUpdate(t *testing.T) { expected := map[string]labels.Labels{ - "test": labels.Labels{ - labels.Label{ - Name: "name", - Value: "value", - }, - }, + "test": labels.FromStrings("name", "value"), } - conf, err := config.LoadFile("../config/testdata/conf.good.yml") - testutil.Ok(t, err) ruleManager := NewManager(&ManagerOptions{ Appendable: nil, Notifier: nil, @@ -312,7 +304,7 @@ func TestApplyConfig(t *testing.T) { }) ruleManager.Run() - err = ruleManager.ApplyConfig(conf) + err := ruleManager.Update(0, nil) testutil.Ok(t, err) for _, g := range ruleManager.groups { 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) for _, g := range ruleManager.groups { for _, actual := range g.seriesInPreviousEval {