Skip to content

Commit 78d4e7d

Browse files
author
thanhnh user
committed
Add loglimit to log provider
1 parent 5a11ecb commit 78d4e7d

File tree

2 files changed

+4
-30
lines changed

2 files changed

+4
-30
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _from_env_if_absent(
157157
cls, value: int | None, env_var: str, default: int | None = None
158158
) -> int | None:
159159
if value == cls.UNSET:
160-
return None
160+
value = None # continue to read the limit from env
161161

162162
err_msg = "{} must be a non-negative integer but got {}"
163163

@@ -556,7 +556,6 @@ def __init__(
556556
) -> None:
557557
super().__init__(level=level)
558558
self._logger_provider = logger_provider or get_logger_provider()
559-
self._log_limits = LogLimits()
560559

561560
@staticmethod
562561
def _get_attributes(record: logging.LogRecord) -> _ExtendedAttributes:
@@ -630,7 +629,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:
630629
body=body,
631630
resource=logger.resource,
632631
attributes=attributes,
633-
limits=self._log_limits,
632+
limits=self._logger_provider._log_limits,
634633
)
635634

636635
def emit(self, record: logging.LogRecord) -> None:
@@ -693,6 +692,7 @@ def __init__(
693692
multi_log_record_processor: SynchronousMultiLogRecordProcessor
694693
| ConcurrentMultiLogRecordProcessor
695694
| None = None,
695+
log_limits: LogLimits | None = _UnsetLogLimits,
696696
):
697697
if resource is None:
698698
self._resource = Resource.create({})
@@ -708,6 +708,7 @@ def __init__(
708708
self._at_exit_handler = atexit.register(self.shutdown)
709709
self._logger_cache = {}
710710
self._logger_cache_lock = Lock()
711+
self._log_limits = log_limits
711712

712713
@property
713714
def resource(self):

opentelemetry-sdk/tests/logs/test_handler.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -426,33 +426,6 @@ def test_otel_attribute_count_limit_includes_code_attributes(self):
426426
f"Should have 6 dropped attributes, got {log_record.dropped_attributes}",
427427
)
428428

429-
@patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "0"})
430-
def test_otel_attribute_count_limit_zero_prevents_all_attributes(self):
431-
"""Test that OTEL_ATTRIBUTE_COUNT_LIMIT=0 prevents all attributes."""
432-
processor, logger = set_up_test_logging(logging.WARNING)
433-
434-
# Create a log record with extra attributes
435-
extra_attrs = {"user_attr": "value", "another_attr": "another_value"}
436-
437-
with self.assertLogs(level=logging.WARNING):
438-
logger.warning("Test message", extra=extra_attrs)
439-
440-
log_record = processor.get_log_record(0)
441-
442-
# With OTEL_ATTRIBUTE_COUNT_LIMIT=0, should have 0 attributes
443-
self.assertEqual(
444-
len(log_record.attributes),
445-
0,
446-
"With OTEL_ATTRIBUTE_COUNT_LIMIT=0, should have no attributes",
447-
)
448-
449-
# Should have 5 dropped attributes (2 user + 3 code = 5 dropped)
450-
self.assertEqual(
451-
log_record.dropped_attributes,
452-
5,
453-
f"Should have 5 dropped attributes, got {log_record.dropped_attributes}",
454-
)
455-
456429
def test_logging_handler_without_env_var_uses_default_limit(self):
457430
"""Test that without OTEL_ATTRIBUTE_COUNT_LIMIT, default limit (128) should apply."""
458431
processor, logger = set_up_test_logging(logging.WARNING)

0 commit comments

Comments
 (0)