Skip to content

Commit a5ede37

Browse files
committed
fixup! fix(cloudflare): Missing events inside waitUntil
1 parent 9b45d97 commit a5ede37

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

packages/cloudflare/src/request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export function wrapRequestHandler(
121121
try {
122122
res = await handler();
123123
setHttpStatus(rootSpan, res.status);
124+
setHttpStatus(fetchSpan, res.status);
124125

125126
// After the handler runs, the span name might have been updated by nested instrumentation
126127
// (e.g., Remix parameterizing routes). The span should already have the correct name

packages/cloudflare/test/request.test.ts

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ function addDelayedWaitUntil(context: ExecutionContext) {
1818
context.waitUntil(new Promise<void>(resolve => setTimeout(() => resolve())));
1919
}
2020

21+
function createMockExecutionContext(): ExecutionContext {
22+
return {
23+
waitUntil: vi.fn(),
24+
passThroughOnException: vi.fn(),
25+
};
26+
}
27+
2128
describe('withSentry', () => {
2229
beforeAll(() => {
2330
setAsyncLocalStorageAsyncContextStrategy();
@@ -46,7 +53,7 @@ describe('withSentry', () => {
4653
() => new Response('test'),
4754
);
4855

49-
expect(waitUntilSpy).toHaveBeenCalledTimes(1);
56+
expect(waitUntilSpy).toHaveBeenCalledTimes(3);
5057
expect(waitUntilSpy).toHaveBeenLastCalledWith(expect.any(Promise));
5158
});
5259

@@ -123,8 +130,10 @@ describe('withSentry', () => {
123130
const after = flushSpy.mock.calls.length;
124131
const delta = after - before;
125132

126-
// Verify that exactly one flush call was made during this test
127-
expect(delta).toBe(1);
133+
// Verify that two flush calls were made during this test
134+
// One for the flush after the request handler is done
135+
// and one for the waitUntil promise
136+
expect(delta).toBe(2);
128137
});
129138

130139
describe('scope instrumentation', () => {
@@ -285,12 +294,17 @@ describe('withSentry', () => {
285294
'sentry-release=2.1.12,sentry-public_key=public,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=0.3232',
286295
);
287296

297+
let sentryEventTransaction: Event = {};
288298
let sentryEvent: Event = {};
289299
await wrapRequestHandler(
290300
{
291301
options: {
292302
...MOCK_OPTIONS,
293303
tracesSampleRate: 0,
304+
beforeSendTransaction(event) {
305+
sentryEventTransaction = event;
306+
return null;
307+
},
294308
beforeSend(event) {
295309
sentryEvent = event;
296310
return null;
@@ -304,8 +318,20 @@ describe('withSentry', () => {
304318
return new Response('test');
305319
},
306320
);
321+
322+
// Wait for async span end and transaction capture
323+
await new Promise(resolve => setTimeout(resolve, 50));
324+
325+
expect(sentryEventTransaction.contexts?.trace).toEqual(
326+
expect.objectContaining({
327+
parent_span_id: '1121201211212012',
328+
span_id: expect.stringMatching(/[a-f0-9]{16}/),
329+
trace_id: '12312012123120121231201212312012',
330+
}),
331+
);
332+
307333
expect(sentryEvent.contexts?.trace).toEqual({
308-
parent_span_id: '1121201211212012',
334+
parent_span_id: sentryEventTransaction.contexts?.trace?.span_id,
309335
span_id: expect.stringMatching(/[a-f0-9]{16}/),
310336
trace_id: '12312012123120121231201212312012',
311337
});
@@ -342,7 +368,25 @@ describe('withSentry', () => {
342368
await new Promise(resolve => setTimeout(resolve, 50));
343369

344370
expect(sentryEvent.transaction).toEqual('GET /');
345-
expect(sentryEvent.spans).toHaveLength(0);
371+
expect(sentryEvent.spans).toHaveLength(1);
372+
expect(sentryEvent.spans).toEqual([
373+
expect.objectContaining({
374+
data: expect.any(Object),
375+
description: 'fetch',
376+
op: 'http.server',
377+
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
378+
span_id: expect.stringMatching(/[a-f0-9]{16}/),
379+
start_timestamp: expect.any(Number),
380+
timestamp: expect.any(Number),
381+
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
382+
origin: 'auto.http.cloudflare',
383+
status: 'ok',
384+
}),
385+
]);
386+
expect(sentryEvent.contexts?.trace?.data).toStrictEqual({
387+
...sentryEvent.spans?.[0]?.data,
388+
'sentry.sample_rate': 1,
389+
});
346390
expect(sentryEvent.contexts?.trace).toEqual({
347391
data: {
348392
'sentry.origin': 'auto.http.cloudflare',
@@ -370,10 +414,3 @@ describe('withSentry', () => {
370414
});
371415
});
372416
});
373-
374-
function createMockExecutionContext(): ExecutionContext {
375-
return {
376-
waitUntil: vi.fn(),
377-
passThroughOnException: vi.fn(),
378-
};
379-
}

0 commit comments

Comments
 (0)