fix(logs): optimize LogRecord memory by removing redundant context#4977
Open
ajuijas wants to merge 4 commits intoopen-telemetry:mainfrom
Open
fix(logs): optimize LogRecord memory by removing redundant context#4977ajuijas wants to merge 4 commits intoopen-telemetry:mainfrom
ajuijas wants to merge 4 commits intoopen-telemetry:mainfrom
Conversation
Removed direct storage of the Context object in LogRecord to prevent memory inflation when logs are buffered. Correlation IDs (TraceId, SpanId, TraceFlags) are still preserved. Resolves open-telemetry#4957
45c7255 to
c77ecd0
Compare
Contributor
|
Not sure if we want to completely remove the entire context from each log record since this will prevent log processors from accessing other useful information from the current context (e.g. baggage). Not sure how common these use cases might be, so it might be fine regardless. |
…etry#4957) Clear LogRecord context references before buffering in the SDK to prevent memory inflation while maintaining context access for synchronous processors.
…ntext" This reverts commit c77ecd0.
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
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.
Optimized LogRecord memory usage in BatchLogRecordProcessor by clearing the Context object before buffering. Correlation IDs (
TraceId,SpanId,TraceFlags) are still preserved.Resolves #4957
Description
This PR optimizes memory usage in the Logging SDK by implementing a targeted context-clearing strategy in the BatchLogRecordProcessor.
Motivation:
Currently, each LogRecord stores a reference to the full Context it was created with. In high-throughput scenarios where logs are emitted within unique and large contexts (e.g., each request having unique baggage), these objects are pinned in memory as long as the logs are buffered in a processor like BatchLogRecordProcessor. This leads to significant memory inflation that scales with both the number of buffered logs and the size of the contexts.
Solution:
To fix the memory leak without breaking context access (e.g., for Baggage), we keep the context attribute in the API but clear it in the BatchLogRecordProcessor right before buffering. We do this by creating a shallow copy of the LogRecord and setting its context to
None. This severs the memory-heavy reference before the log enters the long-lived queue, keeping the API compatible while achieving the performance fix.Changes:
opentelemetry-sdk: UpdatedBatchLogRecordProcessor.on_emitto shallow-copy the record and clear its context reference.opentelemetry-sdk: Addedimport copyto the SDK export module.Fixes #4957
Type of change
How Has This Been Tested?
I performed local benchmarking and ran the full unit test suite to ensure correctness and quantify the memory savings.
log_record.contextduring emission.opentelemetry-apiandopentelemetry-sdk.Does This PR Require a Contrib Repo Change?
Checklist: