diff --git a/filter/pattern_index.go b/filter/pattern_index.go index d8dd49aac..393958e4a 100644 --- a/filter/pattern_index.go +++ b/filter/pattern_index.go @@ -18,6 +18,7 @@ type PatternNode struct { Hash uint64 Prefix string InnerParts []string + Terminal bool } // PatternIndex helps to index patterns and allows to match them by metric @@ -37,7 +38,7 @@ func NewPatternIndex(logger moira.Logger, patterns []string) *PatternIndex { logger.Warningf("Pattern %s is ignored because it contains an empty part", pattern) continue } - for _, part := range parts { + for i, part := range parts { found := false for _, child := range currentNode.Children { if part == child.Part { @@ -75,6 +76,9 @@ func NewPatternIndex(logger moira.Logger, patterns []string) *PatternIndex { currentNode.Children = append(currentNode.Children, newNode) currentNode = newNode } + if i == len(parts)-1 { + currentNode.Terminal = true + } } } @@ -111,7 +115,7 @@ func (source *PatternIndex) MatchPatterns(metric string) []string { matched := make([]string, 0, found) for _, node := range currentLevel { - if len(node.Children) == 0 { + if node.Terminal { matched = append(matched, node.Prefix) } } diff --git a/filter/pattern_index_test.go b/filter/pattern_index_test.go index 11811123b..f7b5e2fe0 100644 --- a/filter/pattern_index_test.go +++ b/filter/pattern_index_test.go @@ -14,6 +14,7 @@ func TestPatternIndex(t *testing.T) { Convey("Given patterns, should build index and match patterns", t, func() { patterns := []string{ "Simple.matching.pattern", + "Simple.matching.pattern.*", "Star.single.*", "Star.*.double.any*", "Bracket.{one,two,three}.pattern",