Skip to content

Commit 08a0202

Browse files
committed
debug
1 parent 1ad1a9c commit 08a0202

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

packages/cloudflare/src/request.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export function wrapRequestHandler(
108108
res = await handler();
109109
setHttpStatus(span, res.status);
110110
} catch (e) {
111+
// DEBUG: Log error path
112+
if (typeof console !== 'undefined' && console.log) {
113+
console.log('[REQUEST.TS] Taking ERROR path, calling flush');
114+
}
111115
span.end();
112116
if (captureErrors) {
113117
captureException(e, { mechanism: { handled: false, type: 'auto.http.cloudflare' } });
@@ -119,8 +123,21 @@ export function wrapRequestHandler(
119123
// Classify response to detect actual streaming
120124
const classification = classifyResponseStreaming(res);
121125

126+
// DEBUG: Log classification result
127+
if (typeof console !== 'undefined' && console.log) {
128+
console.log('[REQUEST.TS] Classification:', {
129+
isStreaming: classification.isStreaming,
130+
hasBody: !!classification.response.body,
131+
contentType: classification.response.headers.get('content-type'),
132+
contentLength: classification.response.headers.get('content-length'),
133+
});
134+
}
135+
122136
if (classification.isStreaming && classification.response.body) {
123137
// Streaming response detected - monitor consumption to keep span alive
138+
if (typeof console !== 'undefined' && console.log) {
139+
console.log('[REQUEST.TS] Taking STREAMING path');
140+
}
124141
const [clientStream, monitorStream] = classification.response.body.tee();
125142

126143
// Monitor stream consumption and end span when complete
@@ -153,6 +170,9 @@ export function wrapRequestHandler(
153170
}
154171

155172
// Non-streaming response - end span immediately and return original
173+
if (typeof console !== 'undefined' && console.log) {
174+
console.log('[REQUEST.TS] Taking NON-STREAMING path, calling flush');
175+
}
156176
span.end();
157177
waitUntil?.(flush(2000));
158178
return classification.response;

packages/cloudflare/test/request.test.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ describe('withSentry', () => {
9191
});
9292

9393
test('flush must be called when all waitUntil are done', async () => {
94+
// Also spy on the exported flush function to see how many times it's called
95+
let exportedFlushCallCount = 0;
96+
const originalExportedFlush = SentryCore.flush;
97+
const exportedFlushSpy = vi.spyOn(SentryCore, 'flush').mockImplementation(async (timeout?: number) => {
98+
exportedFlushCallCount++;
99+
console.log(`[EXPORTED flush() #${exportedFlushCallCount}] timeout: ${timeout}`);
100+
// Call through to the real implementation
101+
return originalExportedFlush(timeout);
102+
});
103+
94104
let flushCallCount = 0;
95105
const originalFlush = SentryCore.Client.prototype.flush;
96106

@@ -99,15 +109,24 @@ describe('withSentry', () => {
99109
timeout?: number,
100110
) {
101111
flushCallCount++;
102-
console.log(`[FLUSH #${flushCallCount}] Client: ${this?.constructor?.name || 'unknown'}, timeout: ${timeout}`);
112+
console.log(`\n[FLUSH #${flushCallCount}] Client: ${this?.constructor?.name || 'unknown'}, timeout: ${timeout}`);
103113
const stack = new Error().stack?.split('\n').slice(2, 10).join('\n ');
104114
console.log(` Stack:\n ${stack}`);
105115
console.log(` this._flushLock:`, this._flushLock ? 'exists' : 'undefined');
106116
console.log(
107117
` Recursion check: flushCallCount=${flushCallCount}, spy.calls.length=${flushSpy.mock.calls.length}`,
108118
);
109119

110-
// Important: Return a resolved promise to avoid triggering parent flush logic
120+
// Check if we're being called from within another flush
121+
const stackString = new Error().stack || '';
122+
const flushInStack = (stackString.match(/\.flush\(/g) || []).length;
123+
console.log(` Number of .flush( in stack: ${flushInStack}`);
124+
125+
// Try calling the original to see if that changes behavior
126+
// Comment this out to see if it matters:
127+
// return originalFlush.call(this, timeout);
128+
129+
// Or just return immediately:
111130
return Promise.resolve(true);
112131
});
113132
flushSpy.mockClear(); // Explicitly clear before test
@@ -116,28 +135,33 @@ describe('withSentry', () => {
116135
onTestFinished(() => {
117136
vi.useRealTimers();
118137
flushSpy.mockRestore();
138+
exportedFlushSpy.mockRestore();
119139
});
120140
let pendingCount = 0;
121141
const waits: Promise<unknown>[] = [];
142+
143+
// Track the ORIGINAL waitUntil to see if makeFlushLock wraps it
144+
let originalWaitUntilCallCount = 0;
122145
const waitUntil = vi.fn(promise => {
123-
const callNum = waitUntil.mock.calls.length;
146+
originalWaitUntilCallCount++;
147+
const callNum = originalWaitUntilCallCount;
124148
pendingCount++;
125149
console.log(
126-
`[WAITUNTIL #${callNum}] Pending: ${pendingCount}, Promise type: ${promise?.constructor?.name || 'unknown'}`,
150+
`[ORIGINAL_WAITUNTIL #${callNum}] Pending: ${pendingCount}, Promise type: ${promise?.constructor?.name || 'unknown'}`,
127151
);
128-
const stack = new Error().stack?.split('\n').slice(2, 5).join('\n ');
152+
const stack = new Error().stack?.split('\n').slice(2, 6).join('\n ');
129153
console.log(` Stack:\n ${stack}`);
130154

131155
// Wrap the promise to log when it resolves
132156
const wrappedPromise = promise.then(
133157
val => {
134158
pendingCount--;
135-
console.log(`[WAITUNTIL #${callNum} RESOLVED] Pending now: ${pendingCount}`);
159+
console.log(`[ORIGINAL_WAITUNTIL #${callNum} RESOLVED] Pending now: ${pendingCount}`);
136160
return val;
137161
},
138162
err => {
139163
pendingCount--;
140-
console.log(`[WAITUNTIL #${callNum} REJECTED] Pending now: ${pendingCount}`, err);
164+
console.log(`[ORIGINAL_WAITUNTIL #${callNum} REJECTED] Pending now: ${pendingCount}`, err);
141165
throw err;
142166
},
143167
);
@@ -175,7 +199,8 @@ describe('withSentry', () => {
175199

176200
console.log('\n=== TEST END ===');
177201
console.log(`Total waitUntil calls: ${waitUntil.mock.calls.length}`);
178-
console.log(`Total flush calls: ${flushSpy.mock.calls.length}`);
202+
console.log(`Total EXPORTED flush() calls: ${exportedFlushCallCount}`);
203+
console.log(`Total Client.flush() calls: ${flushSpy.mock.calls.length}`);
179204
console.log(`Flush calls before resolve: ${callsBeforeResolve}`);
180205

181206
// Check that flush was called at least once after waitUntils resolved
@@ -188,10 +213,16 @@ describe('withSentry', () => {
188213
console.error('\n=== ASSERTION FAILURE ===');
189214
console.error(`Expected exactly 1 NEW flush call, but got ${delta}`);
190215
console.error(`Calls before: ${callsBeforeResolve}, Calls after: ${callsAfterResolve}`);
191-
console.error('\nAll flush call arguments:');
216+
console.error(`\nExported flush() was called ${exportedFlushCallCount} times`);
217+
console.error(`Client.flush() was called ${callsAfterResolve} times`);
218+
console.error('\nAll Client.flush() call arguments:');
192219
flushSpy.mock.calls.forEach((call, i) => {
193220
console.error(` Call ${i + 1}:`, call);
194221
});
222+
console.error('\nAll exported flush() call arguments:');
223+
exportedFlushSpy.mock.calls.forEach((call, i) => {
224+
console.error(` Call ${i + 1}:`, call);
225+
});
195226
}
196227

197228
// Ideally it should be exactly 1, but verify the delta to avoid CI flakiness

0 commit comments

Comments
 (0)