Skip to content

Commit 9c95104

Browse files
authored
Make log sdk add exception.message to logRecord for exceptions whose argument is an exception not a string message (open-telemetry#4122)
1 parent b2b133d commit 9c95104

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Make log sdk add `exception.message` to logRecord for exceptions whose argument
11+
is an exception not a string message
12+
([#4122](https://github.com/open-telemetry/opentelemetry-python/pull/4122))
1013
- Fix use of `link.attributes.dropped`, which may not exist
1114
([#4119](https://github.com/open-telemetry/opentelemetry-python/pull/4119))
1215
- Running mypy on SDK resources

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,9 @@ def _get_attributes(record: logging.LogRecord) -> Attributes:
491491
if exctype is not None:
492492
attributes[SpanAttributes.EXCEPTION_TYPE] = exctype.__name__
493493
if value is not None and value.args:
494-
attributes[SpanAttributes.EXCEPTION_MESSAGE] = value.args[0]
494+
attributes[SpanAttributes.EXCEPTION_MESSAGE] = str(
495+
value.args[0]
496+
)
495497
if tb is not None:
496498
# https://github.com/open-telemetry/opentelemetry-specification/blob/9fa7c656b26647b27e485a6af7e38dc716eba98a/specification/trace/semantic_conventions/exceptions.md#stacktrace-representation
497499
attributes[SpanAttributes.EXCEPTION_STACKTRACE] = "".join(

opentelemetry-sdk/tests/logs/test_handler.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,41 @@ def test_log_record_exception(self):
169169
self.assertTrue("division by zero" in stack_trace)
170170
self.assertTrue(__file__ in stack_trace)
171171

172+
def test_log_record_recursive_exception(self):
173+
"""Exception information will be included in attributes even though it is recursive"""
174+
processor, logger = set_up_test_logging(logging.ERROR)
175+
176+
try:
177+
raise ZeroDivisionError(
178+
ZeroDivisionError(ZeroDivisionError("division by zero"))
179+
)
180+
except ZeroDivisionError:
181+
with self.assertLogs(level=logging.ERROR):
182+
logger.exception("Zero Division Error")
183+
184+
log_record = processor.get_log_record(0)
185+
186+
self.assertIsNotNone(log_record)
187+
self.assertEqual(log_record.body, "Zero Division Error")
188+
self.assertEqual(
189+
log_record.attributes[SpanAttributes.EXCEPTION_TYPE],
190+
ZeroDivisionError.__name__,
191+
)
192+
self.assertEqual(
193+
log_record.attributes[SpanAttributes.EXCEPTION_MESSAGE],
194+
"division by zero",
195+
)
196+
stack_trace = log_record.attributes[
197+
SpanAttributes.EXCEPTION_STACKTRACE
198+
]
199+
self.assertIsInstance(stack_trace, str)
200+
self.assertTrue("Traceback" in stack_trace)
201+
self.assertTrue("ZeroDivisionError" in stack_trace)
202+
self.assertTrue("division by zero" in stack_trace)
203+
self.assertTrue(__file__ in stack_trace)
204+
172205
def test_log_exc_info_false(self):
173-
"""Exception information will be included in attributes"""
206+
"""Exception information will not be included in attributes"""
174207
processor, logger = set_up_test_logging(logging.NOTSET)
175208

176209
try:

0 commit comments

Comments
 (0)