-
Notifications
You must be signed in to change notification settings - Fork 593
fix: Fix bugs when loki.source.file uses non-UTF-8 encoding
#5210
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
Closed
Closed
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -149,19 +149,30 @@ func (f *File) Stop() error { | |
| // wait blocks until a file event is detected (modification, truncation, or deletion). | ||
| // Returns an error if the context is canceled or an unrecoverable error occurs. | ||
| func (f *File) wait(partial bool) error { | ||
| offset, err := f.offset() | ||
| // Use file size instead of calculated offset for blockUntilEvent. | ||
| // This is important when using a decoder (e.g., UTF-16) because the offset() | ||
| // calculation mixes raw file bytes with decoded bytes, which are incompatible | ||
| // units and can cause incorrect comparisons. | ||
| fi, err := f.file.Stat() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fileSize := fi.Size() | ||
|
|
||
| event, err := blockUntilEvent(f.ctx, f.file, offset, f.cfg) | ||
| event, err := blockUntilEvent(f.ctx, f.file, fileSize, f.cfg) | ||
| switch event { | ||
| case eventModified: | ||
| level.Debug(f.logger).Log("msg", "file modified") | ||
| if partial { | ||
| // We need to reset to last successful offset because we consumed a partial line. | ||
| // TODO: Return error from Seek()? | ||
|
blewis12 marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine to return an error here! |
||
| f.file.Seek(f.lastOffset, io.SeekStart) | ||
| f.reader.Reset(f.file) | ||
| f.resetReader() | ||
| } else if f.cfg.Decoder != nil { | ||
| // Reset the reader when a decoder is configured. Once a decoder's underlying | ||
| // transform.Reader returns EOF, it won't read more data even if new content | ||
| // is appended. Resetting creates a fresh decoder that can read the new content. | ||
| f.resetReader() | ||
| } | ||
| return nil | ||
| case eventTruncated: | ||
|
|
@@ -186,9 +197,11 @@ func (f *File) wait(partial bool) error { | |
| // The newline and any trailing carriage return (for Windows line endings) are stripped. | ||
| func (f *File) readLine() (string, error) { | ||
| line, err := f.reader.ReadString('\n') | ||
|
|
||
| if err != nil { | ||
| return line, err | ||
| } | ||
|
|
||
| return strings.TrimRight(line, "\r\n"), err | ||
| } | ||
|
|
||
|
|
@@ -200,7 +213,7 @@ func (f *File) drain() { | |
| if _, err := f.file.Seek(f.lastOffset, io.SeekStart); err != nil { | ||
| return | ||
| } | ||
| f.reader.Reset(f.file) | ||
| f.resetReader() | ||
|
|
||
| for { | ||
| text, err := f.readLine() | ||
|
|
@@ -298,13 +311,21 @@ func (f *File) reopen(truncated bool) error { | |
| } | ||
|
|
||
| f.file = file | ||
| f.reader.Reset(f.file) | ||
| f.resetReader() | ||
| break | ||
| } | ||
|
|
||
| return backoff.Err() | ||
| } | ||
|
|
||
| // resetReader recreates the reader chain, preserving the decoder if one is configured. | ||
| // This is necessary when seeking to a new position in the file, as the decoder may have | ||
| // internal state that needs to be reset. Simply calling bufio.Reader.Reset() would | ||
| // bypass the decoder and read raw bytes instead of decoded content. | ||
| func (f *File) resetReader() { | ||
| f.reader = newReader(f.file, f.cfg) | ||
|
blewis12 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func newReader(f *os.File, cfg *Config) *bufio.Reader { | ||
| if cfg.Decoder != nil { | ||
| return bufio.NewReader(cfg.Decoder.Reader(f)) | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure this is correct. We used offset because we are interested to get events from where we last read.
If new data has come in between us trying to read a line and calling wait we would miss that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I think you're right 🤔 I'll see if I can get offset() to only use raw bytes.