Skip to content

Commit c65f6c5

Browse files
committed
complete error spans in anthropic
1 parent b42c6c0 commit c65f6c5

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sentry_sdk
55
from sentry_sdk.ai.monitoring import record_token_usage
66
from sentry_sdk.ai.utils import set_data_normalized, get_start_span_function
7-
from sentry_sdk.consts import OP, SPANDATA
7+
from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS
88
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
99
from sentry_sdk.scope import should_send_default_pii
1010
from sentry_sdk.tracing_utils import set_span_errored
@@ -360,7 +360,13 @@ def _sentry_patched_create_sync(*args, **kwargs):
360360
integration = sentry_sdk.get_client().get_integration(AnthropicIntegration)
361361
kwargs["integration"] = integration
362362

363-
return _execute_sync(f, *args, **kwargs)
363+
try:
364+
return _execute_sync(f, *args, **kwargs)
365+
finally:
366+
span = sentry_sdk.get_current_span()
367+
if span is not None and span.status == SPANSTATUS.ERROR:
368+
with capture_internal_exceptions():
369+
span.__exit__(None, None, None)
364370

365371
return _sentry_patched_create_sync
366372

@@ -393,6 +399,12 @@ async def _sentry_patched_create_async(*args, **kwargs):
393399
integration = sentry_sdk.get_client().get_integration(AnthropicIntegration)
394400
kwargs["integration"] = integration
395401

396-
return await _execute_async(f, *args, **kwargs)
402+
try:
403+
return await _execute_async(f, *args, **kwargs)
404+
finally:
405+
span = sentry_sdk.get_current_span()
406+
if span is not None and span.status == SPANSTATUS.ERROR:
407+
with capture_internal_exceptions():
408+
span.__exit__(None, None, None)
397409

398410
return _sentry_patched_create_async

0 commit comments

Comments
 (0)