diff --git a/CHANGELOG.md b/CHANGELOG.md index 55197c957c5..93861d2b9dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ 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 + 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 ([#4119](https://github.com/open-telemetry/opentelemetry-python/pull/4119)) - Running mypy on SDK resources diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1a7e58cf1f9..8cadc3e34f3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,7 +12,7 @@ and [**Maintainer**](https://github.com/open-telemetry/community/blob/main/commu Before you can contribute, you will need to sign the [Contributor License Agreement](https://docs.linuxfoundation.org/lfx/easycla/contributors). -Please also read the [OpenTelemetry Contributor Guide](https://github.com/open-telemetry/community/blob/main/CONTRIBUTING.md). +Please also read the [OpenTelemetry Contributor Guide](https://github.com/open-telemetry/community/blob/main/guides/contributor/README.md). # Find your right repo diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index 89432d5b339..56d78ce653a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -491,7 +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: - attributes[SpanAttributes.EXCEPTION_MESSAGE] = value.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 146e4b95b08..5b0175d29bb 100644 --- a/opentelemetry-sdk/tests/logs/test_handler.py +++ b/opentelemetry-sdk/tests/logs/test_handler.py @@ -169,8 +169,41 @@ def test_log_record_exception(self): self.assertTrue("division by zero" in stack_trace) self.assertTrue(__file__ in stack_trace) + def test_log_record_recursive_exception(self): + """Exception information will be included in attributes even though it is recursive""" + processor, logger = set_up_test_logging(logging.ERROR) + + try: + raise ZeroDivisionError( + ZeroDivisionError(ZeroDivisionError("division by zero")) + ) + except ZeroDivisionError: + with self.assertLogs(level=logging.ERROR): + logger.exception("Zero Division Error") + + log_record = processor.get_log_record(0) + + self.assertIsNotNone(log_record) + self.assertEqual(log_record.body, "Zero Division Error") + self.assertEqual( + log_record.attributes[SpanAttributes.EXCEPTION_TYPE], + ZeroDivisionError.__name__, + ) + self.assertEqual( + log_record.attributes[SpanAttributes.EXCEPTION_MESSAGE], + "division by zero", + ) + stack_trace = log_record.attributes[ + SpanAttributes.EXCEPTION_STACKTRACE + ] + self.assertIsInstance(stack_trace, str) + self.assertTrue("Traceback" in stack_trace) + self.assertTrue("ZeroDivisionError" in stack_trace) + self.assertTrue("division by zero" in stack_trace) + self.assertTrue(__file__ in stack_trace) + def test_log_exc_info_false(self): - """Exception information will be included in attributes""" + """Exception information will not be included in attributes""" processor, logger = set_up_test_logging(logging.NOTSET) try: