Skip to content

fix: perform drain when file is deleted in tail package#5139

Merged
kalleep merged 4 commits intomainfrom
kalleep/tailer-partial-read
Dec 18, 2025
Merged

fix: perform drain when file is deleted in tail package#5139
kalleep merged 4 commits intomainfrom
kalleep/tailer-partial-read

Conversation

@kalleep
Copy link
Contributor

@kalleep kalleep commented Dec 17, 2025

PR Description

After the refactor of tail package, and maybe even before that, we have the potential to miss log lines when files are rotated.

If we got to EOF and we detected a delete event we would ignore whats left for that file. It could be that a partial line read before was fully flushed or even more lines.

So to handle this I added bufferedLines. If we detect a delete event we use the open fd we have and drain all remaning lines into that and then perform the reopen.

On reads we check if we have any lines buffered and reads from that first.

Which issue(s) this PR fixes

Notes to the Reviewer

PR Checklist

  • Documentation added
  • Tests updated
  • Config converters updated

BEGIN_COMMIT_OVERRIDE
fix: Perform drain when file is deleted in tail package (#5139)
END_COMMIT_OVERRIDE

@kalleep kalleep requested a review from a team as a code owner December 17, 2025 13:36
@kalleep kalleep changed the title tailer: perform a drain when file is deleted chore: perform a drain when file is deleted in tail package Dec 17, 2025
@kalleep kalleep changed the title chore: perform a drain when file is deleted in tail package chore: perform drain when file is deleted in tail package Dec 17, 2025
@kalleep kalleep changed the title chore: perform drain when file is deleted in tail package chore: perform drain when file is deleted in tail package Dec 18, 2025
@thampiotr thampiotr requested a review from Copilot December 18, 2025 09:31
@kalleep kalleep changed the title chore: perform drain when file is deleted in tail package fix: perform drain when file is deleted in tail package Dec 18, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a potential data loss issue during file rotation in the tail package. The main change introduces a drain() function that reads all remaining lines from an old file handle before it's closed during rotation, storing them in a buffer to be returned before reading from the new file.

Key changes:

  • Added buffering mechanism (bufferedLines) to preserve lines from rotated files
  • Implemented drain() function to read remaining lines when file deletion is detected
  • Enhanced logging consistency by adding path to logger context and standardizing error log keys

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
internal/component/loki/source/file/internal/tail/file.go Core implementation of drain functionality, bufferedLines field, and improved debug logging for file events
internal/component/loki/source/file/internal/tail/file_test.go Added test case to verify drain behavior during file rotation and helper function for rotation simulation
internal/component/loki/source/file/tailer.go Logging improvements: added path to logger context and standardized error log key from "err" to "error"

kalleep and others added 2 commits December 18, 2025 10:49
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@kalleep kalleep force-pushed the kalleep/tailer-partial-read branch from ac85f44 to 8c40953 Compare December 18, 2025 10:01
@kalleep kalleep merged commit 2e48867 into main Dec 18, 2025
45 checks passed
@kalleep kalleep deleted the kalleep/tailer-partial-read branch December 18, 2025 10:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants