Skip to content

Commit c24302a

Browse files
committed
Add unit test
1 parent 992c0e5 commit c24302a

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
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: 32 additions & 4 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):

0 commit comments

Comments
 (0)