fix(logs): optimize LogRecord memory by removing redundant context#4977
fix(logs): optimize LogRecord memory by removing redundant context#4977ajuijas wants to merge 6 commits intoopen-telemetry:mainfrom
Conversation
45c7255 to
c77ecd0
Compare
|
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. |
opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py
Outdated
Show resolved
Hide resolved
opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py
Show resolved
Hide resolved
opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py
Show resolved
Hide resolved
opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
This PR aims to optimize memory usage in the OpenTelemetry Python SDK's logging system by clearing the Context reference in LogRecords before they are buffered in the BatchLogRecordProcessor. The motivation is to prevent memory inflation in high-throughput scenarios where logs contain large context objects with baggage. The PR preserves trace correlation IDs (TraceId, SpanId, TraceFlags) which are extracted and stored separately on the LogRecord before context clearing.
Changes:
- Added
import copyandContextimport to the SDK export module - Modified BatchLogRecordProcessor.on_emit to shallow-copy the LogRecord and clear its context before buffering
- Modified SimpleLogRecordProcessor.on_emit with identical context-clearing logic
- Added CHANGELOG entry documenting the optimization
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/init.py | Added context clearing to both SimpleLogRecordProcessor and BatchLogRecordProcessor via shallow copy |
| CHANGELOG.md | Added entry documenting the memory optimization |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py
Outdated
Show resolved
Hide resolved
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
…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.
…/__init__.py Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
- Add missing Context import. - Apply memory optimization to SimpleLogRecordProcessor. - Update BatchLogRecordProcessor to use Context() instead of None.
cded615 to
6c65913
Compare
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: