diff --git a/pkg/stanza/fileconsumer/matcher/matcher.go b/pkg/stanza/fileconsumer/matcher/matcher.go index a4ce674c2a84..0377bfab3a61 100644 --- a/pkg/stanza/fileconsumer/matcher/matcher.go +++ b/pkg/stanza/fileconsumer/matcher/matcher.go @@ -81,6 +81,14 @@ func New(c Criteria) (*Matcher, error) { m.filterOpts = append(m.filterOpts, filter.ExcludeOlderThan(c.ExcludeOlderThan)) } + if c.OrderingCriteria.GroupBy != "" { + r, err := regexp.Compile(c.OrderingCriteria.GroupBy) + if err != nil { + return nil, fmt.Errorf("compile group_by regex: %w", err) + } + m.groupBy = r + } + if len(c.OrderingCriteria.SortBy) == 0 { return m, nil } @@ -93,14 +101,6 @@ func New(c Criteria) (*Matcher, error) { c.OrderingCriteria.TopN = defaultOrderingCriteriaTopN } - if c.OrderingCriteria.GroupBy != "" { - r, err := regexp.Compile(c.OrderingCriteria.GroupBy) - if err != nil { - return nil, fmt.Errorf("compile group_by regex: %w", err) - } - m.groupBy = r - } - if orderingCriteriaNeedsRegex(c.OrderingCriteria.SortBy) { if c.OrderingCriteria.Regex == "" { return nil, fmt.Errorf("'regex' must be specified when 'sort_by' is specified") @@ -197,8 +197,8 @@ func (m Matcher) MatchFiles() ([]string, error) { } var result []string - for _, files := range groups { - groupResult, err := filter.Filter(files, m.regex, m.filterOpts...) + for _, groupedFiles := range groups { + groupResult, err := filter.Filter(groupedFiles, m.regex, m.filterOpts...) if len(groupResult) == 0 { return groupResult, errors.Join(err, errs) } diff --git a/pkg/stanza/fileconsumer/matcher/matcher_test.go b/pkg/stanza/fileconsumer/matcher/matcher_test.go index 9b078cac35e5..e95c9d07cda4 100644 --- a/pkg/stanza/fileconsumer/matcher/matcher_test.go +++ b/pkg/stanza/fileconsumer/matcher/matcher_test.go @@ -69,6 +69,15 @@ func TestNew(t *testing.T) { }, expectedErr: "exclude: parse glob: syntax error in pattern", }, + { + name: "GroupBy", + criteria: Criteria{ + Include: []string{"*.log"}, + OrderingCriteria: OrderingCriteria{ + GroupBy: "[a-z]", + }, + }, + }, { name: "RegexEmpty", criteria: Criteria{ @@ -118,6 +127,16 @@ func TestNew(t *testing.T) { }, expectedErr: "'top_n' must be a positive integer", }, + { + name: "GroupBy error", + criteria: Criteria{ + Include: []string{"*.log"}, + OrderingCriteria: OrderingCriteria{ + GroupBy: "[a-z", + }, + }, + expectedErr: "compile group_by regex: error parsing regexp: missing closing ]: `[a-z`", + }, { name: "SortTypeEmpty", criteria: Criteria{ @@ -384,6 +403,43 @@ func TestMatcher(t *testing.T) { }, expected: []string{"err.123456789.log"}, }, + { + name: "Numeric Sorting", + files: []string{"err.a.123456788.log", "err.a.123456789.log", "err.a.123456787.log", "err.a.123456786.log", "err.b.123456788.log", "err.b.123456789.log"}, + include: []string{"err.*.log"}, + exclude: []string{}, + filterCriteria: OrderingCriteria{ + TopN: 6, + Regex: `err\.[a-z]\.(?P\d+).*log`, + SortBy: []Sort{ + { + SortType: sortTypeNumeric, + RegexKey: "value", + Ascending: false, + }, + }, + }, + expected: []string{"err.a.123456789.log", "err.b.123456789.log", "err.a.123456788.log", "err.b.123456788.log", "err.a.123456787.log", "err.a.123456786.log"}, + }, + { + name: "Numeric Sorting with grouping", + files: []string{"err.a.123456788.log", "err.a.123456789.log", "err.a.123456787.log", "err.a.123456786.log", "err.b.123456788.log", "err.b.123456789.log"}, + include: []string{"err.*.log"}, + exclude: []string{}, + filterCriteria: OrderingCriteria{ + TopN: 6, + GroupBy: `err\.(?P[a-z]+).[0-9]*.*log`, + Regex: `err\.[a-z]\.(?P\d+).*log`, + SortBy: []Sort{ + { + SortType: sortTypeNumeric, + RegexKey: "value", + Ascending: false, + }, + }, + }, + expected: []string{"err.a.123456789.log", "err.a.123456788.log", "err.a.123456787.log", "err.a.123456786.log", "err.b.123456789.log", "err.b.123456788.log"}, + }, { name: "Numeric Sorting Ascending", files: []string{"err.123456789.log", "err.123456788.log", "err.123456786.log", "err.123456787.log"}, @@ -786,7 +842,7 @@ func TestMatcher(t *testing.T) { } else { assert.NoError(t, err) } - assert.ElementsMatch(t, tc.expected, files) + assert.Equal(t, tc.expected, files) }) } }