Skip to content

Commit 456d334

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

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

dev-packages/e2e-tests/test-applications/cloudflare-mcp/tests/index.test.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ test('sends spans for MCP tool calls', async ({ baseURL }) => {
4949
typeof mcpEvent === 'string' ||
5050
!('contexts' in mcpEvent) ||
5151
typeof requestEvent === 'string' ||
52-
!('contexts' in requestEvent)
52+
!('contexts' in requestEvent) ||
53+
!('spans' in requestEvent)
5354
) {
5455
throw new Error("Events don't have contexts");
5556
}
@@ -71,21 +72,55 @@ test('sends spans for MCP tool calls', async ({ baseURL }) => {
7172
'url.port': '38787',
7273
'url.scheme': 'http:',
7374
'server.address': 'localhost',
74-
'http.request.body.size': 120,
7575
'user_agent.original': 'node',
76-
'http.request.header.content_type': 'application/json',
7776
'network.protocol.name': 'HTTP/1.1',
78-
'mcp.server.extra': ' /|\ ^._.^ /|\ ',
79-
'http.response.status_code': 200,
8077
}),
8178
op: 'http.server',
82-
status: 'ok',
8379
origin: 'auto.http.cloudflare',
8480
});
8581

82+
expect(requestEvent.spans).toEqual([
83+
{
84+
data: {
85+
'sentry.origin': 'auto.http.cloudflare',
86+
'sentry.op': 'http.server',
87+
'sentry.source': 'url',
88+
'http.request.method': 'POST',
89+
'url.path': '/mcp',
90+
'url.full': 'http://localhost:38787/mcp',
91+
'url.port': '38787',
92+
'url.scheme': 'http:',
93+
'server.address': 'localhost',
94+
'http.request.body.size': 120,
95+
'user_agent.original': 'node',
96+
'http.request.header.accept': 'application/json, text/event-stream',
97+
'http.request.header.accept_encoding': 'br, gzip',
98+
'http.request.header.accept_language': '*',
99+
'http.request.header.cf_connecting_ip': '::1',
100+
'http.request.header.content_length': '120',
101+
'http.request.header.content_type': 'application/json',
102+
'http.request.header.host': 'localhost:38787',
103+
'http.request.header.sec_fetch_mode': 'cors',
104+
'http.request.header.user_agent': 'node',
105+
'network.protocol.name': 'HTTP/1.1',
106+
'mcp.server.extra': ' /|\ ^._.^ /|\ ',
107+
'http.response.status_code': 200,
108+
},
109+
description: 'fetch',
110+
op: 'http.server',
111+
parent_span_id: expect.any(String),
112+
span_id: expect.any(String),
113+
start_timestamp: expect.any(Number),
114+
status: 'ok',
115+
timestamp: expect.any(Number),
116+
trace_id: expect.any(String),
117+
origin: 'auto.http.cloudflare',
118+
},
119+
]);
120+
86121
expect(mcpEvent.contexts?.trace).toEqual({
87122
trace_id: expect.any(String),
88-
parent_span_id: requestEvent.contexts?.trace?.span_id,
123+
parent_span_id: requestEvent.spans?.[0]?.span_id,
89124
span_id: expect.any(String),
90125
op: 'mcp.server',
91126
origin: 'auto.function.mcp_server',

dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,34 @@ test.only('waitUntil', async ({ baseURL }) => {
137137
// Transaction trace context (root span - no status/response code, those are on the fetch child span)
138138
expect(transactionEvent.contexts?.trace).toMatchObject({
139139
op: 'http.server',
140-
status: 'ok',
141140
origin: 'auto.http.cloudflare',
142141
data: expect.objectContaining({
143142
'sentry.op': 'http.server',
144143
'sentry.origin': 'auto.http.cloudflare',
145-
'http.request.method': 'GET',
146144
'url.path': '/waitUntil',
147-
'http.response.status_code': 200,
148145
}),
149146
});
150147

148+
expect(transactionEvent.contexts?.trace).not.toEqual(
149+
expect.objectContaining({
150+
data: expect.objectContaining({
151+
'http.request.method': 'GET',
152+
'http.response.status_code': 200,
153+
}),
154+
}),
155+
);
156+
151157
expect(transactionEvent.spans).toEqual([
152158
expect.objectContaining({
159+
status: 'ok',
153160
description: 'fetch',
154161
op: 'http.server',
155162
origin: 'auto.http.cloudflare',
156163
parent_span_id: transactionEvent.contexts?.trace?.span_id,
164+
data: expect.objectContaining({
165+
'http.request.method': 'GET',
166+
'http.response.status_code': 200,
167+
}),
157168
}),
158169
expect.objectContaining({
159170
description: 'waitUntil',

packages/cloudflare/src/request.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export function wrapRequestHandler(
120120

121121
try {
122122
res = await handler();
123-
setHttpStatus(rootSpan, res.status);
124123
setHttpStatus(fetchSpan, res.status);
125124

126125
// After the handler runs, the span name might have been updated by nested instrumentation

packages/cloudflare/test/durableobject.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ describe('instrumentDurableObjectWithSentry', () => {
161161
const after = flush.mock.calls.length;
162162
const delta = after - before;
163163

164-
// Verify that exactly one flush call was made during this test
165-
expect(delta).toBe(1);
164+
// Verify that two flush calls were made during this test
165+
// The first flush is called when the response is captured
166+
// The second flush is called when the waitUntil promises are finished
167+
expect(delta).toBe(2);
166168
});
167169

168170
describe('instrumentPrototypeMethods option', () => {

packages/cloudflare/test/handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('withSentry', () => {
161161
expect(waitUntil).toBeCalled();
162162
vi.advanceTimersToNextTimer().runAllTimers();
163163
await Promise.all(waits);
164-
expect(flush).toHaveBeenCalledOnce();
164+
expect(flush).toHaveBeenCalledTimes(2);
165165
});
166166
});
167167

0 commit comments

Comments
 (0)