Skip to content

Commit 0ac986f

Browse files
committed
Add Test from log provider
1 parent 835f632 commit 0ac986f

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

opentelemetry-sdk/tests/logs/test_handler.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
LogData,
2626
LoggerProvider,
2727
LoggingHandler,
28+
LogLimits,
2829
LogRecordProcessor,
2930
)
3031
from opentelemetry.sdk.environment_variables import OTEL_ATTRIBUTE_COUNT_LIMIT
@@ -487,6 +488,91 @@ def test_otel_attribute_count_limit_with_exception_attributes(self):
487488
f"Should have 5 dropped attributes, got {log_record.dropped_attributes}",
488489
)
489490

491+
def test_custom_log_limits_from_logger_provider(self):
492+
"""Test that LoggingHandler uses custom LogLimits from LoggerProvider."""
493+
# Create a LoggerProvider with custom LogLimits (max_attributes=4)
494+
logger_provider = LoggerProvider(
495+
log_limits=LogLimits(max_attributes=4)
496+
)
497+
498+
# Set up logging with this provider
499+
processor = FakeProcessor()
500+
logger_provider.add_log_record_processor(processor)
501+
logger = logging.getLogger("custom_limits_test")
502+
handler = LoggingHandler(
503+
level=logging.WARNING, logger_provider=logger_provider
504+
)
505+
logger.addHandler(handler)
506+
507+
# Create a log record with many extra attributes
508+
extra_attrs = {f"custom_attr_{i}": f"value_{i}" for i in range(10)}
509+
510+
with self.assertLogs(level=logging.WARNING):
511+
logger.warning(
512+
"Test message with custom limits", extra=extra_attrs
513+
)
514+
515+
log_record = processor.get_log_record(0)
516+
517+
# With custom max_attributes=4, should have exactly 4 attributes
518+
total_attrs = len(log_record.attributes)
519+
self.assertEqual(
520+
total_attrs,
521+
4,
522+
f"Should have exactly 4 attributes due to custom limit, got {total_attrs}",
523+
)
524+
525+
# Should have 9 dropped attributes (10 custom + 3 code - 4 kept = 9 dropped)
526+
self.assertEqual(
527+
log_record.dropped_attributes,
528+
9,
529+
f"Should have 9 dropped attributes, got {log_record.dropped_attributes}",
530+
)
531+
532+
@patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "10"})
533+
def test_custom_log_limits_override_env_vars(self):
534+
"""Test that custom LogLimits from LoggerProvider override environment variables."""
535+
# Create a LoggerProvider with custom LogLimits (max_attributes=3)
536+
# This should override the OTEL_ATTRIBUTE_COUNT_LIMIT=10 from the environment
537+
logger_provider = LoggerProvider(
538+
log_limits=LogLimits(max_attributes=3)
539+
)
540+
541+
# Set up logging with this provider
542+
processor = FakeProcessor()
543+
logger_provider.add_log_record_processor(processor)
544+
logger = logging.getLogger("override_env_test")
545+
handler = LoggingHandler(
546+
level=logging.WARNING, logger_provider=logger_provider
547+
)
548+
logger.addHandler(handler)
549+
550+
# Create a log record with many extra attributes
551+
extra_attrs = {f"custom_attr_{i}": f"value_{i}" for i in range(8)}
552+
553+
with self.assertLogs(level=logging.WARNING):
554+
logger.warning(
555+
"Test message with custom limits", extra=extra_attrs
556+
)
557+
558+
log_record = processor.get_log_record(0)
559+
560+
# With custom max_attributes=3, should have exactly 3 attributes
561+
# (even though OTEL_ATTRIBUTE_COUNT_LIMIT=10)
562+
total_attrs = len(log_record.attributes)
563+
self.assertEqual(
564+
total_attrs,
565+
3,
566+
f"Should have exactly 3 attributes due to custom limit, got {total_attrs}",
567+
)
568+
569+
# Should have 8 dropped attributes (8 custom + 3 code - 3 kept = 8 dropped)
570+
self.assertEqual(
571+
log_record.dropped_attributes,
572+
8,
573+
f"Should have 8 dropped attributes, got {log_record.dropped_attributes}",
574+
)
575+
490576

491577
def set_up_test_logging(level, formatter=None, root_logger=False):
492578
logger_provider = LoggerProvider()

0 commit comments

Comments
 (0)