From a48a01836810055029145881b7777fddd00c233a Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Wed, 14 Jun 2017 12:19:21 +0530 Subject: [PATCH] Make sure groups are unique in a single file Signed-off-by: Goutham Veeramachaneni --- pkg/rulefmt/rulefmt.go | 13 ++++++++++++- rules/manager.go | 11 ++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/rulefmt/rulefmt.go b/pkg/rulefmt/rulefmt.go index 3453ce5d6d..777da84c27 100644 --- a/pkg/rulefmt/rulefmt.go +++ b/pkg/rulefmt/rulefmt.go @@ -16,7 +16,7 @@ type Error struct { } func (err *Error) Error() string { - return errors.Wrapf(err, "group %q, rule %d", err.Group, err.Rule).Error() + return errors.Wrapf(err.Err, "group %q, rule %d", err.Group, err.Rule).Error() } // RuleGroups is a set of rule groups that are typically exposed in a file. @@ -30,7 +30,18 @@ func (g *RuleGroups) Validate() (errs []error) { if g.Version != 1 { errs = append(errs, errors.Errorf("invalid rule group version %d", g.Version)) } + set := map[string]struct{}{} + for _, g := range g.Groups { + if _, ok := set[g.Name]; ok { + errs = append( + errs, + errors.Errorf("groupname: \"%s\" is repeated in the same file", g.Name), + ) + } + + set[g.Name] = struct{}{} + for i, r := range g.Rules { for _, err := range r.Validate() { errs = append(errs, &Error{ diff --git a/rules/manager.go b/rules/manager.go index fcfcba606b..9720c3829c 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -26,6 +26,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" "github.com/prometheus/common/model" + "github.com/prometheus/tsdb" "golang.org/x/net/context" "github.com/prometheus/prometheus/config" @@ -501,10 +502,9 @@ func (m *Manager) loadGroups(interval time.Duration, filenames ...string) (map[s groups := make(map[string]*Group) for _, fn := range filenames { - rgs, err := rulefmt.ParseFile(fn) - if err != nil { - // TODO(gouthamve): Use multi-error? - return nil, err[0] + rgs, errs := rulefmt.ParseFile(fn) + if errs != nil { + return nil, tsdb.MultiError(errs) } for _, rg := range rgs.Groups { @@ -547,7 +547,8 @@ func (m *Manager) loadGroups(interval time.Duration, filenames ...string) (map[s )) } - groups[rg.Name] = NewGroup(rg.Name, itv, rules, m.opts) + // Groups need not be unique across filenames. + groups[rg.Name+";"+fn] = NewGroup(rg.Name, itv, rules, m.opts) } }