-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Add debugId sync APIs between web worker and main thread #16981
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
808245c
feat(browser): Add debugId sync APIs between web worker and main thread
Lms24 683e95e
streamline tests
Lms24 a294ec4
.
Lms24 0426e92
add integration test, add public export
Lms24 3acf7a8
wip e2e test
Lms24 a52eb3c
add working e2e tests
Lms24 6f773f6
fix lint error
Lms24 e656490
make `isWebWorkerMessage` more robust
Lms24 9ff304c
better naming
Lms24 fd6cec8
support multiple workers
Lms24 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
14 changes: 14 additions & 0 deletions
14
dev-packages/browser-integration-tests/suites/integrations/webWorker/assets/worker.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,14 @@ | ||
self._sentryDebugIds = { | ||
'Error at http://sentry-test.io/worker.js': 'worker-debug-id-789', | ||
}; | ||
|
||
self.postMessage({ | ||
_sentryMessage: true, | ||
_sentryDebugIds: self._sentryDebugIds, | ||
}); | ||
|
||
self.addEventListener('message', event => { | ||
if (event.data.type === 'throw-error') { | ||
throw new Error('Worker error for testing'); | ||
} | ||
}); |
18 changes: 18 additions & 0 deletions
18
dev-packages/browser-integration-tests/suites/integrations/webWorker/init.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,18 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
// Initialize Sentry with webWorker integration | ||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
}); | ||
|
||
const worker = new Worker('/worker.js'); | ||
|
||
Sentry.addIntegration(Sentry.webWorkerIntegration({ worker })); | ||
|
||
const btn = document.getElementById('errWorker'); | ||
|
||
btn.addEventListener('click', () => { | ||
worker.postMessage({ | ||
type: 'throw-error', | ||
}); | ||
}); |
Empty file.
9 changes: 9 additions & 0 deletions
9
dev-packages/browser-integration-tests/suites/integrations/webWorker/template.html
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 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<button id="errWorker">Throw error in worker</button> | ||
</body> | ||
</html> |
38 changes: 38 additions & 0 deletions
38
dev-packages/browser-integration-tests/suites/integrations/webWorker/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,38 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/core'; | ||
import { sentryTest } from '../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; | ||
|
||
sentryTest('Assigns web worker debug IDs when using webWorkerIntegration', async ({ getLocalTestUrl, page }) => { | ||
const bundle = process.env.PW_BUNDLE as string | undefined; | ||
if (bundle != null && !bundle.includes('esm') && !bundle.includes('cjs')) { | ||
sentryTest.skip(); | ||
} | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
page.route('**/worker.js', route => { | ||
route.fulfill({ | ||
path: `${__dirname}/assets/worker.js`, | ||
}); | ||
}); | ||
|
||
const button = page.locator('#errWorker'); | ||
await button.click(); | ||
|
||
const errorEvent = await errorEventPromise; | ||
|
||
expect(errorEvent.debug_meta?.images).toBeDefined(); | ||
|
||
const debugImages = errorEvent.debug_meta?.images || []; | ||
|
||
expect(debugImages.length).toBe(1); | ||
|
||
debugImages.forEach(image => { | ||
expect(image.type).toBe('sourcemap'); | ||
expect(image.debug_id).toEqual('worker-debug-id-789'); | ||
expect(image.code_file).toEqual('http://sentry-test.io/worker.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 |
---|---|---|
|
@@ -28,7 +28,7 @@ function fixPackageJson(cwd: string): void { | |
|
||
// 2. Fix volta extends | ||
if (!packageJson.volta) { | ||
throw new Error('No volta config found, please provide one!'); | ||
throw new Error("No volta config found, please add one to the test app's package.json!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjusted this message because I didn't realize the volta config needed to be added to the e2e test apps. Might be a me-problem but I think the message is now fool-proof :D |
||
} | ||
|
||
if (typeof packageJson.volta.extends === 'string') { | ||
|
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/browser-webworker-vite/.npmrc
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,2 @@ | ||
@sentry:registry=http://127.0.0.1:4873 | ||
@sentry-internal:registry=http://127.0.0.1:4873 |
21 changes: 21 additions & 0 deletions
21
dev-packages/e2e-tests/test-applications/browser-webworker-vite/index.html
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,21 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + TS</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
<button id="trigger-error" type="button" style="background-color: #dc3545; color: white"> | ||
Trigger Worker Error | ||
</button> | ||
<button id="trigger-error-2" type="button" style="background-color: #dc3545; color: white"> | ||
Trigger Worker 2 Error | ||
</button> | ||
<button id="trigger-error-3" type="button" style="background-color: #dc3545; color: white"> | ||
Trigger Worker 3 (lazily added) Error | ||
</button> | ||
</body> | ||
</html> |
29 changes: 29 additions & 0 deletions
29
dev-packages/e2e-tests/test-applications/browser-webworker-vite/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": "browser-webworker-vite", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "rm -rf dist && tsc && vite build", | ||
"preview": "vite preview --port 3030", | ||
"test": "playwright test", | ||
"test:build": "pnpm install && pnpm build", | ||
"test:assert": "pnpm test" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "~1.53.2", | ||
"@sentry-internal/test-utils": "link:../../../test-utils", | ||
"typescript": "~5.8.3", | ||
"vite": "^7.0.4" | ||
}, | ||
"dependencies": { | ||
"@sentry/browser": "latest || *", | ||
"@sentry/vite-plugin": "^3.5.0" | ||
}, | ||
"volta": { | ||
"node": "20.19.2", | ||
"yarn": "1.22.22", | ||
"pnpm": "9.15.9" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
dev-packages/e2e-tests/test-applications/browser-webworker-vite/playwright.config.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,10 @@ | ||
import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
||
const config = getPlaywrightConfig({ | ||
startCommand: `pnpm preview`, | ||
eventProxyFile: 'start-event-proxy.mjs', | ||
eventProxyPort: 3031, | ||
port: 3030, | ||
}); | ||
|
||
export default config; |
44 changes: 44 additions & 0 deletions
44
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/main.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,44 @@ | ||
import MyWorker from './worker.ts?worker'; | ||
import MyWorker2 from './worker2.ts?worker'; | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
Sentry.init({ | ||
dsn: import.meta.env.PUBLIC_E2E_TEST_DSN, | ||
environment: import.meta.env.MODE || 'development', | ||
tracesSampleRate: 1.0, | ||
debug: true, | ||
integrations: [Sentry.browserTracingIntegration()], | ||
tunnel: 'http://localhost:3031/', // proxy server | ||
}); | ||
|
||
const worker = new MyWorker(); | ||
const worker2 = new MyWorker2(); | ||
|
||
const webWorkerIntegration = Sentry.webWorkerIntegration({ worker: [worker, worker2] }); | ||
Sentry.addIntegration(webWorkerIntegration); | ||
|
||
worker.addEventListener('message', event => { | ||
// this is part of the test, do not delete | ||
console.log('received message from worker:', event.data.msg); | ||
}); | ||
|
||
document.querySelector<HTMLButtonElement>('#trigger-error')!.addEventListener('click', () => { | ||
worker.postMessage({ | ||
msg: 'TRIGGER_ERROR', | ||
}); | ||
}); | ||
|
||
document.querySelector<HTMLButtonElement>('#trigger-error-2')!.addEventListener('click', () => { | ||
worker2.postMessage({ | ||
msg: 'TRIGGER_ERROR', | ||
}); | ||
}); | ||
|
||
document.querySelector<HTMLButtonElement>('#trigger-error-3')!.addEventListener('click', async () => { | ||
const Worker3 = await import('./worker3.ts?worker'); | ||
const worker3 = new Worker3.default(); | ||
webWorkerIntegration.addWorker(worker3); | ||
worker3.postMessage({ | ||
msg: 'TRIGGER_ERROR', | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/vite-env.d.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 @@ | ||
/// <reference types="vite/client" /> |
17 changes: 17 additions & 0 deletions
17
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.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,17 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
// type cast necessary because TS thinks this file is part of the main | ||
// thread where self is of type `Window` instead of `Worker` | ||
Sentry.registerWebWorker({ self: self as unknown as Worker }); | ||
|
||
// Let the main thread know the worker is ready | ||
self.postMessage({ | ||
msg: 'WORKER_READY', | ||
}); | ||
|
||
self.addEventListener('message', event => { | ||
|
||
if (event.data.msg === 'TRIGGER_ERROR') { | ||
// This will throw an uncaught error in the worker | ||
throw new Error(`Uncaught error in worker`); | ||
} | ||
}); |
17 changes: 17 additions & 0 deletions
17
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.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,17 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
// type cast necessary because TS thinks this file is part of the main | ||
// thread where self is of type `Window` instead of `Worker` | ||
Sentry.registerWebWorker({ self: self as unknown as Worker }); | ||
|
||
// Let the main thread know the worker is ready | ||
self.postMessage({ | ||
msg: 'WORKER_2_READY', | ||
}); | ||
|
||
self.addEventListener('message', event => { | ||
|
||
if (event.data.msg === 'TRIGGER_ERROR') { | ||
// This will throw an uncaught error in the worker | ||
throw new Error(`Uncaught error in worker 2`); | ||
} | ||
}); |
17 changes: 17 additions & 0 deletions
17
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.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,17 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
// type cast necessary because TS thinks this file is part of the main | ||
// thread where self is of type `Window` instead of `Worker` | ||
Sentry.registerWebWorker({ self: self as unknown as Worker }); | ||
|
||
// Let the main thread know the worker is ready | ||
self.postMessage({ | ||
msg: 'WORKER_3_READY', | ||
}); | ||
|
||
self.addEventListener('message', event => { | ||
|
||
if (event.data.msg === 'TRIGGER_ERROR') { | ||
// This will throw an uncaught error in the worker | ||
throw new Error(`Uncaught error in worker 3`); | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/browser-webworker-vite/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: 'browser-webworker-vite', | ||
}); |
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.