Skip to content

Commit 3ccab0c

Browse files
committed
delete continueTraceFromPropagationContext
1 parent 5085e58 commit 3ccab0c

File tree

3 files changed

+16
-118
lines changed

3 files changed

+16
-118
lines changed

packages/cloudflare/src/request.ts

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
captureException,
55
continueTrace,
66
flush,
7-
getCurrentScope,
87
getHttpSpanDetailsFromUrlObject,
98
parseStringToURLObject,
109
SEMANTIC_ATTRIBUTE_SENTRY_OP,
@@ -17,18 +16,7 @@ import { addCloudResourceContext, addCultureContext, addRequest } from './scope-
1716
import { init } from './sdk';
1817

1918
interface RequestHandlerWrapperOptions {
20-
options: CloudflareOptions & {
21-
/**
22-
* Enable or disable the automatic continuation of traces from the propagation context.
23-
*
24-
* When enabled, the SDK will continue a trace from the propagation context if it is present.
25-
*
26-
* When disabled, the SDK will fall back to the default case of continuing a trace from the request headers if they are present.
27-
*
28-
* @default false
29-
*/
30-
continueTraceFromPropagationContext?: boolean;
31-
};
19+
options: CloudflareOptions;
3220
request: Request<unknown, IncomingRequestCfProperties<unknown>>;
3321
context: ExecutionContext;
3422
}
@@ -82,40 +70,25 @@ export function wrapRequestHandler(
8270
}
8371
}
8472

85-
// Check if we already have active trace data - if so, don't wrap with continueTrace
86-
// This allows us to continue an existing trace from the parent context (e.g. in Nuxt)
87-
const existingPropagationContext = getCurrentScope().getPropagationContext();
88-
if (options.continueTraceFromPropagationContext && existingPropagationContext?.traceId) {
89-
return startSpan({ name, attributes }, cloudflareStartSpanCallback(handler, context));
90-
}
91-
92-
// No active trace, create one from headers
9373
return continueTrace(
9474
{ sentryTrace: request.headers.get('sentry-trace') || '', baggage: request.headers.get('baggage') },
9575
() => {
9676
// Note: This span will not have a duration unless I/O happens in the handler. This is
9777
// because of how the cloudflare workers runtime works.
9878
// See: https://developers.cloudflare.com/workers/runtime-apis/performance/
99-
return startSpan({ name, attributes }, cloudflareStartSpanCallback(handler, context));
79+
return startSpan({ name, attributes }, async (span: Span) => {
80+
try {
81+
const res = await handler();
82+
setHttpStatus(span, res.status);
83+
return res;
84+
} catch (e) {
85+
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
86+
throw e;
87+
} finally {
88+
context?.waitUntil(flush(2000));
89+
}
90+
});
10091
},
10192
);
10293
});
10394
}
104-
105-
function cloudflareStartSpanCallback(
106-
handler: (...args: unknown[]) => Response | Promise<Response>,
107-
context?: ExecutionContext,
108-
) {
109-
return async (span: Span) => {
110-
try {
111-
const res = await handler();
112-
setHttpStatus(span, res.status);
113-
return res;
114-
} catch (e) {
115-
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
116-
throw e;
117-
} finally {
118-
context?.waitUntil(flush(2000));
119-
}
120-
};
121-
}

