Skip to content
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

Fix empty file edge case #36076

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions filebeat/input/filestream/fswatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ func (w *fileWatcher) watch(ctx unison.Canceler) {

// remaining files in newFiles are newly created files
for path, fd := range newFilesByName {
// no need to react on empty new files
if fd.Info.Size() == 0 {
w.log.Warnf("file %q has no content yet, skipping", fd.Filename)
continue
}
select {
case <-ctx.Done():
return
Expand Down
32 changes: 28 additions & 4 deletions filebeat/input/filestream/fswatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ scanner:
paths := []string{filepath.Join(dir, "*.log")}
cfgStr := `
scanner:
check_interval: 100ms
check_interval: 10ms
`

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

fw := createWatcherWithConfig(t, paths, cfgStr)
Expand Down Expand Up @@ -251,16 +251,40 @@ scanner:
require.Equal(t, loginp.OpDone, e.Op)
})

t.Run("does not emit events for empty files", func(t *testing.T) {
rdner marked this conversation as resolved.
Show resolved Hide resolved
dir := t.TempDir()
paths := []string{filepath.Join(dir, "*.log")}
cfgStr := `
scanner:
check_interval: 10ms
`

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

fw := createWatcherWithConfig(t, paths, cfgStr)
go fw.Run(ctx)

basename := "created.log"
filename := filepath.Join(dir, basename)
err := os.WriteFile(filename, nil, 0777)
require.NoError(t, err)

require.NoError(t, err)
e := fw.Event()
require.Equal(t, loginp.OpDone, e.Op)
})

t.Run("does not emit an event for a fingerprint collision", func(t *testing.T) {
dir := t.TempDir()
paths := []string{filepath.Join(dir, "*.log")}
cfgStr := `
scanner:
check_interval: 100ms
check_interval: 10ms
fingerprint.enabled: true
`

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

fw := createWatcherWithConfig(t, paths, cfgStr)
Expand Down