Skip to content

Commit 72f2ac2

Browse files
committed
fixup! fix(cloudflare): Auto-instrument classes re-exported from the worker entry
1 parent f4345f4 commit 72f2ac2

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

packages/cloudflare/src/vite/transform.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ interface TransformState {
237237
* wrapped with. `wrappedClasses` counts both.
238238
*/
239239
autoWrapped: Set<ExportName>;
240+
/**
241+
* Top-level bindings already assigned an `instrument*WithSentry(...)` result
242+
* (`const MyDO = instrumentDurableObjectWithSentry(...)`). Exporting one by
243+
* specifier must report it as wrapped rather than wrap it a second time.
244+
*/
245+
manuallyWrappedLocals: Set<string>;
240246
}
241247

242248
/**
@@ -263,12 +269,6 @@ function buildMergedOptionsDeclaration(
263269
`const opts = (${optionsFn})(env); ` +
264270
`return { ...opts, rpcTracePropagationBindings: [${names}, ...(opts?.rpcTracePropagationBindings ?? [])] }; };\n`
265271
);
266-
/**
267-
* Top-level bindings already assigned an `instrument*WithSentry(...)` result
268-
* (`const MyDO = instrumentDurableObjectWithSentry(...)`). Exporting one by
269-
* specifier must report it as wrapped rather than wrap it a second time.
270-
*/
271-
manuallyWrappedLocals: Set<string>;
272272
}
273273

274274
/**
@@ -538,7 +538,7 @@ function wrapCrossModuleSpecifier(
538538
// The class may already be hand-wrapped in its own module, which this transform cannot see. The
539539
// emitted guard returns such a class as-is instead of nesting a second wrapper around it.
540540
prelude.push(
541-
`const ${wrappedName} = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.${WRAPPER_METHODS[kind]}, ${ctx.optionsFn}, ${target});`,
541+
`const ${wrappedName} = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.${WRAPPER_METHODS[kind]}, ${state.optionsFn}, ${target});`,
542542
);
543543
state.wrappedClasses.add(exportedName);
544544
state.autoWrapped.add(exportedName);

packages/cloudflare/test/vite/autoInstrument.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ describe('sentryCloudflareAutoInstrumentPlugin', () => {
315315
expect(result.code).toBe(
316316
[
317317
"import * as __SENTRY__ from '@sentry/cloudflare';",
318+
'const __SENTRY_OPTIONS__ = (env) => { const opts = (() => undefined)(env); return { ...opts, rpcTracePropagationBindings: ["MY_AGENT", ...(opts?.rpcTracePropagationBindings ?? [])] }; };',
318319
"import { MyAgent } from './agent';",
319-
'const __SENTRY_WRAPPED_MyAgent__ = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentAgentWithSentry, () => undefined, MyAgent);',
320+
'const __SENTRY_WRAPPED_MyAgent__ = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentAgentWithSentry, __SENTRY_OPTIONS__, MyAgent);',
320321
'export { __SENTRY_WRAPPED_MyAgent__ as MyAgent };',
321322
].join('\n'),
322323
);
@@ -332,8 +333,9 @@ describe('sentryCloudflareAutoInstrumentPlugin', () => {
332333
expect(result.code).toBe(
333334
[
334335
"import * as __SENTRY__ from '@sentry/cloudflare';",
336+
'const __SENTRY_OPTIONS__ = (env) => { const opts = (() => undefined)(env); return { ...opts, rpcTracePropagationBindings: ["MY_AGENT", ...(opts?.rpcTracePropagationBindings ?? [])] }; };',
335337
"import { MyAgent as __SENTRY_REEXPORT_MyAgent__ } from './agent';",
336-
'const __SENTRY_WRAPPED_MyAgent__ = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentAgentWithSentry, () => undefined, __SENTRY_REEXPORT_MyAgent__);',
338+
'const __SENTRY_WRAPPED_MyAgent__ = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentAgentWithSentry, __SENTRY_OPTIONS__, __SENTRY_REEXPORT_MyAgent__);',
337339
'export { __SENTRY_WRAPPED_MyAgent__ as MyAgent };',
338340
].join('\n'),
339341
);
@@ -352,8 +354,9 @@ describe('sentryCloudflareAutoInstrumentPlugin', () => {
352354
expect(result.code).toBe(
353355
[
354356
"import * as __SENTRY__ from '@sentry/cloudflare';",
357+
'const __SENTRY_OPTIONS__ = (env) => { const opts = (() => undefined)(env); return { ...opts, rpcTracePropagationBindings: ["MY_AGENT", ...(opts?.rpcTracePropagationBindings ?? [])] }; };',
355358
"import { MyAgent as __SENTRY_REEXPORT_MyAgent__ } from './do';",
356-
'const __SENTRY_WRAPPED_MyAgent__ = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentDurableObjectWithSentry, () => undefined, __SENTRY_REEXPORT_MyAgent__);',
359+
'const __SENTRY_WRAPPED_MyAgent__ = __SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentDurableObjectWithSentry, __SENTRY_OPTIONS__, __SENTRY_REEXPORT_MyAgent__);',
357360
'export { __SENTRY_WRAPPED_MyAgent__ as MyAgent };',
358361
].join('\n'),
359362
);

packages/cloudflare/test/vite/transform.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ describe('same-worker RPC binding floor', () => {
922922
expect(result.code).not.toContain('rpcTracePropagationBindings');
923923
});
924924

925-
it('drops a binding whose class is re-exported from another module', () => {
925+
it('keeps a binding whose class is re-exported from another module', () => {
926926
const code = ['export { MyDO } from "./myDo";', 'export default { fetch() {} };'].join('\n');
927927

928928
const result = transform(code, {
@@ -931,8 +931,10 @@ describe('same-worker RPC binding floor', () => {
931931
sameWorkerBindings: [{ bindingName: 'MY_DO', className: 'MyDO' }],
932932
})!;
933933

934-
expect(result.code).toContain('const __SENTRY_OPTIONS__ = () => undefined;');
935-
expect(result.code).not.toContain('rpcTracePropagationBindings');
934+
expect(result.code).toContain('rpcTracePropagationBindings: ["MY_DO",');
935+
expect(result.code).toContain(
936+
'__SENTRY__._INTERNAL_wrapUnlessInstrumented(__SENTRY__.instrumentDurableObjectWithSentry, __SENTRY_OPTIONS__, __SENTRY_REEXPORT_MyDO__)',
937+
);
936938
});
937939

938940
it('leaves the output untouched when there are no same-worker bindings', () => {

0 commit comments

Comments
 (0)