packages/cloudflare/test/request.test.ts

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -220,79 +220,6 @@ describe('withSentry', () => {
220220
trace_id: '12312012123120121231201212312012',
221221
});
222222
});
223-
224-
test('uses existing propagation context when continueTraceFromPropagationContext is enabled', async () => {
225-
const mockGetCurrentScope = vi.spyOn(SentryCore, 'getCurrentScope').mockReturnValue({
226-
// return an existing propagation context
227-
getPropagationContext: () => ({
228-
traceId: '12312012123120121231201212312012',
229-
spanId: '1121201211212012',
230-
}),
231-
} as any);
232-
233-
const mockStartSpan = vi.spyOn(SentryCore, 'startSpan');
234-
const mockContinueTrace = vi.spyOn(SentryCore, 'continueTrace');
235-
236-
const mockRequest = new Request('https://example.com') as any;
237-
// Headers should be ignored when continuing from propagation context
238-
mockRequest.headers.set('sentry-trace', '99999999999999999999999999999999-9999999999999999-1');
239-
240-
await wrapRequestHandler(
241-
{
242-
options: {
243-
...MOCK_OPTIONS,
244-
continueTraceFromPropagationContext: true,
245-
},
246-
request: mockRequest,
247-
context: createMockExecutionContext(),
248-
},
249-
() => new Response('test'),
250-
);
251-
252-
// Should use startSpan directly instead of continueTrace
253-
expect(mockStartSpan).toHaveBeenCalledTimes(1);
254-
expect(mockContinueTrace).not.toHaveBeenCalled();
255-
256-
mockGetCurrentScope.mockRestore();
257-
mockStartSpan.mockRestore();
258-
mockContinueTrace.mockRestore();
259-
});
260-
261-
test('falls back to header-based trace when continueTraceFromPropagationContext is disabled or no context exists', async () => {
262-
const mockGetCurrentScope = vi.spyOn(SentryCore, 'getCurrentScope').mockReturnValue({
263-
// return an existing propagation context
264-
getPropagationContext: () => ({
265-
traceId: '12312012123120121231201212312012',
266-
spanId: '1121201211212012',
267-
}),
268-
} as any);
269-
270-
const mockContinueTrace = vi.spyOn(SentryCore, 'continueTrace').mockImplementation((_, callback) => {
271-
return callback();
272-
});
273-
274-
const mockRequest = new Request('https://example.com') as any;
275-
mockRequest.headers.set('sentry-trace', '99999999999999999999999999999999-9999999999999999-1');
276-
277-
await wrapRequestHandler(
278-
{
279-
options: {
280-
...MOCK_OPTIONS,
281-
continueTraceFromPropagationContext: false, // Explicitly disabled
282-
},
283-
request: mockRequest,
284-
context: createMockExecutionContext(),
285-
},
286-
() => new Response('test'),
287-
);
288-
289-
// Should use continueTrace even with existing propagation context when option is disabled
290-
expect(mockContinueTrace).toHaveBeenCalledTimes(1);
291-
292-
mockGetCurrentScope.mockRestore();
293-
mockContinueTrace.mockRestore();
294-
});
295-
296223
test('creates a span that wraps request handler', async () => {
297224
const mockRequest = new Request('https://example.com') as any;
298225
mockRequest.cf = {

packages/nuxt/src/runtime/plugins/sentry-cloudflare.server.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const sentryCloudflareNitroPlugin =
7777
async apply(handlerTarget, handlerThisArg, handlerArgs: [string, unknown]) {
7878
setAsyncLocalStorageAsyncContextStrategy();
7979

80-
const sentryOptions = typeof optionsOrFn === 'function' ? optionsOrFn(nitroApp) : optionsOrFn;
80+
const cloudflareOptions = typeof optionsOrFn === 'function' ? optionsOrFn(nitroApp) : optionsOrFn;
8181
const pathname = handlerArgs[0];
8282
const event = handlerArgs[1];
8383

@@ -86,7 +86,7 @@ export const sentryCloudflareNitroPlugin =
8686
return handlerTarget.apply(handlerThisArg, handlerArgs);
8787
} else {
8888
const requestHandlerOptions = {
89-
options: { ...sentryOptions, continueTraceFromPropagationContext: true },
89+
options: cloudflareOptions,
9090
request: { ...event, url: `${event.protocol}//${event.host}${pathname}` },
9191
context: event.context.cloudflare.context,
9292
};
@@ -98,7 +98,7 @@ export const sentryCloudflareNitroPlugin =
9898

9999
const traceData = getTraceData();
100100
if (traceData && Object.keys(traceData).length > 0) {
101-
// Storing trace data in the event context for later use in HTML meta tags (enables correct connection of parent/child span relationships)
101+
// Storing trace data in the event context for later use in HTML meta-tags (enables correct connection of parent/child span relationships)
102102
// @ts-expect-error Storing a new key in the event context
103103
event.context[TRACE_DATA_KEY] = traceData;
104104
logger.log('Stored trace data in the event context.');
@@ -116,8 +116,6 @@ export const sentryCloudflareNitroPlugin =
116116
},
117117
});
118118

119-
// fixme: multiple pageload spans in one trace
120-
121119
// @ts-expect-error - 'render:html' is a valid hook name in the Nuxt context
122120
nitroApp.hooks.hook('render:html', (html: NuxtRenderHTMLContext, { event }: { event: H3Event }) => {
123121
const storedTraceData = event.context[TRACE_DATA_KEY] as ReturnType<typeof getTraceData> | undefined;

0 commit comments

Comments
 (0)