Skip to content

Commit b26ba45

Browse files
🚑 Handle "LogRecord.exc_info" being a string
1 parent 23aad5e commit b26ba45

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import concurrent.futures
2020
import json
2121
import logging
22+
import sys
2223
import threading
2324
import traceback
2425
import warnings
@@ -568,8 +569,12 @@ def _get_attributes(record: logging.LogRecord) -> _ExtendedAttributes:
568569
attributes[code_attributes.CODE_FUNCTION_NAME] = record.funcName
569570
attributes[code_attributes.CODE_LINE_NUMBER] = record.lineno
570571

572+
if isinstance(record.exc_info, str):
573+
record.exc_info = sys.exc_info()
574+
571575
if record.exc_info:
572576
exctype, value, tb = record.exc_info
577+
573578
if exctype is not None:
574579
attributes[exception_attributes.EXCEPTION_TYPE] = (
575580
exctype.__name__

opentelemetry-sdk/tests/logs/test_handler.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import logging
1616
import os
17+
import sys
1718
import unittest
1819
from unittest.mock import Mock, patch
1920

@@ -48,6 +49,20 @@ def test_handler_default_log_level(self):
4849
logger.warning("Warning message")
4950
self.assertEqual(processor.emit_count(), 1)
5051

52+
def test_handler_error_exc_info(self):
53+
processor, logger = set_up_test_logging(logging.NOTSET)
54+
exc_info_values = [
55+
# Don't know what caused it in my context, so I'm relying on mocks to replicate the behavior.
56+
# First the `record.exc_info` becomes a string somehow, then `sys.exc_info` brings the tuple.
57+
"Stringified exception",
58+
(None, None, None),
59+
]
60+
61+
with patch.object(sys, "exc_info", side_effect=exc_info_values):
62+
logger.exception("Exception message") # Should not raise exception
63+
64+
assert processor.emit_count() == 1
65+
5166
def test_handler_custom_log_level(self):
5267
processor, logger = set_up_test_logging(logging.ERROR)
5368

0 commit comments

Comments
 (0)