Skip to content

Commit 243b681

Browse files
committed
add some todo comments for trace propagation
1 parent 617fd21 commit 243b681

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

packages/cloudflare/src/request.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
captureException,
44
continueTrace,
55
flush,
6+
getCurrentScope,
67
getHttpSpanDetailsFromUrlObject,
8+
getTraceData,
79
parseStringToURLObject,
810
SEMANTIC_ATTRIBUTE_SENTRY_OP,
911
setHttpStatus,
@@ -69,6 +71,40 @@ export function wrapRequestHandler(
6971
}
7072
}
7173

74+
// fixme: at this point, there is no active span
75+
76+
// Check if we already have active trace data - if so, don't wrap with continueTrace
77+
// This allows us to continue an existing trace from the parent context (e.g., Nuxt)
78+
// todo: create an option for opting out of continueTrace
79+
const existingPropagationContext = getCurrentScope().getPropagationContext();
80+
if (existingPropagationContext?.traceId) {
81+
return startSpan(
82+
{
83+
name,
84+
attributes,
85+
},
86+
async span => {
87+
// fixme: same as 2
88+
console.log('::traceData 2', getTraceData());
89+
console.log('::propagationContext 2', JSON.stringify(getCurrentScope().getPropagationContext()));
90+
91+
try {
92+
const res = await handler();
93+
setHttpStatus(span, res.status);
94+
return res;
95+
} catch (e) {
96+
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
97+
throw e;
98+
} finally {
99+
context?.waitUntil(flush(2000));
100+
}
101+
},
102+
);
103+
}
104+
105+
console.log('request.headers', request.headers);
106+
107+
// No active trace, create one from headers
72108
return continueTrace(
73109
{ sentryTrace: request.headers.get('sentry-trace') || '', baggage: request.headers.get('baggage') },
74110
() => {
@@ -81,6 +117,9 @@ export function wrapRequestHandler(
81117
attributes,
82118
},
83119
async span => {
120+
console.log('::traceData 3', getTraceData());
121+
console.log('::propagationContext 3', JSON.stringify(getCurrentScope().getPropagationContext()));
122+
84123
try {
85124
const res = await handler();
86125
setHttpStatus(span, res.status);

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import type { ExecutionContext } from '@cloudflare/workers-types';
22
import type { CloudflareOptions } from '@sentry/cloudflare';
3-
import { setAsyncLocalStorageAsyncContextStrategy, wrapRequestHandler } from '@sentry/cloudflare';
4-
import { getDefaultIsolationScope, getIsolationScope, logger } from '@sentry/core';
3+
import {
4+
getActiveSpan,
5+
getTraceData,
6+
setAsyncLocalStorageAsyncContextStrategy,
7+
spanToJSON,
8+
wrapRequestHandler,
9+
} from '@sentry/cloudflare';
10+
import { continueTrace, getCurrentScope, getDefaultIsolationScope, getIsolationScope, logger } from '@sentry/core';
11+
import type { H3Event } from 'h3';
512
import type { NitroApp, NitroAppPlugin } from 'nitropack';
613
import type { NuxtRenderHTMLContext } from 'nuxt/app';
714
import { sentryCaptureErrorHook } from '../hooks/captureErrorHook';
@@ -86,27 +93,53 @@ export const sentryCloudflareNitroPlugin =
8693
context: event.context.cloudflare.context,
8794
};
8895

89-
const isolationScope = getIsolationScope();
90-
const newIsolationScope =
91-
isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope;
96+
// fixme same as 5
97+
console.log('::traceData 1', getTraceData());
98+
console.log('::propagationContext 1', JSON.stringify(getCurrentScope().getPropagationContext()));
9299

93-
logger.log(
94-
`Patched Cloudflare handler (\`nitroApp.localFetch\`). ${
95-
isolationScope === newIsolationScope ? 'Using existing' : 'Created new'
96-
} isolation scope.`,
97-
);
100+
const traceData = getTraceData();
98101

99-
return wrapRequestHandler(requestHandlerOptions, () => handlerTarget.apply(handlerThisArg, handlerArgs));
102+
// return continueTrace({ sentryTrace: traceData['sentry-trace'] || '', baggage: traceData.baggage }, () => {
103+
return wrapRequestHandler(requestHandlerOptions, () => {
104+
const isolationScope = getIsolationScope();
105+
const newIsolationScope =
106+
isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope;
107+
108+
logger.log(
109+
`Patched Cloudflare handler (\`nitroApp.localFetch\`). ${
110+
isolationScope === newIsolationScope ? 'Using existing' : 'Created new'
111+
} isolation scope.`,
112+
);
113+
114+
console.log('::traceData 4', getTraceData());
115+
console.log('::propagationContext 4', JSON.stringify(getCurrentScope().getPropagationContext()));
116+
117+
return handlerTarget.apply(handlerThisArg, handlerArgs);
118+
});
119+
// });
100120
}
101121

102122
return handlerTarget.apply(handlerThisArg, handlerArgs);
103123
},
104124
});
105125

126+
// todo: start span in a hook before the request handler
127+
106128
// @ts-expect-error - 'render:html' is a valid hook name in the Nuxt context
107-
nitroApp.hooks.hook('render:html', (html: NuxtRenderHTMLContext) => {
129+
nitroApp.hooks.hook('render:html', (html: NuxtRenderHTMLContext, { event }: { event: H3Event }) => {
108130
// fixme: it's attaching the html meta tag but it's not connecting the trace
109131
// fixme: its' actually connecting the trace but the meta tags are cached
132+
console.log('event.headers', event.headers);
133+
console.log('event.node.req.headers.cache-control', event.node.req.headers['cache-control']);
134+
console.log('event.context', event.context);
135+
136+
const span = getActiveSpan();
137+
138+
console.log('::active span', span ? spanToJSON(span) : 'no active span');
139+
140+
console.log('::traceData 5', getTraceData());
141+
console.log('::propagationContext 5', JSON.stringify(getCurrentScope().getPropagationContext()));
142+
110143
addSentryTracingMetaTags(html.head);
111144
});
112145

0 commit comments

Comments
 (0)