Skip to content

Commit

Permalink
Handle nested exception case by stringfying value
Browse files Browse the repository at this point in the history
- Add formatting in CHANGELOG.md
  • Loading branch information
hyoinandout committed Aug 17, 2024
1 parent 7c07b8f commit 50fd30c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Make log sdk add exception.message to logRecord for exceptions whose argument
- Make log sdk add `exception.message` to logRecord for exceptions whose argument
is an exception not a string message
([#4122](https://github.com/open-telemetry/opentelemetry-python/pull/4122))
- Fix use of `link.attributes.dropped`, which may not exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,9 @@ def _get_attributes(record: logging.LogRecord) -> Attributes:
if exctype is not None:
attributes[SpanAttributes.EXCEPTION_TYPE] = exctype.__name__
if value is not None and value.args:
if type(value.args[0]) is str:
attributes[SpanAttributes.EXCEPTION_MESSAGE] = value.args[
0
]
else:
if value.args[0] is not None and value.args[0].args:
attributes[SpanAttributes.EXCEPTION_MESSAGE] = (
value.args[0].args[0]
)
attributes[SpanAttributes.EXCEPTION_MESSAGE] = str(
value.args[0]
)
if tb is not None:
# https://github.com/open-telemetry/opentelemetry-specification/blob/9fa7c656b26647b27e485a6af7e38dc716eba98a/specification/trace/semantic_conventions/exceptions.md#stacktrace-representation
attributes[SpanAttributes.EXCEPTION_STACKTRACE] = "".join(
Expand Down
6 changes: 4 additions & 2 deletions opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def test_log_record_recursive_exception(self):
processor, logger = set_up_test_logging(logging.ERROR)

try:
raise ZeroDivisionError(ZeroDivisionError("division by zero"))
raise ZeroDivisionError(
ZeroDivisionError(ZeroDivisionError("division by zero"))
)
except ZeroDivisionError:
with self.assertLogs(level=logging.ERROR):
logger.exception("Zero Division Error")
Expand All @@ -189,7 +191,7 @@ def test_log_record_recursive_exception(self):
)
self.assertEqual(
log_record.attributes[SpanAttributes.EXCEPTION_MESSAGE],
"division by zero",
"""ZeroDivisionError(ZeroDivisionError("division by zero"))""",
)
stack_trace = log_record.attributes[
SpanAttributes.EXCEPTION_STACKTRACE
Expand Down

0 comments on commit 50fd30c

Please sign in to comment.