From 73bbaae968c453aebc992407394fa41b6c3e3ac2 Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Mon, 10 Jul 2023 11:47:57 -0400 Subject: [PATCH] Add build test and remove layout overwrite --- .chloggen/fix-file-sort-layout-overwrite.yaml | 20 +++++++++++ pkg/stanza/fileconsumer/file_sort.go | 3 +- pkg/stanza/fileconsumer/file_test.go | 36 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 .chloggen/fix-file-sort-layout-overwrite.yaml diff --git a/.chloggen/fix-file-sort-layout-overwrite.yaml b/.chloggen/fix-file-sort-layout-overwrite.yaml new file mode 100644 index 000000000000..9a0a39975f7d --- /dev/null +++ b/.chloggen/fix-file-sort-layout-overwrite.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# 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: filelogreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix file sort timestamp layout being overwritten + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [24041] + +# (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: diff --git a/pkg/stanza/fileconsumer/file_sort.go b/pkg/stanza/fileconsumer/file_sort.go index aef0fe32ebc3..a97f40d4b662 100644 --- a/pkg/stanza/fileconsumer/file_sort.go +++ b/pkg/stanza/fileconsumer/file_sort.go @@ -94,11 +94,10 @@ func (f *TimestampSortRule) validate() error { f.Location = "UTC" } - layout, err := strptime.ToNative(f.Layout) + _, err := strptime.ToNative(f.Layout) if err != nil { return errors.Wrap(err, "parse strptime layout") } - f.Layout = layout return nil } diff --git a/pkg/stanza/fileconsumer/file_test.go b/pkg/stanza/fileconsumer/file_test.go index 1bdf0e748c3b..7646b74d58c5 100644 --- a/pkg/stanza/fileconsumer/file_test.go +++ b/pkg/stanza/fileconsumer/file_test.go @@ -705,6 +705,42 @@ func TestMultiFileSort(t *testing.T) { expectNoTokens(t, emitCalls) } +func TestMultiFileSortTimestamp(t *testing.T) { + t.Parallel() + + tempDir := t.TempDir() + cfg := NewConfig().includeDir(tempDir) + cfg.StartAt = "beginning" + cfg.MatchingCriteria.OrderingCriteria.Regex = `.*(?P\d).log` + cfg.MatchingCriteria.OrderingCriteria.SortBy = []SortRuleImpl{ + { + &TimestampSortRule{ + BaseSortRule: BaseSortRule{ + RegexKey: `value`, + SortType: "timestamp", + }, + Layout: "%Y%m%d%H", + }, + }, + } + + operator, emitCalls := buildTestManager(t, cfg) + + temp1 := openTempWithPattern(t, tempDir, ".*2023020602.log") + temp2 := openTempWithPattern(t, tempDir, ".*2023020603.log") + + writeString(t, temp1, "testlog1\n") + writeString(t, temp2, "testlog2\n") + + require.NoError(t, operator.Start(testutil.NewMockPersister("test"))) + defer func() { + require.NoError(t, operator.Stop()) + }() + + waitForTokens(t, emitCalls, [][]byte{[]byte("testlog2")}) + expectNoTokens(t, emitCalls) +} + func TestMultiFileParallel_PreloadedFiles(t *testing.T) { t.Parallel()