Skip to content

Commit 9e64cac

Browse files
committed
Add unit test
1 parent 0abb2fc commit 9e64cac

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

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

-11
Original file line numberDiff line numberDiff line change
@@ -266,29 +266,18 @@ def _patch_basic_config():
266266
original_basic_config = logging.basicConfig
267267

268268
def patched_basic_config(*args, **kwargs):
269-
# Get the root logger
270269
root = logging.getLogger()
271-
272-
# Check if the only handler is our OTel handler
273270
has_only_otel = len(root.handlers) == 1 and isinstance(
274271
root.handlers[0], LoggingHandler
275272
)
276-
277273
if has_only_otel:
278-
# Temporarily remove OTel handler
279274
otel_handler = root.handlers[0]
280275
root.handlers = []
281-
282-
# Call original basicConfig
283276
original_basic_config(*args, **kwargs)
284-
285-
# Add OTel handler back
286277
root.addHandler(otel_handler)
287278
else:
288-
# Normal behavior
289279
original_basic_config(*args, **kwargs)
290280

291-
# Apply the monkey patch
292281
logging.basicConfig = patched_basic_config
293282

294283

opentelemetry-sdk/tests/test_configurator.py

+34-17
Original file line numberDiff line numberDiff line change
@@ -845,30 +845,58 @@ def test_initialize_components_kwargs(
845845

846846
def test_basicConfig_works_with_otel_handler(self):
847847
with ClearLoggingHandlers():
848-
# Initialize auto-instrumentation with logging enabled
849848
_init_logging(
850849
{"otlp": DummyOTLPLogExporter},
851850
Resource.create({}),
852851
setup_logging_handler=True,
853852
)
854853

855-
# Call basicConfig - this should work despite OTel handler being present
856854
logging.basicConfig(level=logging.INFO)
857855

858-
# Verify a StreamHandler was added
859856
root_logger = logging.getLogger()
860857
stream_handlers = [
861858
h
862859
for h in root_logger.handlers
863860
if isinstance(h, logging.StreamHandler)
864-
and not isinstance(h, LoggingHandler)
865861
]
866862
self.assertEqual(
867863
len(stream_handlers),
868864
1,
869865
"basicConfig should add a StreamHandler even when OTel handler exists",
870866
)
871867

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

873901
class TestMetricsInit(TestCase):
874902
def setUp(self):
@@ -1112,43 +1140,32 @@ def __init__(self):
11121140
self.original_handlers = None
11131141

11141142
def __enter__(self):
1115-
# Save original state
11161143
self.original_handlers = self.root_logger.handlers[:]
1117-
# Remove all handlers
11181144
self.root_logger.handlers = []
11191145
return self
11201146

11211147
def __exit__(self, exc_type, exc_val, exc_tb):
1122-
# Restore original state
11231148
self.root_logger.handlers = []
11241149
for handler in self.original_handlers:
11251150
self.root_logger.addHandler(handler)
11261151

11271152

11281153
class TestClearLoggingHandlers(TestCase):
11291154
def test_preserves_handlers(self):
1130-
root_logger = getLogger() # Get the root logger
1131-
initial_handlers = root_logger.handlers[
1132-
:
1133-
] # Save initial test environment handlers
1155+
root_logger = getLogger()
1156+
initial_handlers = root_logger.handlers[:]
11341157

1135-
# Add our test handler
11361158
test_handler = logging.StreamHandler()
11371159
root_logger.addHandler(test_handler)
11381160
expected_handlers = initial_handlers + [test_handler]
11391161

11401162
with ClearLoggingHandlers():
1141-
# Should have no handlers during the test
11421163
self.assertEqual(len(root_logger.handlers), 0)
1143-
1144-
# Add a temporary handler that should get cleaned up
11451164
temp_handler = logging.StreamHandler()
11461165
root_logger.addHandler(temp_handler)
11471166

1148-
# After the test, should be back to initial handlers plus our test handler
11491167
self.assertEqual(len(root_logger.handlers), len(expected_handlers))
11501168
for h1, h2 in zip(root_logger.handlers, expected_handlers):
11511169
self.assertIs(h1, h2)
11521170

1153-
# Cleanup our test handler
11541171
root_logger.removeHandler(test_handler)

0 commit comments

Comments
 (0)