fix(logs): optimize LogRecord memory by removing redundant context#4977
Open
ajuijas wants to merge 2 commits intoopen-telemetry:mainfrom
Open
fix(logs): optimize LogRecord memory by removing redundant context#4977ajuijas wants to merge 2 commits intoopen-telemetry:mainfrom
ajuijas wants to merge 2 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
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.
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 #4957
Description
This PR optimizes LogRecord memory usage by removing the redundant storage of the Context object in the API.
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.
Changes:
self.context = contextfromLogRecord.__init__inopentelemetry-api.trace_id,span_id,trace_flags) are still extracted from the context during initialization, ensuring that trace correlation remains fully functional..contextattribute.Fixes #4957
Type of change
Note
While this removes an attribute from LogRecord, the Logging SDK is currently marked as experimental/unstable. Addressing this architectural memory issue now prevents a major performance hurdle for future stable releases.
How Has This Been Tested?
I performed local benchmarking and ran the full unit test suite to ensure correctness and quantify the memory savings.
opentelemetry-apiandopentelemetry-sdk.TraceIdis still correctly assigned to LogRecord even without the explicit context attribute storage.Does This PR Require a Contrib Repo Change?
Checklist:
.contexton LogRecord)