Skip to content

Commit

Permalink
fix(filter): Fix pattern matching with extra segment at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
beevee committed Mar 18, 2020
1 parent 433b5fb commit ea96709
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions filter/pattern_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions filter/pattern_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ea96709

Please sign in to comment.