Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
interface Env {
E2E_TEST_DSN: string;
AI: Ai;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Sentry from '@sentry/cloudflare';

export default {
async fetch(request) {
async fetch(request, env) {
const url = new URL(request.url);
// The handler runs inside the request span the Vite plugin's `withSentry` wrapper starts, so
// this is the `http.server` span.
Expand All @@ -14,6 +14,10 @@ export default {
}
case '/test-unhandled-error':
throw new Error('E2E test unhandled error');
case '/test-workers-ai': {
await env.AI.run('@cf/meta/llama-3.2-1b-instruct', { prompt: 'Say hi', max_tokens: 5 });
return Response.json({ traceId: spanContext?.traceId });
}
case '/test-span':
return Response.json({ spanId: spanContext?.spanId, traceId: spanContext?.traceId });
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { randomBytes } from 'node:crypto';
import { expect, test } from '@playwright/test';
import { EVENT_POLLING_OPTIONS, findErrorInTrace, findSpanInTrace, traceTarget } from '@sentry-internal/test-utils/cli';
import {
EVENT_POLLING_OPTIONS,
fetchSpanAttributes,
findErrorInTrace,
findSpanInTrace,
traceTarget,
} from '@sentry-internal/test-utils/cli';

// Set by global-setup.mjs once the worker for this run is deployed.
const workerUrl = process.env.E2E_TEST_WORKER_URL;
Expand Down Expand Up @@ -43,3 +49,24 @@ test('Sends a request span to Sentry', async () => {
.poll(() => findSpanInTrace(traceId, 'http.server'), EVENT_POLLING_OPTIONS)
.toMatchObject({ event_id: spanId });
});

test('Sends a Workers AI gen_ai span to Sentry', async () => {
const response = await fetch(`${workerUrl}/test-workers-ai`);
expect(response.status).toBe(200);
const { traceId } = await response.json();

console.log(`Polling for gen_ai.chat span: sentry trace view ${traceTarget(traceId)}`);

let spanId: string | undefined;
await expect
.poll(() => (spanId = findSpanInTrace(traceId, 'gen_ai.chat')?.event_id), EVENT_POLLING_OPTIONS)
.toBeDefined();

// Sentry stores `gen_ai.response.text` as `gen_ai.output.messages`.
await expect
.poll(() => fetchSpanAttributes(traceId, spanId!), EVENT_POLLING_OPTIONS)
.toMatchObject({
'gen_ai.input.messages': expect.stringContaining('Say hi'),
'gen_ai.output.messages': expect.stringContaining('"role":"assistant"'),
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"compatibility_date": "2026-05-20",
"compatibility_flags": ["nodejs_compat"],
"workers_dev": true,
"ai": { "binding": "AI" },
// Workers Logs keep the invocations of the last 7 days, so a failed CI run can still be inspected.
"observability": { "enabled": true },
}
Loading