Skip to content

Commit a0adda0

Browse files
committed
Remove UNSET value, only use None as Default value
1 parent dd539f5 commit a0adda0

File tree

2 files changed

+69
-97
lines changed

2 files changed

+69
-97
lines changed

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class LogLimits:
104104
This class does not enforce any limits itself. It only provides a way to read limits from env,
105105
default values and from user provided arguments.
106106
107-
All limit arguments must be either a non-negative integer, ``None`` or ``LogLimits.UNSET``.
107+
All limit arguments must be either a non-negative integer or ``None``.
108108
109109
- All limit arguments are optional.
110110
- If a limit argument is not set, the class will try to read its value from the corresponding
@@ -126,8 +126,6 @@ class LogLimits:
126126
the specified length will be truncated.
127127
"""
128128

129-
UNSET = -1
130-
131129
def __init__(
132130
self,
133131
max_attributes: int | None = None,
@@ -156,9 +154,6 @@ def __repr__(self):
156154
def _from_env_if_absent(
157155
cls, value: int | None, env_var: str, default: int | None = None
158156
) -> int | None:
159-
if value == cls.UNSET:
160-
value = None # continue to read the limit from env
161-
162157
err_msg = "{} must be a non-negative integer but got {}"
163158

164159
# if no value is provided for the limit, try to load it from env
@@ -181,12 +176,6 @@ def _from_env_if_absent(
181176
return value
182177

183178

184-
_UnsetLogLimits = LogLimits(
185-
max_attributes=LogLimits.UNSET,
186-
max_attribute_length=LogLimits.UNSET,
187-
)
188-
189-
190179
class LogRecord(APILogRecord):
191180
"""A LogRecord instance represents an event being logged.
192181
@@ -206,7 +195,7 @@ def __init__(
206195
body: AnyValue | None = None,
207196
resource: Resource | None = None,
208197
attributes: _ExtendedAttributes | None = None,
209-
limits: LogLimits | None = _UnsetLogLimits,
198+
limits: LogLimits | None = None,
210199
event_name: str | None = None,
211200
): ...
212201

@@ -226,7 +215,7 @@ def __init__(
226215
body: AnyValue | None = None,
227216
resource: Resource | None = None,
228217
attributes: _ExtendedAttributes | None = None,
229-
limits: LogLimits | None = _UnsetLogLimits,
218+
limits: LogLimits | None = None,
230219
): ...
231220

