Skip to content

Commit

Permalink
added a more detailed description of errors in buildRetentions function
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Aug 7, 2024
1 parent 90cc505 commit ec217d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
20 changes: 15 additions & 5 deletions filter/cache_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filter

import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
Expand Down Expand Up @@ -84,11 +85,18 @@ func (storage *Storage) buildRetentions(retentionScanner *bufio.Scanner) error {
continue
}

_, after, _ := strings.Cut(line1, "=")
_, after, found := strings.Cut(line1, "=")
if !found {
storage.logger.Error().
String("pattern_line", line1).
Msg(`Invalid pattern format, it is correct to write in the format "pattern = regex"`)
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()
Expand All @@ -97,22 +105,24 @@ func (storage *Storage) buildRetentions(retentionScanner *bufio.Scanner) error {

if len(splitted) < 2 { //nolint
storage.logger.Error().
String("pattern", patternString).
Msg("Invalid pattern found")
String("pattern_line", line1).
String("retentions_line", line2).
Msg(`Invalid retentions format, it is correct to write in the format "retentions = timePerPoint:timeToStore, timePerPoint:timeToStore, ..."`)
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{
pattern: pattern,
retention: retention,
})
}

return retentionScanner.Err()
}

Expand Down
1 change: 1 addition & 0 deletions filter/cache_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func TestRetentions(t *testing.T) {
So(matchedMetric.Retention, ShouldEqual, 10)
So(matchedMetric.RetentionTimestamp, ShouldEqual, 150)
})

Convey("should be default 120sec", func() {
matchedMetric := moira.MatchedMetric{
Metric: "my_super_metric;tag2=val2",
Expand Down

0 comments on commit ec217d7

Please sign in to comment.