forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/filelog] Implement
ExcludeOlderThan
matcher criterion (op…
…en-telemetry#31916) **Description:** This PR implements a new matcher criterion in the Stanza fileconsumer matcher: ``` ExcludeOlderThan time.Duration `mapstructure:"exclude_older_than"` ``` and the corresponding setting in the `filelog` receiver configuration: | Field | Default | Description | |-------------------------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `exclude_older_than` | | Exclude files whose modification time is older than the specified age. | When specified, the matcher will exclude files whose modification times are older than the specified time. **Link to tracking Issue:** open-telemetry#31053 **Testing:** Added unit tests. **Documentation:** Documented `exclude_older_than` configuration setting in the `filelogreceiver`'s README. --------- Co-authored-by: Daniel Jaglowski <[email protected]>
- Loading branch information
1 parent
7f3e7a4
commit b93f1d7
Showing
6 changed files
with
212 additions
and
19 deletions.
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