Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filter): Add error handling for regex compilation #933

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может стоит добавить тесты на эту функцию? Я просто посмотрел, там на часть функций из файла series_by_tag.go есть тесты, а на другую часть нет, не обязательно прям в этом пр добавлять, но в целом они бы не помешали, я думаю

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо подумать, нужны ли тесты на эти функции или хватит тестов на самую выскокоуровневую. Тестируем-то логический модуль, а не каждую функцию по-отдельности. Но в этом ПР прям не хочется ещё тестов писать

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, в этом пр давайте оставим пока так, это пока просто мысли на будущее :)

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На этой функции также тестов нет

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аналогично комменту выше

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
kissken marked this conversation as resolved.
Show resolved Hide resolved
}

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