feat: support openTelemetry for browser mode#9180
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
This PR adds OpenTelemetry tracing support for Vitest's browser mode, enabling trace collection from browser runtime in addition to the existing Node.js side tracing. The implementation propagates trace context from the Node.js orchestrator to browser contexts (orchestrator iframe and test iframes) using OTEL carrier objects, allowing proper parent-child span relationships across runtime boundaries.
Key Changes
- Added
browserSdkPathconfiguration option to specify a browser-compatible OpenTelemetry SDK - Implemented trace context propagation from Node.js to browser using OTEL carriers
- Added traces for browser pool operations, orchestrator lifecycle, tester execution, and browser commands
Reviewed changes
Copilot reviewed 28 out of 29 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
packages/vitest/src/utils/traces.ts |
Added bind() and flush() methods; made SDK type support optional forceFlush; added @vite-ignore for dynamic imports |
packages/vitest/src/types/browser.ts |
Added otelCarrier field to BrowserTesterOptions for context propagation |
packages/vitest/src/runtime/config.ts |
Added openTelemetry config to SerializedConfig for browser runtime |
packages/vitest/src/public/browser.ts |
Exported Traces and OTELCarrier for browser use |
packages/vitest/src/node/types/config.ts |
Added browserSdkPath to OpenTelemetry config interface |
packages/vitest/src/node/pools/browser.ts |
Instrumented browser pool with vitest.browser, vitest.browser.open, and vitest.browser.run spans |
packages/vitest/src/node/config/serializeConfig.ts |
Serialized OpenTelemetry config for browser runtime |
packages/vitest/src/node/config/resolveConfig.ts |
Resolved browserSdkPath to absolute path |
packages/browser/src/node/plugin.ts |
Added browser SDK to Vite optimization entries |
packages/browser/src/client/utils.ts |
Added traces to BrowserRunnerState |
packages/browser/src/client/tester/tester.ts |
Initialized traces in tester, created root span, and instrumented test execution |
packages/browser/src/client/tester/tester-utils.ts |
Added tracing to browser commands |
packages/browser/src/client/tester/runner.ts |
Implemented trace method for browser test runner |
packages/browser/src/client/orchestrator.ts |
Created orchestrator traces and propagated context to iframes |
packages/browser/src/client/channel.ts |
Added otelCarrier to IframePrepareEvent |
packages/browser/scripts/build-client.js |
Added build wrapper script for client watch mode |
packages/browser/package.json |
Updated dev:client script; added @opentelemetry/api dependency |
test/cli/test/open-telemetry.test.ts |
Added test case for browser SDK configuration |
test/cli/package.json |
Added OpenTelemetry browser dependencies |
test/cli/fixtures/otel-tests/otel.browser.sdk.js |
Added minimal browser SDK fixture for testing |
test/core/test/exports.test.ts |
Updated snapshot for new Traces export |
examples/opentelemetry/vite.config.ts |
Added browserSdkPath to example config |
examples/opentelemetry/src/other.test.ts |
Added example tests demonstrating browser tracing |
examples/opentelemetry/package.json |
Added OpenTelemetry browser dependencies |
examples/opentelemetry/otel-browser.js |
Created example browser SDK setup |
examples/opentelemetry/jaeger-config.yml |
Configured CORS for browser trace exports |
docs/guide/open-telemetry.md |
Added browser mode documentation section |
docs/config/experimental.md |
Documented browserSdkPath configuration |
pnpm-lock.yaml |
Added OpenTelemetry browser dependencies and updated jsdom |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
sheremet-va
left a comment
There was a problem hiding this comment.
Looks good to me! Thank you for working on this.
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [@vitest/coverage-v8](https://github.com/vitest-dev/vitest/tree/main/packages/coverage-v8#readme) ([source](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-v8)) | [`4.0.16` → `4.0.18`](https://renovatebot.com/diffs/npm/@vitest%2fcoverage-v8/4.0.16/4.0.18) |  |  | | [vitest](https://vitest.dev) ([source](https://github.com/vitest-dev/vitest/tree/HEAD/packages/vitest)) | [`4.0.16` → `4.0.18`](https://renovatebot.com/diffs/npm/vitest/4.0.16/4.0.18) |  |  | --- ### Release Notes <details> <summary>vitest-dev/vitest (@​vitest/coverage-v8)</summary> ### [`v4.0.18`](https://github.com/vitest-dev/vitest/releases/tag/v4.0.18) [Compare Source](vitest-dev/vitest@v4.0.17...v4.0.18) ##### 🚀 Experimental Features - **experimental**: Add `onModuleRunner` hook to `worker.init` - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9286](vitest-dev/vitest#9286) [<samp>(ea837)</samp>](vitest-dev/vitest@ea837de7d) ##### 🐞 Bug Fixes - Use `meta.url` in `createRequire` - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9441](vitest-dev/vitest#9441) [<samp>(e0572)</samp>](vitest-dev/vitest@e057281ca) - **browser**: Hide injected data-testid attributes - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9503](vitest-dev/vitest#9503) [<samp>(f8989)</samp>](vitest-dev/vitest@f89899cd8) - **ui**: Process artifact attachments when generating HTML reporter - by [@​macarie](https://github.com/macarie) in [#​9472](vitest-dev/vitest#9472) [<samp>(22543)</samp>](vitest-dev/vitest@225435647) ##### [View changes on GitHub](vitest-dev/vitest@v4.0.17...v4.0.18) ### [`v4.0.17`](https://github.com/vitest-dev/vitest/releases/tag/v4.0.17) [Compare Source](vitest-dev/vitest@v4.0.16...v4.0.17) ##### 🚀 Experimental Features - Support openTelemetry for browser mode - by [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9180](vitest-dev/vitest#9180) [<samp>(1ec3a)</samp>](vitest-dev/vitest@1ec3a8b68) - Support TRACEPARENT and TRACESTATE environment variables for OpenTelemetry context propagation - by [@​Copilot](https://github.com/Copilot), **hi-ogawa** and [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9295](vitest-dev/vitest#9295) [<samp>(876cb)</samp>](vitest-dev/vitest@876cb84c2) ##### 🐞 Bug Fixes - Improve asymmetric matcher diff readability by unwrapping container matchers - by [@​Copilot](https://github.com/Copilot), **sheremet-va**, **hi-ogawa** and [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9330](vitest-dev/vitest#9330) [<samp>(b2ec7)</samp>](vitest-dev/vitest@b2ec724a8) - Improve runner error when importing outside of test context - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9335](vitest-dev/vitest#9335) [<samp>(2dd3d)</samp>](vitest-dev/vitest@2dd3dd839) - Replace crypto.randomUUID to allow insecure environments (fix [#​9](https://github.com/vitest-dev/vitest/issues/9)… - by [@​plusgut](https://github.com/plusgut) in [#​9339](vitest-dev/vitest#9339) and [#​9](vitest-dev/vitest#9) [<samp>(e6a3f)</samp>](vitest-dev/vitest@e6a3f8cc7) - Handle null options in `addEventHandler` [#​9371](vitest-dev/vitest#9371) - by [@​ThibautMarechal](https://github.com/ThibautMarechal) in [#​9372](vitest-dev/vitest#9372) and [#​9371](vitest-dev/vitest#9371) [<samp>(40841)</samp>](vitest-dev/vitest@40841ff00) - Typo in browser.provider error - by [@​deammer](https://github.com/deammer) in [#​9394](vitest-dev/vitest#9394) [<samp>(4b67f)</samp>](vitest-dev/vitest@4b67fc25a) - **browser**: - Fix `process.env` and `import.meta.env` defines in inline project - by [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9239](vitest-dev/vitest#9239) [<samp>(b70c9)</samp>](vitest-dev/vitest@b70c96121) - Fix upload File instance - by [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9294](vitest-dev/vitest#9294) [<samp>(b6778)</samp>](vitest-dev/vitest@b67788c69) - Fix invalid project token for artifacts assets - by [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9321](vitest-dev/vitest#9321) [<samp>(caa7d)</samp>](vitest-dev/vitest@caa7d73d4) - Log `ErrorEvent.message` when unhandled `ErrorEvent.error` is null - by [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9322](vitest-dev/vitest#9322) [<samp>(5d84e)</samp>](vitest-dev/vitest@5d84eeb91) - Support `fileParallelism` on an instance - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9328](vitest-dev/vitest#9328) [<samp>(15006)</samp>](vitest-dev/vitest@150065459) - **coverage**: - Remove unnecessary `istanbul-lib-source-maps` usage - by [@​AriPerkkio](https://github.com/AriPerkkio) in [#​9344](vitest-dev/vitest#9344) [<samp>(b0940)</samp>](vitest-dev/vitest@b09405375) - Apply patch from [istanbuljs/istanbuljs#837](istanbuljs/istanbuljs#837) - by [@​AriPerkkio](https://github.com/AriPerkkio) and **sapphi-red** in [#​9413](vitest-dev/vitest#9413) and [#​837](vitest-dev/vitest#837) [<samp>(e05ce)</samp>](vitest-dev/vitest@e05cedbf4) - **fsModuleCache**: - Don't store importers in cache - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9422](vitest-dev/vitest#9422) [<samp>(75136)</samp>](vitest-dev/vitest@751364eec) - Add importers alongside importedModules - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9423](vitest-dev/vitest#9423) [<samp>(59f92)</samp>](vitest-dev/vitest@59f92d403) - **mocker**: - Fix mock transform with class - by [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9421](vitest-dev/vitest#9421) [<samp>(d390e)</samp>](vitest-dev/vitest@d390eb527) - **pool**: - Validate environment options when reusing the worker - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9349](vitest-dev/vitest#9349) [<samp>(a8a88)</samp>](vitest-dev/vitest@a8a8836e3) - Handle worker start failures gracefully - by [@​AriPerkkio](https://github.com/AriPerkkio) in [#​9337](vitest-dev/vitest#9337) [<samp>(200da)</samp>](vitest-dev/vitest@200dadb32) - **reporter**: - Report test module if it failed to run - by [@​sheremet-va](https://github.com/sheremet-va) in [#​9272](vitest-dev/vitest#9272) [<samp>(c7888)</samp>](vitest-dev/vitest@c78882985) - **runner**: - Respect nested test.only within describe.only - by [@​Ujjwaljain16](https://github.com/Ujjwaljain16) in [#​9021](vitest-dev/vitest#9021) and [#​9213](vitest-dev/vitest#9213) [<samp>(55d5d)</samp>](vitest-dev/vitest@55d5dad69) - **typecheck**: - Improve error message when tsc outputs help text - by [@​Ujjwaljain16](https://github.com/Ujjwaljain16) in [#​9214](vitest-dev/vitest#9214) [<samp>(7b10a)</samp>](vitest-dev/vitest@7b10ab4cd) - **ui**: - Detect gzip by magic numbers instead of Content-Type header in html reporter - by [@​Copilot](https://github.com/Copilot), **hi-ogawa** and [@​hi-ogawa](https://github.com/hi-ogawa) in [#​9278](vitest-dev/vitest#9278) [<samp>(dd033)</samp>](vitest-dev/vitest@dd0331632) - **webdriverio**: - Fall back to WebDriver Classic [#​9244](vitest-dev/vitest#9244) - by [@​JustasMonkev](https://github.com/JustasMonkev) in [#​9373](vitest-dev/vitest#9373) and [#​9244](vitest-dev/vitest#9244) [<samp>(c23dd)</samp>](vitest-dev/vitest@c23dd11bd) ##### [View changes on GitHub](vitest-dev/vitest@v4.0.16...v4.0.17) </details> --- ### Configuration 📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM, on day 1 of the month ( * 0-3 1 * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi45My4xIiwidXBkYXRlZEluVmVyIjoiNDIuOTMuMSIsInRhcmdldEJyYW5jaCI6ImZvcmdlam8iLCJsYWJlbHMiOlsiZGVwZW5kZW5jeS11cGdyYWRlIiwidGVzdC9ub3QtbmVlZGVkIl19--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11121 Reviewed-by: Gusted <[email protected]> Reviewed-by: Michael Kriese <[email protected]> Co-authored-by: Renovate Bot <[email protected]> Co-committed-by: Renovate Bot <[email protected]>
Description
Resolves #9043
Summary
This PR improves open telemetry integration for browser by adding following traces:
vitest.browser.run,vitest.browser.openvitest.browser.orchestrator.runvitest.browser.tester.run,vitest.browser.tester.command(+ enable existing test runner traces)The browser runtime side traces (orchestrator and tester) rely on SDK provided through
browserSdkPath. On browser runtime, async context doesn't work, so each test runner span aren't nested. However, main internal spans relationsvitest.browser.run -> vitest.browser.orchestrator.run -> vitest.browser.tester.run -> vitest.test.runner.xxxare nested by manually passing otel carrier and setting up root context in each browser runtime.TODO
pools/poolRunner.tstraces topools/browser.tsvitest.test.runner.test.callback)To explore:
Example
pnpm -C examples/opentelemetry/ compose up -d pnpm -C examples/opentelemetry/ test --experimental.openTelemetry.enabled --browser.enabled runScreenshot 📸
pnpm -C examples/opentelemetry/ test --experimental.openTelemetry.enabled --browser.enabled --browser.headless runScreenshot 📸
npm -C examples/opentelemetry/ test --experimental.openTelemetry.enabled --browser.enabled --browser.headless --isolate=false --maxWorkers=1 run(This looks weird because there are two
vitest.browser.orchestrator.runbut only onevitest.browser.tester.run, but it shows tester iframe is reused properly.)Screenshot 📸
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.