Skip to content

fix(cloudflare): Auto-instrument classes re-exported from the worker entry - #23282

Draft
JPeer264 wants to merge 4 commits into
developfrom
jp/instrument-agent-vite-plugin
Draft

fix(cloudflare): Auto-instrument classes re-exported from the worker entry#23282
JPeer264 wants to merge 4 commits into
developfrom
jp/instrument-agent-vite-plugin

Conversation

@JPeer264

@JPeer264 JPeer264 commented Aug 11, 2026

Copy link
Copy Markdown
Member

The majority of additional code lines are tests

Agents are different, they need special care on top of the wrangler config. Because they are marked as DurableObjects, but have a different wrapper, they need extra hacks within the Vite plugin.

Issue

Before, specific type of import/exports didn't work properly and caused manual instrumentation again: https://github.com/sergical/cuzz/blob/4ec6080865e37491ff240007bbe886f907ad1de7/src/worker/index.ts#L11-L15

Solution(s)

First one

We had to add a agent return inside resolveWrapperKind, to correctly identify Agents in there as well, so we know when to add instrumentAgentWithSentry instead of instrumentDurableObjectWithSentry

Second one

When a class gets exported right away, we need to change that export:

export { MyAgent, MyDo } from './do' 

needs to be changed to the following in order to wrap it:

import { MyAgent as __SENTRY_REEXPORT_MyAgent__, MyDo as __SENTRY_REEXPORT_MyDo__ } from './do' 
const __SENTRY_WRAPPED_MyDurableObject__ = __SENTRY__.instrumentDurableObjectWithSentry((env) => ({}), __SENTRY_REEXPORT_MyDurableObject__);
export { __SENTRY_WRAPPED_MyAgent__ as MyAgent, __SENTRY_WRAPPED_MyDo__ as MyDo } from './do' 

This should fix what was needed before: getsentry/sentry-docs#18944


Clanker description

The Vite auto-instrument transform only wrapped classes declared in the entry module. An entry that just aggregates its Durable Objects, Agents and Workflows from other files — import { MyAgent } from './agent'; export { MyAgent }, or export { MyAgent } from './agent' — got no instrumentation at all, leaving manual instrument*WithSentry wrapping as the only option.

An import binding cannot be reassigned, so instead of renaming a declaration the transform now re-points those specifiers at a fresh wrapper binding and rebuilds the export statement, carrying unrelated specifiers over verbatim.

Agent detection was blind to the same shapes: collectAgentCandidates only considered local classes, so a re-exported Agent was never even offered to the cross-module base-class walk that already knew how to resolve it.

Star re-exports (export * from './do') still cannot be wrapped — they name no binding — so the warning stays, reworded to say that rather than blaming re-exports in general.

@JPeer264
JPeer264 requested a review from timfish August 11, 2026 12:13
@JPeer264 JPeer264 self-assigned this Aug 11, 2026
@JPeer264
JPeer264 requested a review from a team as a code owner August 11, 2026 12:13
@JPeer264
JPeer264 requested review from isaacs and mydea and removed request for a team August 11, 2026 12:13
@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 28.57 kB - -
@sentry/browser - with treeshaking flags 26.92 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.82 kB - -
@sentry/browser (incl. Tracing) 48.63 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.65 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.56 kB - -
@sentry/browser (incl. Tracing, Replay) 88.11 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.51 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 92.82 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 105.51 kB - -
@sentry/browser (incl. Feedback) 45.79 kB - -
@sentry/browser (incl. sendFeedback) 33.35 kB - -
@sentry/browser (incl. FeedbackAsync) 38.46 kB - -
@sentry/browser (incl. Metrics) 29.51 kB - -
@sentry/browser (incl. Logs) 29.8 kB - -
@sentry/browser (incl. Metrics & Logs) 30.43 kB - -
@sentry/react 30.31 kB - -
@sentry/react (incl. Tracing) 50.84 kB - -
@sentry/vue 35.69 kB - -
@sentry/vue (incl. Tracing) 50.88 kB - -
@sentry/svelte 28.59 kB - -
CDN Bundle 30.36 kB - -
CDN Bundle (incl. Tracing) 49.12 kB - -
CDN Bundle (incl. Logs, Metrics) 32.56 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 51.01 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 73 kB - -
CDN Bundle (incl. Tracing, Replay) 86.62 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.52 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 92.4 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.33 kB - -
CDN Bundle - uncompressed 89.97 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.82 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.26 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.51 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.42 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 266.32 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 272 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 280.01 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.68 kB - -
@sentry/nextjs (client) 53.41 kB - -
@sentry/sveltekit (client) 49.08 kB - -
@sentry/core/server 65.3 kB - -
@sentry/core/browser 52.37 kB - -
@sentry/node 122.34 kB +0.02% +20 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 85.22 kB - -
@sentry/node - without tracing 87.66 kB +0.03% +22 B 🔺
@sentry/node - without channel injection 102.04 kB +0.02% +17 B 🔺
@sentry/aws-serverless 95.82 kB +0.03% +24 B 🔺
@sentry/cloudflare (withSentry) - minified 199.61 kB +0.03% +44 B 🔺
@sentry/cloudflare (withSentry) 495.98 kB +0.06% +264 B 🔺

View base workflow run

@JPeer264
JPeer264 requested review from chargome and s1gr1d and removed request for isaacs and mydea August 11, 2026 14:16
@JPeer264
JPeer264 marked this pull request as draft August 11, 2026 14:46
@JPeer264
JPeer264 removed request for s1gr1d and timfish August 11, 2026 14:46
@JPeer264

Copy link
Copy Markdown
Member Author

Put back to draft. It seems there is a double instrumentation by accident

@JPeer264
JPeer264 removed the request for review from chargome August 12, 2026 14:27
@JPeer264
JPeer264 force-pushed the jp/instrument-agent-vite-plugin branch 2 times, most recently from 0b08c7b to 08f2abe Compare August 25, 2026 16:23
JPeer264 and others added 3 commits August 26, 2026 08:59
…entry

The Vite auto-instrument transform only wrapped classes declared in the entry
module. An entry that just aggregates its Durable Objects, Agents and Workflows
from other files — `import { MyAgent } from './agent'; export { MyAgent }`, or
`export { MyAgent } from './agent'` — got no instrumentation at all, leaving
manual `instrument*WithSentry` wrapping as the only option.

An import binding cannot be reassigned, so instead of renaming a declaration the
transform now re-points those specifiers at a fresh wrapper binding and rebuilds
the export statement, carrying unrelated specifiers over verbatim.

Agent detection was blind to the same shapes: `collectAgentCandidates` only
considered local classes, so a re-exported Agent was never even offered to the
cross-module base-class walk that already knew how to resolve it.

Star re-exports (`export * from './do'`) still cannot be wrapped — they name no
binding — so the warning stays, reworded to say that rather than blaming
re-exports in general.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`export { X } from './x'` has no local binding: the specifier's "local" name is
an export of the *source* module. Matching it against `workerEntrypointClasses`
— which structural detection populates from this module's own AST — wrapped a
re-exported class whenever an unrelated local class happened to share its name.

Configured names are unaffected; those are keyed by exported name, which is what
wrangler binds.

Also covers the re-export path for Workflows and WorkerEntrypoints, which went
through it untested.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…worker entry

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the jp/instrument-agent-vite-plugin branch from 08f2abe to 8746c0b Compare August 26, 2026 05:59
@JPeer264

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8746c0b. Configure here.

Comment thread packages/cloudflare/src/vite/transform.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant