From 80a9dc79fee0fc293d9ad1081adc9238caea18fe Mon Sep 17 00:00:00 2001 From: Nikolay Khramchikhin Date: Thu, 3 Sep 2020 11:04:42 +0300 Subject: [PATCH] changed vmalert behaviour (#738) * VMAlert start with empty rules dir There are some applications (operator for instance), that generates alerts configuration at runtime and vmalert must start correctly without rules to support this behaviour. Later application will add rules files and send SIGHUP to vmalert, which will trigger reading rules files and start rules exectuion. Removing rules files with SIGHUP signal must stop rules execution and vmalert will wait for new rules. * imports sorted * added test cases for empty rules, removed blank line * fixed imports conflict * updated tests --- app/vmalert/config/config.go | 3 ++- app/vmalert/config/config_test.go | 4 ---- app/vmalert/manager_test.go | 35 ++++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/vmalert/config/config.go b/app/vmalert/config/config.go index 33864f64b..6c38d9e67 100644 --- a/app/vmalert/config/config.go +++ b/app/vmalert/config/config.go @@ -11,6 +11,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier" "github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/metricsql" "gopkg.in/yaml.v2" ) @@ -170,7 +171,7 @@ func Parse(pathPatterns []string, validateAnnotations, validateExpressions bool) } } if len(groups) < 1 { - return nil, fmt.Errorf("no groups found in %s", strings.Join(pathPatterns, ";")) + logger.Warnf("no groups found in %s", strings.Join(pathPatterns, ";")) } return groups, nil } diff --git a/app/vmalert/config/config_test.go b/app/vmalert/config/config_test.go index 9e653480b..d46b4ba23 100644 --- a/app/vmalert/config/config_test.go +++ b/app/vmalert/config/config_test.go @@ -51,10 +51,6 @@ func TestParseBad(t *testing.T) { []string{"testdata/dir/rules4-bad.rules"}, "either `record` or `alert` must be set", }, - { - []string{"testdata/*.yaml"}, - "no groups found", - }, } for _, tc := range testCases { _, err := Parse(tc.path, true, true) diff --git a/app/vmalert/manager_test.go b/app/vmalert/manager_test.go index bc6f30856..a3cd3d31f 100644 --- a/app/vmalert/manager_test.go +++ b/app/vmalert/manager_test.go @@ -5,7 +5,6 @@ import ( "math/rand" "net/url" "os" - "strings" "sync" "testing" "time" @@ -19,16 +18,15 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -func TestManagerUpdateError(t *testing.T) { +// TestManagerEmptyRulesDir tests +// successful cases of +// starting with empty rules folder +func TestManagerEmptyRulesDir(t *testing.T) { m := &manager{groups: make(map[uint64]*Group)} path := []string{"foo/bar"} err := m.update(context.Background(), path, true, true, false) - if err == nil { - t.Fatalf("expected to have err; got nil instead") - } - expErr := "no groups found" - if !strings.Contains(err.Error(), expErr) { - t.Fatalf("expected to got err %s; got %s", expErr, err) + if err != nil { + t.Fatalf("expected to load succesfully with empty rules dir; got err instead: %v", err) } } @@ -180,6 +178,27 @@ func TestManagerUpdate(t *testing.T) { }}, }, }, + { + name: "update empty dir rules from 0 to 2 groups", + initPath: "config/testdata/empty/*", + updatePath: "config/testdata/rules0-good.rules", + want: []*Group{ + { + File: "config/testdata/rules0-good.rules", + Name: "groupGorSingleAlert", + Interval: defaultEvalInterval, + Rules: []Rule{VMRows}, + }, + { + File: "config/testdata/rules0-good.rules", + Interval: defaultEvalInterval, + Name: "TestGroup", Rules: []Rule{ + Conns, + ExampleAlertAlwaysFiring, + }, + }, + }, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) {