-
Notifications
You must be signed in to change notification settings - Fork 69
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
fix(filter): fix support custom retention for tagged metrics #950
Changes from 5 commits
576eacb
90cc505
ec217d7
6600553
d1f3600
de92374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package filter | |
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
"strconv" | ||
|
@@ -11,7 +13,12 @@ import ( | |
"github.com/moira-alert/moira/metrics" | ||
) | ||
|
||
var defaultRetention = 60 | ||
var ( | ||
defaultRetention = 60 | ||
|
||
InvalidRetentionsFormatErr = errors.New("Invalid retentions format, it is correct to write in the format 'retentions = timePerPoint:timeToStore, timePerPoint:timeToStore, ...'") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ошибки не должны быть с маленькой буквы? (у меня Goland подчеркнул) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Резонно, ошибка на текущий момент по сути наша внутренняя и снаружи не нужна There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Исправил |
||
InvalidPatternFormatErr = errors.New("Invalid pattern format, it is correct to write in the format 'pattern = regex'") | ||
) | ||
|
||
type retentionMatcher struct { | ||
pattern *regexp.Regexp | ||
|
@@ -82,33 +89,43 @@ func (storage *Storage) buildRetentions(retentionScanner *bufio.Scanner) error { | |
storage.retentions = make([]retentionMatcher, 0, 100) | ||
|
||
for retentionScanner.Scan() { | ||
line1 := retentionScanner.Text() | ||
if strings.HasPrefix(line1, "#") || strings.Count(line1, "=") != 1 { | ||
patternLine := retentionScanner.Text() | ||
if strings.HasPrefix(patternLine, "#") || strings.Count(patternLine, "=") < 1 { | ||
continue | ||
} | ||
|
||
patternString := strings.TrimSpace(strings.Split(line1, "=")[1]) | ||
_, after, found := strings.Cut(patternLine, "=") | ||
if !found { | ||
storage.logger.Error(). | ||
Error(InvalidPatternFormatErr). | ||
String("pattern_line", patternLine). | ||
Msg("Invalid pattern format") | ||
continue | ||
} | ||
|
||
patternString := strings.TrimSpace(after) | ||
pattern, err := regexp.Compile(patternString) | ||
if err != nil { | ||
return err | ||
return fmt.Errorf("failed to compile regexp pattern '%s': %w", patternString, err) | ||
} | ||
|
||
retentionScanner.Scan() | ||
line2 := retentionScanner.Text() | ||
splitted := strings.Split(line2, "=") | ||
retentionsLine := retentionScanner.Text() | ||
splitted := strings.Split(retentionsLine, "=") | ||
|
||
if len(splitted) < 2 { //nolint | ||
storage.logger.Error(). | ||
String("pattern", patternString). | ||
Msg("Invalid pattern found") | ||
|
||
Error(InvalidRetentionsFormatErr). | ||
String("pattern_line", patternLine). | ||
String("retentions_line", retentionsLine). | ||
Msg("Invalid retentions format") | ||
continue | ||
} | ||
|
||
retentions := strings.TrimSpace(splitted[1]) | ||
retention, err := rawRetentionToSeconds(retentions[0:strings.Index(retentions, ":")]) | ||
if err != nil { | ||
return err | ||
return fmt.Errorf("failed to convert raw retentions '%s' to seconds: %w", retentions, err) | ||
} | ||
|
||
storage.retentions = append(storage.retentions, retentionMatcher{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мб константой эту штуку сделать? Вроде нигде ей ничего не присваивается.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сделал