Skip to content

Commit 9d16864

Browse files
committed
Add unit test
1 parent 992c0e5 commit 9d16864

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,18 @@ def _patch_basic_config():
264264
original_basic_config = logging.basicConfig
265265

266266
def patched_basic_config(*args, **kwargs):
267-
# Get the root logger
268267
root = logging.getLogger()
269-
270-
# Check if the only handler is our OTel handler
271268
has_only_otel = len(root.handlers) == 1 and isinstance(
272269
root.handlers[0], LoggingHandler
273270
)
274-
275271
if has_only_otel:
276-
# Temporarily remove OTel handler
277272
otel_handler = root.handlers[0]
278273
root.handlers = []
279-
280-
# Call original basicConfig
281274
original_basic_config(*args, **kwargs)
282-
283-
# Add OTel handler back
284275
root.addHandler(otel_handler)
285276
else:
286-
# Normal behavior
287277
original_basic_config(*args, **kwargs)
288278

289-
# Apply the monkey patch
290279
logging.basicConfig = patched_basic_config
291280

292281

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -842,30 +842,58 @@ def test_initialize_components_kwargs(
842842

843843
def test_basicConfig_works_with_otel_handler(self):
844844
with ClearLoggingHandlers():
845-
# Initialize auto-instrumentation with logging enabled
846845
_init_logging(
847846
{"otlp": DummyOTLPLogExporter},
848847
Resource.create({}),
849848
setup_logging_handler=True,
850849
)
851850

852-
# Call basicConfig - this should work despite OTel handler being present
853851
logging.basicConfig(level=logging.INFO)
854852

855-
# Verify a StreamHandler was added
856853
root_logger = logging.getLogger()
857854
stream_handlers = [
858855
h
859856
for h in root_logger.handlers
860857
if isinstance(h, logging.StreamHandler)
861-
and not isinstance(h, LoggingHandler)
862858
]
863859
self.assertEqual(
864860
len(stream_handlers),
865861
1,
866862
"basicConfig should add a StreamHandler even when OTel handler exists",
867863
)
868864

865+
def test_basicConfig_preserves_otel_handler(self):
866+
with ClearLoggingHandlers():
867+
_init_logging(
868+
{"otlp": DummyOTLPLogExporter},
869+
Resource.create({}),
870+
setup_logging_handler=True,
871+
)
872+
873+
root_logger = logging.getLogger()
874+
self.assertEqual(
875+
len(root_logger.handlers),
876+
1,
877+
"Should be exactly one OpenTelemetry LoggingHandler",
878+
)
879+
handler = root_logger.handlers[0]
880+
self.assertIsInstance(handler, LoggingHandler)
881+
882+
logging.basicConfig()
883+
884+
self.assertGreater(len(root_logger.handlers), 1)
885+
886+
logging_handlers = [
887+
h
888+
for h in root_logger.handlers
889+
if isinstance(h, LoggingHandler)
890+
]
891+
self.assertEqual(
892+
len(logging_handlers),
893+
1,
894+
"Should still have exactly one OpenTelemetry LoggingHandler",
895+
)
896+
869897

870898
class TestMetricsInit(TestCase):
871899
def setUp(self):
@@ -1109,43 +1137,32 @@ def __init__(self):
11091137
self.original_handlers = None
11101138

11111139
def __enter__(self):
1112-
# Save original state
11131140
self.original_handlers = self.root_logger.handlers[:]
1114-
# Remove all handlers
11151141
self.root_logger.handlers = []
11161142
return self
11171143

11181144
def __exit__(self, exc_type, exc_val, exc_tb):
1119-
# Restore original state
11201145
self.root_logger.handlers = []
11211146
for handler in self.original_handlers:
11221147
self.root_logger.addHandler(handler)
11231148

11241149

11251150
class TestClearLoggingHandlers(TestCase):
11261151
def test_preserves_handlers(self):
1127-
root_logger = getLogger() # Get the root logger
1128-
initial_handlers = root_logger.handlers[
1129-
:
1130-
] # Save initial test environment handlers
1152+
root_logger = getLogger()
1153+
initial_handlers = root_logger.handlers[:]
11311154

1132-
# Add our test handler
11331155
test_handler = logging.StreamHandler()
11341156
root_logger.addHandler(test_handler)
11351157
expected_handlers = initial_handlers + [test_handler]
11361158

11371159
with ClearLoggingHandlers():
1138-
# Should have no handlers during the test
11391160
self.assertEqual(len(root_logger.handlers), 0)
1140-
1141-
# Add a temporary handler that should get cleaned up
11421161
temp_handler = logging.StreamHandler()
11431162
root_logger.addHandler(temp_handler)
11441163

1145-
# After the test, should be back to initial handlers plus our test handler
11461164
self.assertEqual(len(root_logger.handlers), len(expected_handlers))
11471165
for h1, h2 in zip(root_logger.handlers, expected_handlers):
11481166
self.assertIs(h1, h2)
11491167

1150-
# Cleanup our test handler
11511168
root_logger.removeHandler(test_handler)

0 commit comments

Comments
 (0)