From 629b7196a5f1cd83971292107e6457d51fc37ece Mon Sep 17 00:00:00 2001 From: Kenny Guo <110429254+kennykguo@users.noreply.github.com> Date: Tue, 23 Jan 2024 20:53:35 -0500 Subject: [PATCH] Update __init__.py Attempted to add traceback.format_exc() to solve issue with exceptions. --- .../src/opentelemetry/sdk/trace/__init__.py | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 6dae70b2f6b..5676e472ba6 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -938,20 +938,26 @@ def __exit__( exc_tb: Optional[TracebackType], ) -> None: """Ends context manager and calls `end` on the `Span`.""" - if exc_val is not None and self.is_recording(): - # Record the exception as an event - # pylint:disable=protected-access - if self._record_exception: - self.record_exception(exception=exc_val, escaped=True) - # Records status if span is used as context manager - # i.e. with tracer.start_span() as span: - if self._set_status_on_exception: - self.set_status( - Status( - status_code=StatusCode.ERROR, - description=f"{exc_type.__name__}: {exc_val}", + try: + if exc_val is not None and self.is_recording(): + # Record the exception as an event + # pylint:disable=protected-access + if self._record_exception: + stacktrace = traceback.format_exc() + self.record_exception(exception=exc_val, escaped=True) + # Records status if span is used as context manager + # i.e. with tracer.start_span() as span: + if self._set_status_on_exception: + self.set_status( + Status( + status_code=StatusCode.ERROR, + description=f"{exc_type.__name__}: {exc_val}", + ) ) - ) + except Exception as record_exception_error: + # Log or handle the error appropriately + print(f"Error recording exception: {record_exception_error}") + super().__exit__(exc_type, exc_val, exc_tb)