Skip to content

Commit

Permalink
Update __init__.py
Browse files Browse the repository at this point in the history
Attempted to add traceback.format_exc() to solve issue with exceptions.
  • Loading branch information
kennykguo authored Jan 24, 2024
1 parent 975733c commit 629b719
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 629b719

Please sign in to comment.