Skip to content
Draft
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
Expand Up @@ -81,7 +81,7 @@ export default Sentry.withSentry(
},
}),
{
async fetch(request, env) {
async fetch(request, env, ctx) {
const url = new URL(request.url);
switch (url.pathname) {
case '/rpc/throwException':
Expand All @@ -96,6 +96,20 @@ export default Sentry.withSentry(
}
}
break;
case '/waitUntil':
console.log('waitUntil called');

const longRunningTask = async () => {
await new Promise(resolve => setTimeout(resolve, 3000));

console.log('ʕっ•ᴥ•ʔっ');
Sentry.captureException(new Error('ʕノ•ᴥ•ʔノ ︵ ┻━┻'));
};

ctx.waitUntil(longRunningTask());

return new Response(null, { status: 200 });

case '/throwException':
throw new Error('To be recorded in Sentry.');
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForRequest } from '@sentry-internal/test-utils';
import { waitForError, waitForRequest, waitForTransaction } from '@sentry-internal/test-utils';
import { SDK_VERSION } from '@sentry/cloudflare';
import { waitForDebugger } from 'inspector';
import { WebSocket } from 'ws';

test('Index page', async ({ baseURL }) => {
Expand Down Expand Up @@ -82,3 +83,33 @@ test('sends user-agent header with SDK name and version in envelope requests', a
'user-agent': `sentry.javascript.cloudflare/${SDK_VERSION}`,
});
});

test('waitUntil', async ({ baseURL }) => {
const errorWaiter = waitForError('cloudflare-workers', () => true);
const waitUntilSpanWaiter = waitForTransaction('cloudflare-workers', span => span.transaction === 'waitUntil');
const httpTransactionWaiter = waitForTransaction(
'cloudflare-workers',
transactionEvent => transactionEvent.contexts?.trace?.op === 'http.server',
);

const response = await fetch(`${baseURL}/waitUntil`);

const transactionRequest = await httpTransactionWaiter;
const waitUntilSpan = await waitUntilSpanWaiter;
const errorEvent = await errorWaiter;

expect(response.status).toBe(200);

// All traceIds should be the same
expect(transactionRequest.contexts?.trace?.trace_id).toBe(waitUntilSpan.contexts?.trace?.trace_id);
expect(transactionRequest.contexts?.trace?.trace_id).toBe(errorEvent.contexts?.trace?.trace_id);

expect(errorEvent.exception?.values?.[0]?.value).toBe('ʕノ•ᴥ•ʔノ ︵ ┻━┻');
expect(errorEvent.breadcrumbs).toStrictEqual(waitUntilSpan.breadcrumbs);

expect(waitUntilSpan.contexts?.trace?.parent_span_id).toBe(transactionRequest.contexts?.trace?.span_id);

console.log(JSON.stringify(transactionRequest, null, 2));
console.log(JSON.stringify(waitUntilSpan, null, 2));
console.log(JSON.stringify(errorEvent, null, 2));
});
16 changes: 14 additions & 2 deletions packages/cloudflare/src/flush.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ExecutionContext } from '@cloudflare/workers-types';
import { startSpan, withScope } from '@sentry/core';

type FlushLock = {
readonly ready: Promise<void>;
Expand All @@ -22,9 +23,20 @@ export function makeFlushLock(context: ExecutionContext): FlushLock {
const originalWaitUntil = context.waitUntil.bind(context) as typeof context.waitUntil;
context.waitUntil = promise => {
pending++;

return originalWaitUntil(
promise.finally(() => {
if (--pending === 0) resolveAllDone();
// Wrap the promise in a new scope and transaction so spans created inside
// waitUntil callbacks are properly isolated from the HTTP request transaction
withScope(() =>
startSpan({ forceTransaction: true, op: 'cloudflare.wait_until', name: 'wait_until' }, async () => {
// By awaiting the promise inside the new scope, all of its continuations
// will execute in this isolated scope
await promise;
}),
).finally(() => {
if (--pending === 0) {
resolveAllDone();
}
}),
);
};
Expand Down
Loading