Skip to content

Commit

Permalink
feat(filter): Add error handling for regex compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru committed Oct 10, 2023
1 parent 015eb55 commit befb374
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
38 changes: 23 additions & 15 deletions filter/series_by_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,19 @@ func ParseSeriesByTag(input string) ([]TagSpec, error) {
type MatchingHandler func(string, map[string]string) bool

// CreateMatchingHandlerForPattern creates function for matching by tag list
func CreateMatchingHandlerForPattern(tagSpecs []TagSpec, compatibility *Compatibility) (string, MatchingHandler) {
func CreateMatchingHandlerForPattern(tagSpecs []TagSpec, compatibility *Compatibility) (string, MatchingHandler, error) {
matchingHandlers := make([]MatchingHandler, 0)
var nameTagValue string

for _, tagSpec := range tagSpecs {
if tagSpec.Name == "name" && tagSpec.Operator == EqualOperator {
nameTagValue = tagSpec.Value
} else {
handler := createMatchingHandlerForOneTag(tagSpec, compatibility)
handler, err := createMatchingHandlerForOneTag(tagSpec, compatibility)
if err != nil {
return "", nil, err
}

matchingHandlers = append(matchingHandlers, handler)
}
}
Expand All @@ -151,10 +155,10 @@ func CreateMatchingHandlerForPattern(tagSpecs []TagSpec, compatibility *Compatib
return true
}

return nameTagValue, matchingHandler
return nameTagValue, matchingHandler, nil
}

func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility) MatchingHandler {
func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility) (MatchingHandler, error) {
var matchingHandlerCondition func(string) bool
allowMatchEmpty := false

Expand All @@ -171,15 +175,21 @@ func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility)
case MatchOperator:
allowMatchEmpty = compatibility.AllowRegexMatchEmpty

matchRegex := newMatchRegex(spec.Value, compatibility)
matchRegex, err := newMatchRegex(spec.Value, compatibility)
if err != nil {
return nil, err
}

matchingHandlerCondition = func(value string) bool {
return matchRegex.MatchString(value)
}
case NotMatchOperator:
allowMatchEmpty = compatibility.AllowRegexMatchEmpty

matchRegex := newMatchRegex(spec.Value, compatibility)
matchRegex, err := newMatchRegex(spec.Value, compatibility)
if err != nil {
return nil, err
}

matchingHandlerCondition = func(value string) bool {
return !matchRegex.MatchString(value)
Expand All @@ -199,21 +209,19 @@ func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility)
return matchingHandlerCondition(value)
}
return allowMatchEmpty && matchEmpty
}
}, nil
}

func newMatchRegex(value string, compatibility *Compatibility) *regexp.Regexp {
var matchRegex *regexp.Regexp

func newMatchRegex(value string, compatibility *Compatibility) (*regexp.Regexp, error) {
if value == "*" {
value = ".*"
}

if compatibility.AllowRegexLooseStartMatch {
matchRegex = regexp.MustCompile(value)
} else {
matchRegex = regexp.MustCompile("^" + value)
if !compatibility.AllowRegexLooseStartMatch {
value = "^" + value
}

return matchRegex
matchRegex, err := regexp.Compile(value)

return matchRegex, err
}
10 changes: 9 additions & 1 deletion filter/series_by_tag_pattern_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ func NewSeriesByTagPatternIndex(
withoutStrictNameTagPatternMatchers := make(map[string]MatchingHandler)

for pattern, tagSpecs := range tagSpecsByPattern {
nameTagValue, matchingHandler := CreateMatchingHandlerForPattern(tagSpecs, &compatibility)
nameTagValue, matchingHandler, err := CreateMatchingHandlerForPattern(tagSpecs, &compatibility)

if err != nil {
logger.Info().
Error(err).
String("pattern", pattern).
Msg("Failed to create MatchingHandler for pattern")
continue
}

if nameTagValue == "" {
withoutStrictNameTagPatternMatchers[pattern] = matchingHandler
Expand Down

0 comments on commit befb374

Please sign in to comment.