Repository navigation
fix(cloudflare): auto-instrument when wrangler main is a package
#25343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
isaacs
wants to merge
4
commits into
develop
from
isaacschlueter/js-3725-sentrycloudflarevite-auto-instrumentation-skips-the-worker
+524
−39
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c3a46f
fix(cloudflare): auto-instrument when wrangler `main` is a package
isaacs 103b2dd
fix: bugbot
isaacs 2d9b239
fix(cloudflare): set the wrapped default export where its binding is set
isaacs 1698a1d
fix(cloudflare): wrap the default export as it is at the end of the m…
isaacs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
dev-packages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| dist | ||
| .wrangler | ||
| test-results | ||
| pnpm-lock.yaml |
10 changes: 10 additions & 0 deletions
10
.../e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/instrument.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { defineCloudflareOptions } from '@sentry/cloudflare'; | ||
|
|
||
| // The worker entry is a package, so the plugin reads this file from beside | ||
| // the wrangler config. | ||
| export default defineCloudflareOptions((env: { E2E_TEST_DSN: string }) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| environment: 'qa', | ||
| tunnel: 'http://localhost:3031/', | ||
| tracesSampleRate: 1.0, | ||
| })); |
29 changes: 29 additions & 0 deletions
29
...packages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "cloudflare-autoinstrument-package-entry", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "vite build", | ||
| "preview": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\" --log-level=$(test $CI && echo 'none' || echo 'log')", | ||
| "test": "playwright test", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:assert": "pnpm test" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/cloudflare": "file:../../packed/sentry-cloudflare-packed.tgz", | ||
| "worker-entry": "file:../../test-applications/cloudflare-autoinstrument-package-entry/worker-entry" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/vite-plugin": "^1.47.0", | ||
| "@cloudflare/workers-types": "^5.20260727.1", | ||
| "@playwright/test": "~1.63.0", | ||
| "@sentry/conventions": "0.27.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "vite": "^8.1.5", | ||
| "wrangler": "^4.114.0" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
.../e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/playwright.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
|
||
| export default getPlaywrightConfig( | ||
| { | ||
| startCommand: 'pnpm preview', | ||
| port: 8787, | ||
| }, | ||
| { | ||
| workers: '100%', | ||
| retries: 0, | ||
| }, | ||
| ); |
6 changes: 6 additions & 0 deletions
6
...e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/start-event-proxy.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'cloudflare-autoinstrument-package-entry', | ||
| }); |
35 changes: 35 additions & 0 deletions
35
...sts/test-applications/cloudflare-autoinstrument-package-entry/tests/package-entry.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { SENTRY_ENVIRONMENT, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; | ||
| import { HTTP_SERVER } from '@sentry/conventions/op'; | ||
| import { getSpanOp, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| // The worker entry is a package with no Sentry calls. Any event that arrives | ||
| // proves the plugin wrapped the module that wrangler's `main` resolved to. | ||
|
|
||
| test('wraps a package entry with withSentry (options from instrument.server.ts)', async ({ baseURL }) => { | ||
| const spanPromise = waitForStreamedSpan( | ||
| 'cloudflare-autoinstrument-package-entry', | ||
| span => getSpanOp(span) === HTTP_SERVER && span.is_segment, | ||
| ); | ||
|
|
||
| const res = await fetch(baseURL!); | ||
| expect(res.status).toBe(200); | ||
|
|
||
| const span = await spanPromise; | ||
| expect(span.attributes[SENTRY_ORIGIN]?.value).toBe('auto.http.cloudflare'); | ||
| // Only `instrument.server.ts` sets this environment. | ||
| expect(span.attributes[SENTRY_ENVIRONMENT]?.value).toBe('qa'); | ||
| }); | ||
|
|
||
| test('captures an error thrown by a package entry', async ({ baseURL }) => { | ||
| const errorPromise = waitForError( | ||
| 'cloudflare-autoinstrument-package-entry', | ||
| event => event.exception?.values?.[0]?.value === 'Thrown from a package entry', | ||
| ); | ||
|
|
||
| const res = await fetch(`${baseURL}/throw`); | ||
| expect(res.status).toBe(500); | ||
|
|
||
| const error = await errorPromise; | ||
| expect(error.environment).toBe('qa'); | ||
| }); |
7 changes: 7 additions & 0 deletions
7
...ckages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/vite.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { cloudflare } from '@cloudflare/vite-plugin'; | ||
| import { sentryCloudflareVitePlugin } from '@sentry/cloudflare/vite'; | ||
| import { defineConfig } from 'vite'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [cloudflare(), sentryCloudflareVitePlugin()], | ||
| }); |
11 changes: 11 additions & 0 deletions
11
...s/test-applications/cloudflare-autoinstrument-package-entry/worker-entry/fetch-handler.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Shaped like bundler output (e.g. `@astrojs/cloudflare`): the default export | ||
| // is a specifier, not `export default <expr>`. | ||
| var fetch_handler_default = { | ||
| async fetch(request) { | ||
| if (new URL(request.url).pathname === '/throw') { | ||
| throw new Error('Thrown from a package entry'); | ||
| } | ||
| return new Response('ok'); | ||
| }, | ||
| }; | ||
| export { fetch_handler_default as default }; |
9 changes: 9 additions & 0 deletions
9
...tests/test-applications/cloudflare-autoinstrument-package-entry/worker-entry/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "worker-entry", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "exports": { | ||
| "./fetch-handler": "./fetch-handler.js" | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
...ckages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/wrangler.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "cloudflare-autoinstrument-package-entry", | ||
| // A package export, not a file path. The Sentry plugin resolves it with Vite. | ||
| "main": "worker-entry/fetch-handler", | ||
| "compatibility_date": "2026-05-20", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.