232221
def __init__( # pylint:disable=too-many-locals
@@ -242,7 +231,7 @@ def __init__( # pylint:disable=too-many-locals
242231
body: AnyValue | None = None,
243232
resource: Resource | None = None,
244233
attributes: _ExtendedAttributes | None = None,
245-
limits: LogLimits | None = _UnsetLogLimits,
234+
limits: LogLimits | None = None,
246235
event_name: str | None = None,
247236
):
248237
if trace_id or span_id or trace_flags:
@@ -258,6 +247,10 @@ def __init__( # pylint:disable=too-many-locals
258247
span = get_current_span(context)
259248
span_context = span.get_span_context()
260249

250+
# Use default LogLimits if none provided
251+
if limits is None:
252+
limits = LogLimits()
253+
261254
super().__init__(
262255
**{
263256
"timestamp": timestamp,
@@ -692,7 +685,7 @@ def __init__(
692685
multi_log_record_processor: SynchronousMultiLogRecordProcessor
693686
| ConcurrentMultiLogRecordProcessor
694687
| None = None,
695-
log_limits: LogLimits | None = _UnsetLogLimits,
688+
log_limits: LogLimits | None = None,
696689
):
697690
if resource is None:
698691
self._resource = Resource.create({})
@@ -708,7 +701,7 @@ def __init__(
708701
self._at_exit_handler = atexit.register(self.shutdown)
709702
self._logger_cache = {}
710703
self._logger_cache_lock = Lock()
711-
self._log_limits = log_limits
704+
self._log_limits = log_limits or LogLimits()
712705

713706
@property
714707
def resource(self):

opentelemetry-sdk/tests/logs/test_handler.py

Lines changed: 59 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
LoggingHandler,
2828
LogRecordProcessor,
2929
)
30-
from opentelemetry.sdk._logs._internal import LogLimits, _UnsetLogLimits
30+
from opentelemetry.sdk._logs._internal import LogLimits
3131
from opentelemetry.sdk.environment_variables import OTEL_ATTRIBUTE_COUNT_LIMIT
3232
from opentelemetry.semconv._incubating.attributes import code_attributes
3333
from opentelemetry.semconv.attributes import exception_attributes
@@ -372,99 +372,78 @@ def test_handler_root_logger_with_disabled_sdk_does_not_go_into_recursion_error(
372372
@patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "3"})
373373
def test_otel_attribute_count_limit_respected_in_logging_handler(self):
374374
"""Test that OTEL_ATTRIBUTE_COUNT_LIMIT is properly respected by LoggingHandler."""
375-
# Store original values to restore later
376-
original_max_attributes = _UnsetLogLimits.max_attributes
377-
original_max_attribute_length = _UnsetLogLimits.max_attribute_length
378-
379-
try:
380-
# Force _UnsetLogLimits to re-read the environment variable
381-
_UnsetLogLimits.max_attributes = (
382-
_UnsetLogLimits._from_env_if_absent(
383-
LogLimits.UNSET, OTEL_ATTRIBUTE_COUNT_LIMIT
384-
)
385-
)
386-
387-
processor, logger = set_up_test_logging(logging.WARNING)
375+
# Create a new LoggerProvider within the patched environment
376+
# This will create LogLimits() that reads from the environment variable
377+
logger_provider = LoggerProvider()
378+
processor = FakeProcessor()
379+
logger_provider.add_log_record_processor(processor)
380+
logger = logging.getLogger("env_test")
381+
handler = LoggingHandler(
382+
level=logging.WARNING, logger_provider=logger_provider
383+
)
384+
logger.addHandler(handler)
388385

389-
# Create a log record with many extra attributes
390-
extra_attrs = {f"custom_attr_{i}": f"value_{i}" for i in range(10)}
386+
# Create a log record with many extra attributes
387+
extra_attrs = {f"custom_attr_{i}": f"value_{i}" for i in range(10)}
391388

392-
with self.assertLogs(level=logging.WARNING):
393-
logger.warning(
394-
"Test message with many attributes", extra=extra_attrs
395-
)
389+
with self.assertLogs(level=logging.WARNING):
390+
logger.warning(
391+
"Test message with many attributes", extra=extra_attrs
392+
)
396393

397-
log_record = processor.get_log_record(0)
394+
log_record = processor.get_log_record(0)
398395

399-
# With OTEL_ATTRIBUTE_COUNT_LIMIT=3, should have exactly 3 attributes
400-
total_attrs = len(log_record.attributes)
401-
self.assertEqual(
402-
total_attrs,
403-
3,
404-
f"Should have exactly 3 attributes due to limit, got {total_attrs}",
405-
)
396+
# With OTEL_ATTRIBUTE_COUNT_LIMIT=3, should have exactly 3 attributes
397+
total_attrs = len(log_record.attributes)
398+
self.assertEqual(
399+
total_attrs,
400+
3,
401+
f"Should have exactly 3 attributes due to limit, got {total_attrs}",
402+
)
406403

407-
# Should have 10 dropped attributes (10 custom + 3 code - 3 kept = 10 dropped)
408-
self.assertEqual(
409-
log_record.dropped_attributes,
410-
10,
411-
f"Should have 10 dropped attributes, got {log_record.dropped_attributes}",
412-
)
413-
finally:
414-
# Restore original values
415-
_UnsetLogLimits.max_attributes = original_max_attributes
416-
_UnsetLogLimits.max_attribute_length = (
417-
original_max_attribute_length
418-
)
404+
# Should have 10 dropped attributes (10 custom + 3 code - 3 kept = 10 dropped)
405+
self.assertEqual(
406+
log_record.dropped_attributes,
407+
10,
408+
f"Should have 10 dropped attributes, got {log_record.dropped_attributes}",
409+
)
419410

420411
@patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "5"})
421412
def test_otel_attribute_count_limit_includes_code_attributes(self):
422413
"""Test that OTEL_ATTRIBUTE_COUNT_LIMIT applies to all attributes including code attributes."""
423-
# Import _UnsetLogLimits directly
424-
425-
# Store original values to restore later
426-
original_max_attributes = _UnsetLogLimits.max_attributes
427-
original_max_attribute_length = _UnsetLogLimits.max_attribute_length
428-
429-
try:
430-
# Force _UnsetLogLimits to re-read the environment variable
431-
_UnsetLogLimits.max_attributes = (
432-
_UnsetLogLimits._from_env_if_absent(
433-
LogLimits.UNSET, OTEL_ATTRIBUTE_COUNT_LIMIT
434-
)
435-
)
436-
437-
# Now proceed with the test
438-
processor, logger = set_up_test_logging(logging.WARNING)
414+
# Create a new LoggerProvider within the patched environment
415+
# This will create LogLimits() that reads from the environment variable
416+
logger_provider = LoggerProvider()
417+
processor = FakeProcessor()
418+
logger_provider.add_log_record_processor(processor)
419+
logger = logging.getLogger("env_test_2")
420+
handler = LoggingHandler(
421+
level=logging.WARNING, logger_provider=logger_provider
422+
)
423+
logger.addHandler(handler)
439424

440-
# Create a log record with some extra attributes
441-
extra_attrs = {f"user_attr_{i}": f"value_{i}" for i in range(8)}
425+
# Create a log record with some extra attributes
426+
extra_attrs = {f"user_attr_{i}": f"value_{i}" for i in range(8)}
442427

443-
with self.assertLogs(level=logging.WARNING):
444-
logger.warning("Test message", extra=extra_attrs)
428+
with self.assertLogs(level=logging.WARNING):
429+
logger.warning("Test message", extra=extra_attrs)
445430

446-
log_record = processor.get_log_record(0)
431+
log_record = processor.get_log_record(0)
447432

448-
# With OTEL_ATTRIBUTE_COUNT_LIMIT=5, should have exactly 5 attributes
449-
total_attrs = len(log_record.attributes)
450-
self.assertEqual(
451-
total_attrs,
452-
5,
453-
f"Should have exactly 5 attributes due to limit, got {total_attrs}",
454-
)
433+
# With OTEL_ATTRIBUTE_COUNT_LIMIT=5, should have exactly 5 attributes
434+
total_attrs = len(log_record.attributes)
435+
self.assertEqual(
436+
total_attrs,
437+
5,
438+
f"Should have exactly 5 attributes due to limit, got {total_attrs}",
439+
)
455440

456-
# Should have 6 dropped attributes (8 user + 3 code - 5 kept = 6 dropped)
457-
self.assertEqual(
458-
log_record.dropped_attributes,
459-
6,
460-
f"Should have 6 dropped attributes, got {log_record.dropped_attributes}",
461-
)
462-
finally:
463-
# Restore original values
464-
_UnsetLogLimits.max_attributes = original_max_attributes
465-
_UnsetLogLimits.max_attribute_length = (
466-
original_max_attribute_length
467-
)
441+
# Should have 6 dropped attributes (8 user + 3 code - 5 kept = 6 dropped)
442+
self.assertEqual(
443+
log_record.dropped_attributes,
444+
6,
445+
f"Should have 6 dropped attributes, got {log_record.dropped_attributes}",
446+
)
468447

469448
def test_logging_handler_without_env_var_uses_default_limit(self):
470449
"""Test that without OTEL_ATTRIBUTE_COUNT_LIMIT, default limit (128) should apply."""

0 commit comments

Comments
 (0)