Skip to content

Commit 45abbe3

Browse files
fix(anthropic): Only finish relevant spans in .create() patches (#5716)
Anthropic spans are only created in `_sentry_patched_create()` and `_sentry_patched_create_async()`. Exit spans in these functions when the anthropic library function raises an exception. Resolves an edge case where `_sentry_patched_create()` or `_sentry_patched_create_async()` exits early without creating a span, causing `get_current_span()` to return a non-anthropic span.
1 parent 9cb8aa4 commit 45abbe3

1 file changed

Lines changed: 7 additions & 17 deletions

File tree

sentry_sdk/integrations/anthropic.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
get_start_span_function,
1515
transform_anthropic_content_part,
1616
)
17-
from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS
17+
from sentry_sdk.consts import OP, SPANDATA
1818
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
1919
from sentry_sdk.scope import should_send_default_pii
2020
from sentry_sdk.tracing_utils import set_span_errored
@@ -602,7 +602,7 @@ def _set_output_data(
602602

603603
def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any":
604604
"""
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.
606606
"""
607607
integration = kwargs.pop("integration")
608608
if integration is None:
@@ -633,6 +633,7 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
633633
exc_info = sys.exc_info()
634634
with capture_internal_exceptions():
635635
_capture_exception(exc)
636+
span.__exit__(None, None, None)
636637
reraise(*exc_info)
637638

638639
if isinstance(result, Stream):
@@ -689,7 +690,7 @@ async def _sentry_patched_create_async(
689690
f: "Any", *args: "Any", **kwargs: "Any"
690691
) -> "Any":
691692
"""
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.
693694
"""
694695
integration = kwargs.pop("integration")
695696
if integration is None:
@@ -720,6 +721,7 @@ async def _sentry_patched_create_async(
720721
exc_info = sys.exc_info()
721722
with capture_internal_exceptions():
722723
_capture_exception(exc)
724+
span.__exit__(None, None, None)
723725
reraise(*exc_info)
724726

725727
if isinstance(result, AsyncStream):
@@ -778,13 +780,7 @@ def _sentry_wrapped_create_sync(*args: "Any", **kwargs: "Any") -> "Any":
778780
integration = sentry_sdk.get_client().get_integration(AnthropicIntegration)
779781
kwargs["integration"] = integration
780782

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)
788784

789785
return _sentry_wrapped_create_sync
790786

@@ -878,13 +874,7 @@ async def _sentry_wrapped_create_async(*args: "Any", **kwargs: "Any") -> "Any":
878874
integration = sentry_sdk.get_client().get_integration(AnthropicIntegration)
879875
kwargs["integration"] = integration
880876

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)
888878

889879
return _sentry_wrapped_create_async
890880

0 commit comments

Comments
 (0)