Skip to content

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

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

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
Copy Markdown

@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
Copy Markdown
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi Lukas. Looks like trace_id, span_id, and trace_flags are extracted from the SpanContext and set as fields below, on lines 122-124. They can be optionally overridden, but I'm not seeing any examples of this in practice. Does this address your concern?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was thinking we might want to keep SpanContext to still provide access to the trace state and flags which could potentially be useful, but we can probably add these back later if there is a valid use case.

@ajuijas ajuijas marked this pull request as ready for review March 28, 2026 19:26
@ajuijas ajuijas requested a review from a team as a code owner March 28, 2026 19:26
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

3 participants