-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[receiver/filelog] Suppress repeated permission denied errors #44350
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
17e2ed9
[receiver/filelog] Suppress repeated permission denied errors
khpeet 027e344
Merge branch 'open-telemetry:main' into main
khpeet 26258b9
chore: lint fixes
khpeet 2ef7438
Merge branch 'open-telemetry:main' into main
khpeet b0126a1
chore: remove re-init of unreadable;fix unreadable test
khpeet 6058c23
chore: testLint cleanup
khpeet 6a2a220
Merge branch 'open-telemetry:main' into main
khpeet 1dd2a33
chore: remove mutex
khpeet 73071ab
Merge branch 'open-telemetry:main' into main
khpeet 993e8b0
Merge branch 'open-telemetry:main' into main
khpeet 1019893
Merge branch 'main' into main
khpeet 3e4aa14
Merge branch 'open-telemetry:main' into main
khpeet b13f238
chore: limit size of unreadable map;misc enhancements
khpeet 24f92ea
Merge branch 'open-telemetry:main' into main
khpeet 1a1d5bd
Merge branch 'open-telemetry:main' into main
khpeet bbb6ce4
chore: latest rounds of changes
khpeet 7c787b6
chore: update test to reflect updated unreadable log message
khpeet 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 hidden or 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,29 @@ | ||
| # 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. receiver/filelog) | ||
| component: receiver/filelog | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Suppress repeated permission-denied errors | ||
|
|
||
| # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
| issues: [39491] | ||
|
|
||
| # (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: | | ||
| Only one error is logged per file per process run, and an informational message is emitted when the file becomes readable again. | ||
| This reduces log spam and improves clarity for operators. | ||
|
|
||
| # 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] |
This file contains hidden or 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 hidden or 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,165 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package fileconsumer | ||
|
|
||
| import ( | ||
| "os" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/collector/component/componenttest" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| "go.uber.org/zap/zaptest/observer" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/filetest" | ||
| "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil" | ||
| ) | ||
|
|
||
| // TestUnreadableFileLoggedOnce verifies that permission-denied errors when | ||
| // opening files are logged only once per file per process run, and that an | ||
| // informational message is emitted when the file later becomes readable. | ||
| func TestUnreadableFileLoggedOnce(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| t.Skip("permission manipulation tests are not reliable on Windows") | ||
| } | ||
|
|
||
| t.Parallel() | ||
|
|
||
| tempDir := t.TempDir() | ||
| cfg := NewConfig().includeDir(tempDir) | ||
| operator, _ := testManager(t, cfg) | ||
|
|
||
| // Create a file and remove permissions so open will fail | ||
| f := filetest.OpenTemp(t, tempDir) | ||
| _, err := f.WriteString("abc\n") | ||
| require.NoError(t, err) | ||
| require.NoError(t, f.Close()) | ||
| require.NoError(t, os.Chmod(f.Name(), 0)) | ||
|
|
||
| core, obs := observer.New(zapcore.DebugLevel) | ||
| logger := zap.New(core) | ||
| set := componenttest.NewNopTelemetrySettings() | ||
| set.Logger = logger | ||
|
|
||
| operator.set.Logger = set.Logger | ||
| require.NoError(t, operator.Start(testutil.NewUnscopedMockPersister())) | ||
| defer func() { | ||
| require.NoError(t, operator.Stop()) | ||
| }() | ||
|
|
||
| // First poll should attempt to open and log an error once | ||
| operator.poll(t.Context()) | ||
|
|
||
| // Verify the unreadable map recorded the path and exactly one error was logged | ||
| require.Eventually(t, func() bool { | ||
| return len(operator.unreadable) == 1 | ||
| }, 2*time.Second, 10*time.Millisecond, "expected unreadable map to have one entry after first poll") | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| countErrMsgs := 0 | ||
| for _, e := range obs.All() { | ||
| if e.Level == zapcore.ErrorLevel && e.Message == "Failed to open file - unreadable" { | ||
| countErrMsgs++ | ||
| } | ||
| } | ||
| return countErrMsgs == 1 | ||
| }, 2*time.Second, 10*time.Millisecond, "expected exactly one 'Failed to open file - unreadable' error after first poll") | ||
|
|
||
| // Second poll should not add another error-level log for the same path | ||
| operator.poll(t.Context()) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| countErrMsgs := 0 | ||
| for _, e := range obs.All() { | ||
| if e.Level == zapcore.ErrorLevel && e.Message == "Failed to open file - unreadable" { | ||
| countErrMsgs++ | ||
| } | ||
| } | ||
| return countErrMsgs == 1 | ||
| }, 2*time.Second, 10*time.Millisecond, "expected still exactly one 'Failed to open file - unreadable' error after second poll") | ||
|
|
||
| // Verify the unreadable map still contains the entry (no reinitialization) | ||
| require.Len(t, operator.unreadable, 1, "expected unreadable map to still have one entry after second poll") | ||
|
|
||
| // Now make the file readable again and poll; should emit an info message | ||
| require.NoError(t, os.Chmod(f.Name(), 0o644)) | ||
| operator.poll(t.Context()) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| for _, e := range obs.All() { | ||
| if e.Level == zapcore.InfoLevel && e.Message == "Previously unreadable file is now readable" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| }, 2*time.Second, 10*time.Millisecond, "expected at least one info message when file becomes readable") | ||
| } | ||
|
|
||
| // TestNonPermissionErrorsAlwaysLogged verifies that errors other than permission | ||
| // errors (e.g., file not found) are always logged and not suppressed. | ||
| func TestNonPermissionErrorsAlwaysLogged(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tempDir := t.TempDir() | ||
| cfg := NewConfig().includeDir(tempDir) | ||
| operator, _ := testManager(t, cfg) | ||
|
|
||
| core, obs := observer.New(zapcore.DebugLevel) | ||
| logger := zap.New(core) | ||
| set := componenttest.NewNopTelemetrySettings() | ||
| set.Logger = logger | ||
|
|
||
| operator.set.Logger = set.Logger | ||
| require.NoError(t, operator.Start(testutil.NewUnscopedMockPersister())) | ||
| defer func() { | ||
| require.NoError(t, operator.Stop()) | ||
| }() | ||
|
|
||
| // Directly call makeFingerprint with a non-existent file path | ||
| // This simulates a file being deleted after matching but before opening | ||
| nonExistentPath := tempDir + "/non_existent_file.log" | ||
|
|
||
| // First call should log an error | ||
| fp, file := operator.makeFingerprint(nonExistentPath) | ||
| require.Nil(t, fp) | ||
| require.Nil(t, file) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| for _, e := range obs.All() { | ||
| if e.Level == zapcore.ErrorLevel && e.Message == "Failed to open file" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| }, 2*time.Second, 10*time.Millisecond, "expected an error log on first call for non-existent file") | ||
|
|
||
| countBeforeSecondCall := 0 | ||
| for _, e := range obs.All() { | ||
| if e.Level == zapcore.ErrorLevel && e.Message == "Failed to open file" { | ||
| countBeforeSecondCall++ | ||
| } | ||
| } | ||
|
|
||
| // Second call should also log an error (not suppressed like permission errors) | ||
| fp, file = operator.makeFingerprint(nonExistentPath) | ||
| require.Nil(t, fp) | ||
| require.Nil(t, file) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| countAfterSecondCall := 0 | ||
| for _, e := range obs.All() { | ||
| if e.Level == zapcore.ErrorLevel && e.Message == "Failed to open file" { | ||
| countAfterSecondCall++ | ||
| } | ||
| } | ||
| // Should have at least one more error than before | ||
| return countAfterSecondCall > countBeforeSecondCall | ||
| }, 2*time.Second, 10*time.Millisecond, "expected another error log on second call for non-permission errors") | ||
|
|
||
| // Verify the unreadable map is empty (non-permission errors shouldn't be tracked) | ||
| require.Empty(t, operator.unreadable, "expected unreadable map to be empty for non-permission errors") | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.