From 7f417aba9d0c531478242a190a275963a0ed00a9 Mon Sep 17 00:00:00 2001 From: VihasMakwana <121151420+VihasMakwana@users.noreply.github.com> Date: Wed, 21 Jun 2023 20:40:06 +0530 Subject: [PATCH] [chore][pkg/stanza/fileconsumer] Add utility functions (#23415) **Description:** Convert some code blocks into utility functions and improve readability. --- pkg/stanza/fileconsumer/file.go | 46 ++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/pkg/stanza/fileconsumer/file.go b/pkg/stanza/fileconsumer/file.go index 1e7ad414718e..d34e57e8920b 100644 --- a/pkg/stanza/fileconsumer/file.go +++ b/pkg/stanza/fileconsumer/file.go @@ -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) @@ -198,13 +194,15 @@ 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 { @@ -212,20 +210,38 @@ func (m *Manager) makeReader(path string) *Reader { 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)