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(),
)
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
}

View File

@ -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)

View File

@ -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 {