|
1 | 1 | import type { ExecutionContext } from '@cloudflare/workers-types';
|
2 | 2 | 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'; |
5 | 12 | import type { NitroApp, NitroAppPlugin } from 'nitropack';
|
6 | 13 | import type { NuxtRenderHTMLContext } from 'nuxt/app';
|
7 | 14 | import { sentryCaptureErrorHook } from '../hooks/captureErrorHook';
|
@@ -86,27 +93,53 @@ export const sentryCloudflareNitroPlugin =
|
86 | 93 | context: event.context.cloudflare.context,
|
87 | 94 | };
|
88 | 95 |
|
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())); |
92 | 99 |
|
93 |
| - logger.log( |
94 |
| - `Patched Cloudflare handler (\`nitroApp.localFetch\`). ${ |
95 |
| - isolationScope === newIsolationScope ? 'Using existing' : 'Created new' |
96 |
| - } isolation scope.`, |
97 |
| - ); |
| 100 | + const traceData = getTraceData(); |
98 | 101 |
|
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 | + // }); |
100 | 120 | }
|
101 | 121 |
|
102 | 122 | return handlerTarget.apply(handlerThisArg, handlerArgs);
|
103 | 123 | },
|
104 | 124 | });
|
105 | 125 |
|
| 126 | + // todo: start span in a hook before the request handler |
| 127 | + |
106 | 128 | // @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 }) => { |
108 | 130 | // fixme: it's attaching the html meta tag but it's not connecting the trace
|
109 | 131 | // 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 | + |
110 | 143 | addSentryTracingMetaTags(html.head);
|
111 | 144 | });
|
112 | 145 |
|
|
0 commit comments