Skip to content

Commit bf3310b

Browse files
committed
add pydantic test without mocks
1 parent 6638930 commit bf3310b

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

tests/integrations/pydantic_ai/test_pydantic_ai.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,3 +2604,31 @@ async def test_ai_client_span_gets_agent_from_scope(sentry_init, capture_events)
26042604

26052605
# Should not crash
26062606
assert transaction is not None
2607+
2608+
2609+
@pytest.mark.asyncio
2610+
async def test_set_usage_data_with_cache_tokens(sentry_init, capture_events):
2611+
"""Test that cache_read_tokens and cache_write_tokens are tracked."""
2612+
import sentry_sdk
2613+
from pydantic_ai.usage import RequestUsage
2614+
from sentry_sdk.integrations.pydantic_ai.spans.utils import _set_usage_data
2615+
from sentry_sdk.consts import SPANDATA
2616+
2617+
sentry_init(integrations=[PydanticAIIntegration()], traces_sample_rate=1.0)
2618+
events = capture_events()
2619+
2620+
with sentry_sdk.start_transaction(op="test", name="test"):
2621+
span = sentry_sdk.start_span(op="test_span")
2622+
usage = RequestUsage(
2623+
input_tokens=100,
2624+
output_tokens=50,
2625+
cache_read_tokens=80,
2626+
cache_write_tokens=20,
2627+
)
2628+
_set_usage_data(span, usage)
2629+
span.finish()
2630+
2631+
(event,) = events
2632+
(span_data,) = event["spans"]
2633+
assert span_data["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED] == 80
2634+
assert span_data["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE] == 20

0 commit comments

Comments
 (0)