feat(anthropic): Emit AI Client Spans for synchronous messages.stream()
#5565
4 issues
code-review: Found 4 issues (1 medium, 3 low)
Medium
Span never closed if exception occurs after span.__enter__() - `sentry_sdk/integrations/anthropic.py:789-809`
In _wrap_message_stream_manager_enter, span.__enter__() is called at line 789, but there's no try/finally block to ensure the span is closed if an exception occurs before the iterator wrapper's finally block runs. If _set_common_input_data() or the iterator assignment at lines 791-809 throws, the span remains open indefinitely. Other similar functions like _sentry_patched_create_sync have a finally block to close the span on errors.
Low
Test missing assertion for GEN_AI_SYSTEM span data - `tests/integrations/anthropic/test_anthropic.py:420-423`
The new test_stream_messages test does not assert that SPANDATA.GEN_AI_SYSTEM is set to 'anthropic'. The existing test_streaming_create_message test (line 306) includes this assertion. Since _set_common_input_data is called by _wrap_message_stream_manager_enter and does set this attribute (in anthropic.py line 289), the assertion should be included for test completeness.
Also found at:
tests/integrations/anthropic/test_anthropic.py:842-843tests/integrations/anthropic/test_anthropic.py:1788-1791
Test missing assertion for GEN_AI_RESPONSE_ID span data - `tests/integrations/anthropic/test_anthropic.py:439`
The new test_stream_messages test does not assert that SPANDATA.GEN_AI_RESPONSE_ID is set. The existing test_streaming_create_message test (line 325) includes this assertion. Since the same iterator wrapper (_wrap_synchronous_message_iterator) is used for both streaming paths and the response_id is captured in _collect_ai_data and set via _set_output_data, this assertion should be included for test coverage parity.
Malformed comment contains copy-paste error - `tests/integrations/anthropic/test_anthropic.py:2919`
The comment on line 2919 says # input_tokens should be total: 19 + 2846 = test_stream_messages_input_tokens_include_cache_read_streaming but should say = 2865. This appears to be a copy-paste error where the new test function name was accidentally inserted into the comment. While this doesn't affect runtime behavior, it may confuse future maintainers.
Duration: 4m 9s · Tokens: 1.7M in / 19.2k out · Cost: $2.36 (+extraction: $0.00, +merge: $0.00, +fix_gate: $0.01)
Annotations
Check warning on line 809 in sentry_sdk/integrations/anthropic.py
sentry-warden / warden: code-review
Span never closed if exception occurs after span.__enter__()
In `_wrap_message_stream_manager_enter`, `span.__enter__()` is called at line 789, but there's no try/finally block to ensure the span is closed if an exception occurs before the iterator wrapper's `finally` block runs. If `_set_common_input_data()` or the iterator assignment at lines 791-809 throws, the span remains open indefinitely. Other similar functions like `_sentry_patched_create_sync` have a `finally` block to close the span on errors.