-
Notifications
You must be signed in to change notification settings - Fork 28
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
Initialize new JsonEventWriters when layouting in JsonLayout #246
Merged
Conversation
This file contains 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
knittl
force-pushed
the
jsonlayout-threadsafe
branch
from
June 30, 2024 05:45
54ce92d
to
79e9a5e
Compare
nehaev
reviewed
Jul 1, 2024
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.
Thanks for your contribution, @knittl! Could you please address the comments above?
loki-logback-appender/src/main/java/com/github/loki4j/logback/JsonLayout.java
Outdated
Show resolved
Hide resolved
loki-logback-appender/src/test/java/com/github/loki4j/logback/JsonLayoutTest.java
Outdated
Show resolved
Hide resolved
knittl
force-pushed
the
jsonlayout-threadsafe
branch
from
July 2, 2024 06:15
4e16f32
to
e37115b
Compare
The doLayout method used a single JsonEventWriter field in a non-thread-safe way. Consequently, writing logs from multiple threads would corrupt its buffer and send malformed JSON to Loki. This patch initializes a new JsonEventWriter instance for every event that is layouted, similar to how Logback's `PatternLayout` is implemented. That way, each thread has its own writer instance (and buffer), guaranteeing that the buffer's content remains valid JSON. Allocated memory of the writer and buffer can be freed immediately after the method has returned.
knittl
force-pushed
the
jsonlayout-threadsafe
branch
from
July 2, 2024 06:22
e37115b
to
d037a5c
Compare
nehaev
approved these changes
Jul 2, 2024
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.
LGTM!
nehaev
pushed a commit
that referenced
this pull request
Jul 2, 2024
knittl
changed the title
Synchronize JsonLayout
Initialize new JsonEventWriters when layouting in JsonLayout
Jul 2, 2024
nehaev
pushed a commit
that referenced
this pull request
Jul 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The doLayout method used a single JsonEventWriter field in a non-thread-safe way. Consequently, writing logs from multiple threads would corrupt its buffer and send malformed JSON to Loki.
This patch initializes a new JsonEventWriter instance for every event that is layouted, similar to how Logback's
PatternLayout
is implemented. That way, each thread has its own writer instance (and buffer), guaranteeing that the buffer's content remains valid JSON. Allocated memory of the writer and buffer can be freed immediately after the method has returned.Alternatives include but are not limited to:
Closes #245