Skip to content

Commit

Permalink
fix(filter): fix support custom retention for tagged metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kissken committed Oct 27, 2023
1 parent 18568f0 commit 576eacb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions filter/cache_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ func (storage *Storage) buildRetentions(retentionScanner *bufio.Scanner) error {

for retentionScanner.Scan() {
line1 := retentionScanner.Text()
if strings.HasPrefix(line1, "#") || strings.Count(line1, "=") != 1 {
if strings.HasPrefix(line1, "#") || strings.Count(line1, "=") < 1 {
continue
}

patternString := strings.TrimSpace(strings.Split(line1, "=")[1])
_, after, _ := strings.Cut(line1, "=")
patternString := strings.TrimSpace(after)
pattern, err := regexp.Compile(patternString)
if err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions filter/cache_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ var testRetentions = `
pattern = yearly$
retentions = 1y:100y
[tagged metrics]
pattern = ;tag1=val1(;.*)?$
retentions = 10s:2d,1m:30d,15m:1y
[default]
pattern = .*
retentions = 120:7d
Expand Down Expand Up @@ -229,4 +233,35 @@ func TestRetentions(t *testing.T) {
So(metr.Retention, ShouldEqual, 120)
So(metr.RetentionTimestamp, ShouldEqual, 120)
})

Convey("Tagged metrics", t, func() {
Convey("should be 10sec", func() {
matchedMetric := moira.MatchedMetric{
Metric: "my_super_metric;tag1=val1;tag2=val2",
Value: 12,
Timestamp: 151,
RetentionTimestamp: 0,
Retention: 10,
}
buffer := make(map[string]*moira.MatchedMetric)
storage.EnrichMatchedMetric(buffer, &matchedMetric)
So(len(buffer), ShouldEqual, 1)
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",
Value: 12,
Timestamp: 151,
RetentionTimestamp: 0,
Retention: 60,
}
buffer := make(map[string]*moira.MatchedMetric)
storage.EnrichMatchedMetric(buffer, &matchedMetric)
So(len(buffer), ShouldEqual, 1)
So(matchedMetric.Retention, ShouldEqual, 120)
So(matchedMetric.RetentionTimestamp, ShouldEqual, 120)
})
})
}

0 comments on commit 576eacb

Please sign in to comment.