-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[receiver/filelog] Implement ExcludeOlderThan
matcher criterion
#31916
Merged
djaglowski
merged 17 commits into
open-telemetry:main
from
ycombinator:receiver-filelog-exclude-older-than
Apr 15, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7f69039
Implement ExcludeOlderThan matcher criterion
ycombinator 814777e
Use duration for option value
ycombinator 56c0ee3
Document setting
ycombinator d79fa12
Adding CHANGELOG entry
ycombinator 2b6392a
Undo change
ycombinator a2f854d
Running make fmt
ycombinator bc11769
Disambiguate variable name from package name
ycombinator ced13b6
Simplify code
ycombinator a5d0294
Fix syntax error
ycombinator 6b3d1df
Compare ages for every match
ycombinator 8377b99
Modify same matcher instance
ycombinator e7828cd
Move ExcludeOlderThan filter to own file
ycombinator 6d1b434
Add test case for ExcludeOlderThan in matcher unit test
ycombinator abc2b8f
Use time.Since() instead of time.Now().Sub()
ycombinator 0d32a53
Fix imports
ycombinator 5d3cbd1
Merge branch 'main' into receiver-filelog-exclude-older-than
djaglowski 2d4e7c3
Merge branch 'main' into receiver-filelog-exclude-older-than
djaglowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: enhancement | ||
|
||
# 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: "Add `exclude_older_than` configuration setting" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [31053] | ||
|
||
# (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, api] |
40 changes: 40 additions & 0 deletions
40
pkg/stanza/fileconsumer/matcher/internal/filter/exclude.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filter // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher/internal/filter" | ||
import ( | ||
"os" | ||
"time" | ||
|
||
"go.uber.org/multierr" | ||
) | ||
|
||
type excludeOlderThanOption struct { | ||
age time.Duration | ||
} | ||
|
||
func (eot excludeOlderThanOption) apply(items []*item) ([]*item, error) { | ||
filteredItems := make([]*item, 0, len(items)) | ||
var errs error | ||
for _, item := range items { | ||
fi, err := os.Stat(item.value) | ||
if err != nil { | ||
errs = multierr.Append(errs, err) | ||
continue | ||
} | ||
|
||
// Keep (include) the file if its age (since last modification) | ||
// is the same or less than the configured age. | ||
fileAge := time.Since(fi.ModTime()) | ||
if fileAge <= eot.age { | ||
filteredItems = append(filteredItems, item) | ||
} | ||
} | ||
|
||
return filteredItems, errs | ||
} | ||
|
||
// ExcludeOlderThan excludes files whose modification time is older than the specified age. | ||
func ExcludeOlderThan(age time.Duration) Option { | ||
return excludeOlderThanOption{age: age} | ||
} |
110 changes: 110 additions & 0 deletions
110
pkg/stanza/fileconsumer/matcher/internal/filter/exclude_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filter | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExcludeOlderThanFilter(t *testing.T) { | ||
twoHoursAgo := time.Now().Add(-2 * time.Hour) | ||
threeHoursAgo := twoHoursAgo.Add(-1 * time.Hour) | ||
|
||
cases := map[string]struct { | ||
files []string | ||
fileMTimes []time.Time | ||
excludeOlderThan time.Duration | ||
|
||
expect []string | ||
expectedErr string | ||
}{ | ||
"no_files": { | ||
files: []string{}, | ||
fileMTimes: []time.Time{}, | ||
excludeOlderThan: 2 * time.Hour, | ||
|
||
expect: []string{}, | ||
expectedErr: "", | ||
}, | ||
"exclude_no_files": { | ||
files: []string{"a.log", "b.log"}, | ||
fileMTimes: []time.Time{twoHoursAgo, twoHoursAgo}, | ||
excludeOlderThan: 3 * time.Hour, | ||
|
||
expect: []string{"a.log", "b.log"}, | ||
expectedErr: "", | ||
}, | ||
"exclude_some_files": { | ||
files: []string{"a.log", "b.log"}, | ||
fileMTimes: []time.Time{twoHoursAgo, threeHoursAgo}, | ||
excludeOlderThan: 3 * time.Hour, | ||
|
||
expect: []string{"a.log"}, | ||
expectedErr: "", | ||
}, | ||
"exclude_all_files": { | ||
files: []string{"a.log", "b.log"}, | ||
fileMTimes: []time.Time{twoHoursAgo, threeHoursAgo}, | ||
excludeOlderThan: 90 * time.Minute, | ||
|
||
expect: []string{}, | ||
expectedErr: "", | ||
}, | ||
"file_not_present": { | ||
files: []string{"a.log", "b.log"}, | ||
fileMTimes: []time.Time{twoHoursAgo, {}}, | ||
excludeOlderThan: 3 * time.Hour, | ||
|
||
expect: []string{"a.log"}, | ||
expectedErr: "b.log: no such file or directory", | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
var items []*item | ||
// Create files with specified mtime | ||
for i, file := range tc.files { | ||
mtime := tc.fileMTimes[i] | ||
fullPath := filepath.Join(tmpDir, file) | ||
|
||
// Only create file if mtime is specified | ||
if !mtime.IsZero() { | ||
f, err := os.Create(fullPath) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
require.NoError(t, os.Chtimes(fullPath, twoHoursAgo, mtime)) | ||
} | ||
|
||
it, err := newItem(fullPath, nil) | ||
require.NoError(t, err) | ||
|
||
items = append(items, it) | ||
} | ||
|
||
f := ExcludeOlderThan(tc.excludeOlderThan) | ||
result, err := f.apply(items) | ||
if tc.expectedErr != "" { | ||
require.ErrorContains(t, err, tc.expectedErr) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
|
||
relativeResult := make([]string, 0, len(result)) | ||
for _, r := range result { | ||
rel, err := filepath.Rel(tmpDir, r.value) | ||
require.NoError(t, err) | ||
relativeResult = append(relativeResult, rel) | ||
} | ||
|
||
require.Equal(t, tc.expect, relativeResult) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be a followup PR but I think the test is set up a bit strange here because we're comparing "exactly 3 hours" to "3 hour + trivial time" (because we initialize
threeHoursAgo
above and compare it just a few cycles later.) I think we should instead make this an unambiguous comparison and consider ways to test the exactly equal case.