diff --git a/CHANGELOG.md b/CHANGELOG.md index 594b7c12b05..93861d2b9dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index cecaaeba4b4..56d78ce653a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -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( diff --git a/opentelemetry-sdk/tests/logs/test_handler.py b/opentelemetry-sdk/tests/logs/test_handler.py index c276031ca07..bcd75914568 100644 --- a/opentelemetry-sdk/tests/logs/test_handler.py +++ b/opentelemetry-sdk/tests/logs/test_handler.py @@ -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") @@ -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