|
27 | 27 | LoggingHandler, |
28 | 28 | LogRecordProcessor, |
29 | 29 | ) |
30 | | -from opentelemetry.sdk._logs._internal import LogLimits, _UnsetLogLimits |
| 30 | +from opentelemetry.sdk._logs._internal import LogLimits |
31 | 31 | from opentelemetry.sdk.environment_variables import OTEL_ATTRIBUTE_COUNT_LIMIT |
32 | 32 | from opentelemetry.semconv._incubating.attributes import code_attributes |
33 | 33 | 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( |
372 | 372 | @patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "3"}) |
373 | 373 | def test_otel_attribute_count_limit_respected_in_logging_handler(self): |
374 | 374 | """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) |
388 | 385 |
|
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)} |
391 | 388 |
|
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 | + ) |
396 | 393 |
|
397 | | - log_record = processor.get_log_record(0) |
| 394 | + log_record = processor.get_log_record(0) |
398 | 395 |
|
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 | + ) |
406 | 403 |
|
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 | + ) |
419 | 410 |
|
420 | 411 | @patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "5"}) |
421 | 412 | def test_otel_attribute_count_limit_includes_code_attributes(self): |
422 | 413 | """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) |
439 | 424 |
|
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)} |
442 | 427 |
|
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) |
445 | 430 |
|
446 | | - log_record = processor.get_log_record(0) |
| 431 | + log_record = processor.get_log_record(0) |
447 | 432 |
|
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 | + ) |
455 | 440 |
|
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 | + ) |
468 | 447 |
|
469 | 448 | def test_logging_handler_without_env_var_uses_default_limit(self): |
470 | 449 | """Test that without OTEL_ATTRIBUTE_COUNT_LIMIT, default limit (128) should apply.""" |
|
0 commit comments