|
14 | 14 | get_start_span_function, |
15 | 15 | transform_anthropic_content_part, |
16 | 16 | ) |
17 | | -from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS |
| 17 | +from sentry_sdk.consts import OP, SPANDATA |
18 | 18 | from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration |
19 | 19 | from sentry_sdk.scope import should_send_default_pii |
20 | 20 | from sentry_sdk.tracing_utils import set_span_errored |
@@ -602,7 +602,7 @@ def _set_output_data( |
602 | 602 |
|
603 | 603 | def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any": |
604 | 604 | """ |
605 | | - Creates an AI Client Span for both non-streaming and streaming calls. |
| 605 | + Creates and manages an AI Client Span for both non-streaming and streaming calls. |
606 | 606 | """ |
607 | 607 | integration = kwargs.pop("integration") |
608 | 608 | if integration is None: |
@@ -633,6 +633,7 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any |
633 | 633 | exc_info = sys.exc_info() |
634 | 634 | with capture_internal_exceptions(): |
635 | 635 | _capture_exception(exc) |
| 636 | + span.__exit__(None, None, None) |
636 | 637 | reraise(*exc_info) |
637 | 638 |
|
638 | 639 | if isinstance(result, Stream): |
@@ -689,7 +690,7 @@ async def _sentry_patched_create_async( |
689 | 690 | f: "Any", *args: "Any", **kwargs: "Any" |
690 | 691 | ) -> "Any": |
691 | 692 | """ |
692 | | - Creates an AI Client Span for both non-streaming and streaming calls. |
| 693 | + Creates and manages an AI Client Span for both non-streaming and streaming calls. |
693 | 694 | """ |
694 | 695 | integration = kwargs.pop("integration") |
695 | 696 | if integration is None: |
@@ -720,6 +721,7 @@ async def _sentry_patched_create_async( |
720 | 721 | exc_info = sys.exc_info() |
721 | 722 | with capture_internal_exceptions(): |
722 | 723 | _capture_exception(exc) |
| 724 | + span.__exit__(None, None, None) |
723 | 725 | reraise(*exc_info) |
724 | 726 |
|
725 | 727 | if isinstance(result, AsyncStream): |
@@ -778,13 +780,7 @@ def _sentry_wrapped_create_sync(*args: "Any", **kwargs: "Any") -> "Any": |
778 | 780 | integration = sentry_sdk.get_client().get_integration(AnthropicIntegration) |
779 | 781 | kwargs["integration"] = integration |
780 | 782 |
|
781 | | - try: |
782 | | - return _sentry_patched_create_sync(f, *args, **kwargs) |
783 | | - finally: |
784 | | - span = sentry_sdk.get_current_span() |
785 | | - if span is not None and span.status == SPANSTATUS.INTERNAL_ERROR: |
786 | | - with capture_internal_exceptions(): |
787 | | - span.__exit__(None, None, None) |
| 783 | + return _sentry_patched_create_sync(f, *args, **kwargs) |
788 | 784 |
|
789 | 785 | return _sentry_wrapped_create_sync |
790 | 786 |
|
@@ -878,13 +874,7 @@ async def _sentry_wrapped_create_async(*args: "Any", **kwargs: "Any") -> "Any": |
878 | 874 | integration = sentry_sdk.get_client().get_integration(AnthropicIntegration) |
879 | 875 | kwargs["integration"] = integration |
880 | 876 |
|
881 | | - try: |
882 | | - return await _sentry_patched_create_async(f, *args, **kwargs) |
883 | | - finally: |
884 | | - span = sentry_sdk.get_current_span() |
885 | | - if span is not None and span.status == SPANSTATUS.INTERNAL_ERROR: |
886 | | - with capture_internal_exceptions(): |
887 | | - span.__exit__(None, None, None) |
| 877 | + return await _sentry_patched_create_async(f, *args, **kwargs) |
888 | 878 |
|
889 | 879 | return _sentry_wrapped_create_async |
890 | 880 |
|
|
0 commit comments