|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction, waitForError } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('should create AI spans with correct attributes and error linking', async ({ page }) => { |
| 5 | + const aiTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => { |
| 6 | + return transactionEvent.transaction === 'GET /ai-error-test'; |
| 7 | + }); |
| 8 | + |
| 9 | + const errorEventPromise = waitForError('nextjs-15', async errorEvent => { |
| 10 | + return errorEvent.exception?.values?.[0]?.value?.includes('Tool call failed'); |
| 11 | + }); |
| 12 | + |
| 13 | + await page.goto('/ai-error-test'); |
| 14 | + |
| 15 | + const aiTransaction = await aiTransactionPromise; |
| 16 | + const errorEvent = await errorEventPromise; |
| 17 | + |
| 18 | + expect(aiTransaction).toBeDefined(); |
| 19 | + expect(aiTransaction.transaction).toBe('GET /ai-error-test'); |
| 20 | + |
| 21 | + const spans = aiTransaction.spans || []; |
| 22 | + |
| 23 | + // Each generateText call should create 2 spans: one for the pipeline and one for doGenerate |
| 24 | + // Plus a span for the tool call |
| 25 | + // TODO: For now, this is sadly not fully working - the monkey patching of the ai package is not working |
| 26 | + // because of this, only spans that are manually opted-in at call time will be captured |
| 27 | + // this may be fixed by https://github.com/vercel/ai/pull/6716 in the future |
| 28 | + const aiPipelineSpans = spans.filter(span => span.op === 'gen_ai.invoke_agent'); |
| 29 | + const aiGenerateSpans = spans.filter(span => span.op === 'gen_ai.generate_text'); |
| 30 | + const toolCallSpans = spans.filter(span => span.op === 'gen_ai.execute_tool'); |
| 31 | + |
| 32 | + expect(aiPipelineSpans.length).toBeGreaterThanOrEqual(1); |
| 33 | + expect(aiGenerateSpans.length).toBeGreaterThanOrEqual(1); |
| 34 | + expect(toolCallSpans.length).toBeGreaterThanOrEqual(0); |
| 35 | + |
| 36 | + expect(errorEvent).toBeDefined(); |
| 37 | + |
| 38 | + //Verify error is linked to the same trace as the transaction |
| 39 | + expect(errorEvent?.contexts?.trace?.trace_id).toBe(aiTransaction.contexts?.trace?.trace_id); |
| 40 | +}); |
0 commit comments