logs: remove context from LogRecord and pass to on_emit#5005
Draft
ajuijas wants to merge 1 commit intoopen-telemetry:mainfrom
Draft
logs: remove context from LogRecord and pass to on_emit#5005ajuijas wants to merge 1 commit intoopen-telemetry:mainfrom
ajuijas wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
- Remove context attribute from LogRecord to prevent memory inflation. - Update signature to accept context separately. - Update and SDK processors to align with the new signature. - Update tests to reflect the breaking changes. This aligns the Python SDK with the OpenTelemetry specification and resolves memory usage concerns in BatchLogRecordProcessor.
herin049
reviewed
Mar 22, 2026
| if observed_timestamp is None: | ||
| observed_timestamp = time_ns() | ||
| self.observed_timestamp = observed_timestamp | ||
| self.context = context |
Contributor
There was a problem hiding this comment.
I think removing the full context is fine, but we might want to keep at least SpanContext.
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.
Pull Request Content
LogRecordProcessor.on_emitsignature to accept context separately.Logger.emitand SDK processors to align with the new signature.This aligns the Python SDK with the OpenTelemetry specification and resolves memory usage concerns in BatchLogRecordProcessor.
Description
This PR implements the architectural changes discussed in #4977 to address memory usage issues in LogRecord. Storing the full
Contexton each LogRecord caused significant memory overhead because theContextcould not be GC'd until the log was exported.By removing context from LogRecord and passing it as a separate argument to
LogRecordProcessor.on_emit, we resolve the memory inflation while maintaining consistency with other OpenTelemetry SDKs (Go, JS) and the specification.Fixes #4957
Type of change
How Has This Been Tested?
I have updated the SDK's log test suite to reflect the new signatures and verified that all tests pass.
pytest opentelemetry-sdk/tests/logs/78 passed, 39 warnings, 2 subtests passed in 2.56sDoes This PR Require a Contrib Repo Change?
Checklist: