From b7d2395d54574a60eb51668ea49d60c7fa430c64 Mon Sep 17 00:00:00 2001 From: Peter Deng Date: Fri, 26 Apr 2024 21:28:03 +0800 Subject: [PATCH] Fix issue when `exclude_older_than` is enabled without `ordering_criteria` configured (#32683) **Description:** Fix issue when `exclude_older_than` is enabled without `ordering_criteria` configured **Link to tracking Issue:** #32681 **Testing:** **Documentation:** --- .chloggen/fix-stanza-matcher.yaml | 27 +++++++++++++++++++ .../matcher/internal/filter/sort.go | 10 +++++++ pkg/stanza/fileconsumer/matcher/matcher.go | 11 +++----- 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 .chloggen/fix-stanza-matcher.yaml diff --git a/.chloggen/fix-stanza-matcher.yaml b/.chloggen/fix-stanza-matcher.yaml new file mode 100644 index 000000000000..97b1c50f815a --- /dev/null +++ b/.chloggen/fix-stanza-matcher.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/stanza + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Fix issue when `exclude_older_than` is enabled without `ordering_criteria` configured" + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [32681] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/pkg/stanza/fileconsumer/matcher/internal/filter/sort.go b/pkg/stanza/fileconsumer/matcher/internal/filter/sort.go index 67002f7e3405..a8da985b5b70 100644 --- a/pkg/stanza/fileconsumer/matcher/internal/filter/sort.go +++ b/pkg/stanza/fileconsumer/matcher/internal/filter/sort.go @@ -132,6 +132,16 @@ func SortTemporal(regexKey string, ascending bool, layout string, location strin ) } +type TopNOption int + +//nolint:unparam +func (t TopNOption) apply(items []*item) ([]*item, error) { + if len(items) <= int(t) { + return items, nil + } + return items[:t], nil +} + type mtimeSortOption struct{} type mtimeItem struct { diff --git a/pkg/stanza/fileconsumer/matcher/matcher.go b/pkg/stanza/fileconsumer/matcher/matcher.go index a01a9fe1641a..adcf42da6909 100644 --- a/pkg/stanza/fileconsumer/matcher/matcher.go +++ b/pkg/stanza/fileconsumer/matcher/matcher.go @@ -91,7 +91,6 @@ func New(c Criteria) (*Matcher, error) { if c.OrderingCriteria.TopN == 0 { c.OrderingCriteria.TopN = defaultOrderingCriteriaTopN } - m.topN = c.OrderingCriteria.TopN var regex *regexp.Regexp if orderingCriteriaNeedsRegex(c.OrderingCriteria.SortBy) { @@ -138,6 +137,8 @@ func New(c Criteria) (*Matcher, error) { } } + m.filterOpts = append(m.filterOpts, filter.TopNOption(c.OrderingCriteria.TopN)) + return m, nil } @@ -156,7 +157,6 @@ type Matcher struct { include []string exclude []string regex *regexp.Regexp - topN int filterOpts []filter.Option } @@ -178,10 +178,5 @@ func (m Matcher) MatchFiles() ([]string, error) { if len(result) == 0 { return result, errors.Join(err, errs) } - - if len(result) <= m.topN { - return result, errors.Join(err, errs) - } - - return result[:m.topN], errors.Join(err, errs) + return result, errs }