|
| 1 | +import sentry_sdk |
| 2 | +from sentry_sdk.ai.monitoring import ai_track |
| 3 | + |
| 4 | + |
| 5 | +def test_ai_track(sentry_init, capture_events): |
| 6 | + sentry_init(traces_sample_rate=1.0) |
| 7 | + events = capture_events() |
| 8 | + |
| 9 | + @ai_track("my tool") |
| 10 | + def tool(**kwargs): |
| 11 | + pass |
| 12 | + |
| 13 | + @ai_track("some test pipeline") |
| 14 | + def pipeline(): |
| 15 | + tool() |
| 16 | + |
| 17 | + with sentry_sdk.start_transaction(): |
| 18 | + pipeline() |
| 19 | + |
| 20 | + transaction = events[0] |
| 21 | + assert transaction["type"] == "transaction" |
| 22 | + assert len(transaction["spans"]) == 2 |
| 23 | + spans = transaction["spans"] |
| 24 | + |
| 25 | + ai_pipeline_span = spans[0] if spans[0]["op"] == "ai.pipeline" else spans[1] |
| 26 | + ai_run_span = spans[0] if spans[0]["op"] == "ai.run" else spans[1] |
| 27 | + |
| 28 | + assert ai_pipeline_span["description"] == "some test pipeline" |
| 29 | + assert ai_run_span["description"] == "my tool" |
| 30 | + |
| 31 | + |
| 32 | +def test_ai_track_with_tags(sentry_init, capture_events): |
| 33 | + sentry_init(traces_sample_rate=1.0) |
| 34 | + events = capture_events() |
| 35 | + |
| 36 | + @ai_track("my tool") |
| 37 | + def tool(**kwargs): |
| 38 | + pass |
| 39 | + |
| 40 | + @ai_track("some test pipeline") |
| 41 | + def pipeline(): |
| 42 | + tool() |
| 43 | + |
| 44 | + with sentry_sdk.start_transaction(): |
| 45 | + pipeline(sentry_tags={"user": "colin"}, sentry_data={"some_data": "value"}) |
| 46 | + |
| 47 | + transaction = events[0] |
| 48 | + assert transaction["type"] == "transaction" |
| 49 | + assert len(transaction["spans"]) == 2 |
| 50 | + spans = transaction["spans"] |
| 51 | + |
| 52 | + ai_pipeline_span = spans[0] if spans[0]["op"] == "ai.pipeline" else spans[1] |
| 53 | + ai_run_span = spans[0] if spans[0]["op"] == "ai.run" else spans[1] |
| 54 | + |
| 55 | + assert ai_pipeline_span["description"] == "some test pipeline" |
| 56 | + print(ai_pipeline_span) |
| 57 | + assert ai_pipeline_span["tags"]["user"] == "colin" |
| 58 | + assert ai_pipeline_span["data"]["some_data"] == "value" |
| 59 | + assert ai_run_span["description"] == "my tool" |
0 commit comments