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

[chore][pkg/stanza/fileconsumer] Add utility functions #23415

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Changes from all commits
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
46 changes: 31 additions & 15 deletions pkg/stanza/fileconsumer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ func (m *Manager) consume(ctx context.Context, paths []string) {
m.clearCurrentFingerprints()
}

// makeReader take a file path, then creates reader,
// discarding any that have a duplicate fingerprint to other files that have already
// been read this polling interval
func (m *Manager) makeReader(path string) *Reader {
// Open the files first to minimize the time between listing and opening
func (m *Manager) makeFingerprint(path string) (*Fingerprint, *os.File) {
if _, ok := m.seenPaths[path]; !ok {
if m.readerFactory.fromBeginning {
m.Infow("Started watching file", "path", path)
Expand All @@ -198,34 +194,54 @@ func (m *Manager) makeReader(path string) *Reader {
file, err := os.Open(path) // #nosec - operator must read in files defined by user
if err != nil {
m.Debugf("Failed to open file", zap.Error(err))
return nil
return nil, nil
}

fp, err := m.readerFactory.newFingerprint(file)
if err != nil {
m.Errorw("Failed creating fingerprint", zap.Error(err))
return nil
if err = file.Close(); err != nil {
m.Errorf("problem closing file %s", file.Name())
}
return nil, nil
}

if len(fp.FirstBytes) == 0 {
// Empty file, don't read it until we can compare its fingerprint
if err = file.Close(); err != nil {
m.Errorf("problem closing file %s", file.Name())
}
return nil
return nil, nil
}
return fp, file
}

// Exclude any empty fingerprints or duplicate fingerprints to avoid doubling up on copy-truncate files
func (m *Manager) checkDuplicates(fp *Fingerprint) bool {
for i := 0; i < len(m.currentFps); i++ {
fp2 := m.currentFps[i]
if fp.StartsWith(fp2) || fp2.StartsWith(fp) {
// Exclude duplicates
if err = file.Close(); err != nil {
m.Errorf("problem closing file", "file", file.Name())
}
return nil
return true
}
}
return false
}

// makeReader take a file path, then creates reader,
// discarding any that have a duplicate fingerprint to other files that have already
// been read this polling interval
func (m *Manager) makeReader(path string) *Reader {
// Open the files first to minimize the time between listing and opening
fp, file := m.makeFingerprint(path)
if fp == nil {
return nil
}

// Exclude any empty fingerprints or duplicate fingerprints to avoid doubling up on copy-truncate files
if m.checkDuplicates(fp) {
if err := file.Close(); err != nil {
m.Errorf("problem closing file", "file", file.Name())
}
return nil
}

m.currentFps = append(m.currentFps, fp)
reader, err := m.newReader(file, fp)
Expand Down