Skip to content

Commit 1efb56d

Browse files
committed
fixup! feat(cloudflare): Continue the Cloudflare trace of the invocation
1 parent 94302da commit 1efb56d

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎packages/cloudflare/src/utils/cloudflareTrace.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ const SPAN_ID_REGEXP = /^(?!0{16})[0-9a-f]{16}$/;
2323
* Sets the propagation context of the current scope to the Cloudflare trace of the invocation, so
2424
* errors, logs and spans get the trace id of the spans that Cloudflare exports.
2525
*
26-
* Does nothing when Cloudflare does not trace the invocation, or when the runtime does not expose
26+
* Does nothing when the request `headers` have a `sentry-trace` header: an incoming Sentry trace
27+
* keeps precedence, so traces from instrumented clients stay connected. Also does nothing when
28+
* Cloudflare does not trace the invocation, or when the runtime does not expose
2729
* `Span.spanContext()`. The sampling decision is left to the SDK.
2830
*/
29-
export function continueCloudflareTrace(context: unknown): void {
31+
export function continueCloudflareTrace(context: unknown, headers?: Headers): void {
32+
if (headers?.get('sentry-trace')) {
33+
return;
34+
}
35+
3036
let spanContext: CloudflareSpanContext | undefined;
3137
try {
3238
const span = (context as { tracing?: CloudflareTracing } | undefined)?.tracing?.getActiveSpan?.();

‎packages/cloudflare/src/wrapRequestHandlerWithInit.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ export function wrapRequestHandlerWithInit(
159159
return continueTrace(
160160
{ sentryTrace: request.headers.get('sentry-trace') || '', baggage: request.headers.get('baggage') },
161161
() => {
162-
// An incoming Sentry trace keeps precedence, so traces from instrumented clients stay connected.
163-
if (!request.headers.get('sentry-trace')) {
164-
continueCloudflareTrace(context);
165-
}
162+
continueCloudflareTrace(context, request.headers);
166163

167164
// Note: This span will not have a duration unless I/O happens in the handler. This is
168165
// because of how the cloudflare workers runtime works.

‎packages/cloudflare/test/utils/cloudflareTrace.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ describe('continueCloudflareTrace()', () => {
2727
});
2828
});
2929

30+
it('keeps the propagation context when the request has a sentry-trace header', () => {
31+
const context = {
32+
tracing: {
33+
getActiveSpan: () => ({
34+
isTraced: true,
35+
spanContext: () => ({ traceId: CLOUDFLARE_TRACE_ID, spanId: CLOUDFLARE_SPAN_ID, traceFlags: 1 }),
36+
}),
37+
},
38+
};
39+
const headers = new Headers({ 'sentry-trace': '12345678901234567890123456789012-1234567890123456-1' });
40+
41+
withScope(scope => {
42+
const propagationContext = scope.getPropagationContext();
43+
44+
continueCloudflareTrace(context, headers);
45+
46+
expect(scope.getPropagationContext()).toBe(propagationContext);
47+
});
48+
});
49+
3050
it.each([
3151
['there is no execution context', undefined],
3252
['the runtime has no tracing API', {}],

0 commit comments

Comments
 (0)