Skip to content

Commit 6c65913

Browse files
committed
logs: address memory optimization review comments
- Add missing Context import. - Apply memory optimization to SimpleLogRecordProcessor. - Update BatchLogRecordProcessor to use Context() instead of None.
1 parent 22a4a5a commit 6c65913

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from opentelemetry.context import (
2727
_ON_EMIT_RECURSION_COUNT_KEY,
2828
_SUPPRESS_INSTRUMENTATION_KEY,
29+
Context,
2930
attach,
3031
detach,
3132
get_value,
@@ -224,8 +225,13 @@ def on_emit(self, log_record: ReadWriteLogRecord):
224225
if log_record.resource is not None
225226
else Resource.create({})
226227
)
228+
# Shallow copy the API log record to break the reference to the potentially large context
229+
# while keeping the original context intact for other processors.
230+
api_log_record = copy.copy(log_record.log_record)
231+
api_log_record.context = Context()
232+
227233
readable_log_record = ReadableLogRecord(
228-
log_record=log_record.log_record,
234+
log_record=api_log_record,
229235
resource=resource,
230236
instrumentation_scope=log_record.instrumentation_scope,
231237
limits=log_record.limits,

0 commit comments

Comments
 (0)