Skip to content

Commit

Permalink
removed cleanAsterisks
Browse files Browse the repository at this point in the history
- changing `*` to `.*` will result in unexpected behaviour when used
  with matchOperator.
- matchOperator should contain valid regexp by default.
- matchOperator when invalid should not panic but always return a
  non-matching callback
  • Loading branch information
lordvidex committed Aug 23, 2023
1 parent a353ba9 commit df0c8be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 72 deletions.
59 changes: 18 additions & 41 deletions filter/series_by_tag.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package filter

import (
"bytes"
"fmt"
"regexp"
"strings"

"github.com/moira-alert/moira"
)

var (
Expand Down Expand Up @@ -159,7 +156,9 @@ func CreateMatchingHandlerForPattern(tagSpecs []TagSpec) (string, MatchingHandle
}

func createMatchingHandlerForOneTag(spec TagSpec) MatchingHandler {
var matchingHandlerCondition func(string) bool
matchingHandlerCondition := func(_ string) bool {
return false
}
switch spec.Operator {
case EqualOperator:
matchingHandlerCondition = func(value string) bool {
Expand All @@ -170,19 +169,29 @@ func createMatchingHandlerForOneTag(spec TagSpec) MatchingHandler {
return value != spec.Value
}
case MatchOperator:
value := cleanAsterisks(spec.Value)
value := spec.Value
// work around
if value == "*" {
value = ".*"
}
if !strings.HasPrefix(value, "^") {
value = ".*" + value
}
if !strings.HasSuffix(value, "$") {
value += ".*"
}
matchRegex := regexp.MustCompile(value)
matchingHandlerCondition = func(value string) bool {
return matchRegex.MatchString(value)
matchRegex, err := regexp.Compile(value)
if err == nil {
matchingHandlerCondition = func(value string) bool {
return matchRegex.MatchString(value)
}
}
case NotMatchOperator:
value := cleanAsterisks(spec.Value)
value := spec.Value
// work around
if value == "*" {
value = ".*"
}
if !strings.HasPrefix(value, "^") {
value = ".*" + value
}
Expand All @@ -193,10 +202,6 @@ func createMatchingHandlerForOneTag(spec TagSpec) MatchingHandler {
matchingHandlerCondition = func(value string) bool {
return !matchRegex.MatchString(value)
}
default:
matchingHandlerCondition = func(_ string) bool {
return false
}
}

matchEmpty := matchingHandlerCondition("")
Expand All @@ -210,31 +215,3 @@ func createMatchingHandlerForOneTag(spec TagSpec) MatchingHandler {
return matchEmpty
}
}

// cleanAsterisks converts instances of "*" to ".*" wildcard match
func cleanAsterisks(s string) string {
// store `*` indices
positions := make([]int, 0)
for i := 0; i < len(s); i++ {
if s[i] == '*' {
positions = append(positions, i)
}
}
if len(positions) == 0 {
return s
}

b := moira.UnsafeStringToBytes(s)
var writer bytes.Buffer
writer.Grow(len(s) + len(positions))
writeIndex := 0
for _, i := range positions {
writer.Write(b[writeIndex:i])
if i == 0 || b[i-1] != '.' {
writer.WriteByte('.')
}
writeIndex = i
}
writer.Write(b[writeIndex:])
return writer.String()
}
23 changes: 23 additions & 0 deletions filter/series_by_tag_pattern_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,29 @@ func TestSeriesByTagPatternIndex(t *testing.T) {
"name=something;tag1=*",
},
},
{
"something",
map[string]string{"tag1": "val1", "tag2": "she"},
[]string{
"name=something;tag1=*",
"name=something;tag1=val1",
"name=something;tag1=val1;tag2=~*",
"name=something;tag1=~a",
"name=something;tag1=~a;tag2!=sh",
},
},
{
"something",
map[string]string{"tag1": "val1", "tag2": "shoe"},
[]string{
"name=something;tag1=*",
"name=something;tag1=val1",
"name=something;tag1=val1;tag2=~*",
"name=something;tag1=~a",
"name=something;tag1=~a;tag2!=sh",
"name=something;tag1=~a;tag2!=~sh*e",
},
},
}

index := NewSeriesByTagPatternIndex(logger, tagSpecsByPattern)
Expand Down
31 changes: 0 additions & 31 deletions filter/series_by_tag_test.go

This file was deleted.

0 comments on commit df0c8be

Please sign in to comment.