Skip to content

Commit 5b205b2

Browse files
JPeer264claude
andauthored
test(cloudflare): Assert Workers AI gen_ai spans in the send-to-sentry E2E test
Call a Workers AI model from the deployed Worker and check that the gen_ai.chat span reaches Sentry with its input and output messages. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 9ac9641 commit 5b205b2

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
interface Env {
22
E2E_TEST_DSN: string;
33
SLEEP_WORKFLOW: Workflow;
4+
AI: Ai;
45
}

‎dev-packages/e2e-tests/test-applications/cloudflare-workers-send-to-sentry/src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export default {
2727
}
2828
case '/test-unhandled-error':
2929
throw new Error('E2E test unhandled error');
30+
case '/test-workers-ai': {
31+
await env.AI.run('@cf/meta/llama-3.2-1b-instruct', { prompt: 'Say hi', max_tokens: 5 });
32+
return Response.json({ traceId: spanContext?.traceId });
33+
}
3034
case '/test-span':
3135
return Response.json({ spanId: spanContext?.spanId, traceId: spanContext?.traceId });
3236
case '/test-workflow-sleep': {

‎dev-packages/e2e-tests/test-applications/cloudflare-workers-send-to-sentry/tests/send-to-sentry.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { randomBytes } from 'node:crypto';
22
import { expect, test } from '@playwright/test';
33
import {
44
EVENT_POLLING_OPTIONS,
5+
fetchSpanAttributes,
56
fetchTrace,
67
findErrorInTrace,
78
findSpanInTrace,
@@ -73,3 +74,22 @@ test('Sends the spans of Workflow steps before the Workflow goes to sleep', asyn
7374
);
7475
expect(['running', 'waiting']).toContain(status);
7576
});
77+
78+
test('Sends a Workers AI gen_ai span to Sentry', async () => {
79+
const { traceId }: { traceId: string } = JSON.parse(await fetchFromWorker(`${workerUrl}/test-workers-ai`, 200));
80+
81+
console.log(`Polling for gen_ai.chat span: sentry trace view ${traceTarget(traceId)}`);
82+
83+
let spanId: string | undefined;
84+
await expect
85+
.poll(() => (spanId = findSpanInTrace(traceId, 'gen_ai.chat')?.event_id), EVENT_POLLING_OPTIONS)
86+
.toBeDefined();
87+
88+
// Sentry stores `gen_ai.response.text` as `gen_ai.output.messages`.
89+
await expect
90+
.poll(() => fetchSpanAttributes(traceId, spanId!), EVENT_POLLING_OPTIONS)
91+
.toMatchObject({
92+
'gen_ai.input.messages': expect.stringContaining('Say hi'),
93+
'gen_ai.output.messages': expect.stringContaining('"role":"assistant"'),
94+
});
95+
});

‎dev-packages/e2e-tests/test-applications/cloudflare-workers-send-to-sentry/wrangler.jsonc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"compatibility_date": "2026-05-20",
77
"compatibility_flags": ["nodejs_compat"],
88
"workers_dev": true,
9+
"ai": { "binding": "AI" },
910
// Workers Logs keep the invocations of the last 7 days, so a failed CI run can still be inspected.
1011
"observability": { "enabled": true },
1112
// Workflow names are unique per account, so deployWorker() renames it to the worker name.

0 commit comments

Comments
 (0)