diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index f937ece8958..8d6630b39a4 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -962,21 +962,24 @@ 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: + pass super().__exit__(exc_type, exc_val, exc_tb) def record_exception(