Skip to content

logs: remove context from LogRecord and pass to on_emit#5005

Draft
ajuijas wants to merge 1 commit intoopen-telemetry:mainfrom
ajuijas:log-record-context-removal
Draft

logs: remove context from LogRecord and pass to on_emit#5005
ajuijas wants to merge 1 commit intoopen-telemetry:mainfrom
ajuijas:log-record-context-removal

Conversation

@ajuijas
Copy link

@ajuijas ajuijas commented Mar 21, 2026

Pull Request Content

  • Remove context attribute from LogRecord to prevent memory inflation.
  • Update LogRecordProcessor.on_emit signature to accept context separately.
  • Update Logger.emit 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.

Description

This PR implements the architectural changes discussed in #4977 to address memory usage issues in LogRecord. Storing the full Context on each LogRecord caused significant memory overhead because the Context could 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

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

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.

  • Automated Tests: Ran pytest opentelemetry-sdk/tests/logs/
  • Result: 78 passed, 39 warnings, 2 subtests passed in 2.56s

Does This PR Require a Contrib Repo Change?

  • Yes

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated (Waiting for release planning)
  • Unit tests have been added/updated
  • Documentation has been updated

- 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.
if observed_timestamp is None:
observed_timestamp = time_ns()
self.observed_timestamp = observed_timestamp
self.context = context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing the full context is fine, but we might want to keep at least SpanContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

why does LogRecord store the entire context it was called with

2 participants