Repository navigation
feat(rum): add web worker profiling support as opt-in subpath plugin - #5369
Draft
thomasbertet wants to merge 10 commits into
Draft
thomasbertet wants to merge 10 commits into
thomasbertet wants to merge 10 commits into
Conversation
… and zero-dep worker entry
Bundles Sizes Evolution
|
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: a732bc7 | Docs | View more details | Give us feedback! |
…CDN bundles Build the worker-profiling subpath entries as standalone CDN bundles (datadog-rum-worker-profiling.js and datadog-rum-worker-profiling-worker.js) exposing a DD_RUM_WORKER_PROFILING global, since webpack drops exports from bundles without a library config and re-export-only entries built empty. - Main bundle exposes profileWorker, createWorkerProfilingPort and workerProfilingPlugin (auto-registering with DD_RUM on first use) - Worker bundle exposes startWorkerProfiling and stays dependency-free (no @datadog/js-core / @datadog/browser-core runtime imports) - Document the CDN usage in the README
Cover the full worker profiling matrix against real Chromium (M154+):
- network-loaded dedicated worker: main + worker profile events with
thread_type/thread_id/main_thread_id/parent_thread_id/worker_name tags,
page-clock-aligned timestamps, no main-thread long tasks in worker traces
- blob: worker inheriting the page Document-Policy
- nested dedicated workers via workerProfiler.profileWorker()
- console warning + no worker events when the worker script misses the
Document-Policy header
Infra: enable DocumentPolicyInDedicatedWorker,ProfilerAPIForDedicatedWorker
on the chromium project, serve the two new worker-profiling bundles from the
dev server and mock server, support createWorker({ dedicated, profiling,
documentPolicy }), and set Document-Policy: js-profiling on /sw.js.
Note: on current Chromium, classic workers honor the Document-Policy header
on the worker script response but module workers do not (fixed upstream by
CL 8804097, not yet in Chrome for Testing 159), so profiling workers are
served as classic workers.
Also fix the pre-existing main-thread profiling assertion that this branch
broke by adding thread tags to main-thread profile events.
Add the datadog-rum-worker-profiling[-worker] bundles to the expected rename, upload and CloudFront invalidation commands in the deploy and upload-source-maps specs.
Safari 14 (the BrowserStack unit matrix) empties the peer port's message queue when a MessagePort closes, so the final 'trace' and 'closed' messages posted by workerProfiler.close() right before closePort() are dropped and the test times out. Modern engines deliver queued messages even after the sender closes. Skip the test on Safari < 15 for now; keeping the port open for one macrotask before closing is a follow-up option to preserve the final trace on Safari 14.
Safari does not support the JS Self-Profiling API, and the profiler never activates there at runtime, so the profiling suites cannot exercise production behavior on Safari and only risk engine-specific flakes (e.g. Safari 14 dropping queued MessagePort messages on close). Wrap the top-level describes of all profiling specs (main thread, worker profiling, early collection) in a describeUnlessSafari helper so they run on Chromium and are reported as skipped on Safari/WebKit. E2E profiling scenarios already skip on non-chromium browsers.
Reconcile the Web Worker Profiling extension seam with the profiling state and lifecycle refactor from #5364: - keep main's simplified start/stop lifecycle (cleanupTasks, QuotaResult, no stateReason) - re-add the ProfilerExtension/ProfilerHost seam: mainThreadId shared with profileAssembly, attachExtension, onStart/onStop/onFlush hooks, and the stoppedByUser distinction for closePorts - stopProfiling() semantics: quota_ko discards extensions, stopped-by-user closes their ports, session expiry keeps them open for renewal
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
RUM Profiling shows what code runs in the browser. It samples the running functions at regular intervals. Today it only records the main thread.
Many apps do heavy work in Web Workers. Workers run on separate threads. The profiler does not see that work today.
This PR adds profiling for regular Web Workers (created with
new Worker(...)).@datadog/browser-rum. It does not change@datadog/browser-coreor@datadog/browser-rum-slim.It is an alternative to #5368. #5368 puts the feature on the RUM public API. This PR moves it into separate import paths. This keeps the main bundle smaller.
Refs: PROF-15093
Browser support (read before testing)
The browser support is narrow. This comes from the browser API, not from this PR.
self.Profilerin the worker.--enable-blink-features=DocumentPolicyInDedicatedWorker,ProfilerAPIForDedicatedWorker.Document-Policy: js-profilingon the worker script response.How to use it (npm)
onmessagehandler:Each worker sends its own profile events. Tags link them to the page:
thread_type:worker,thread_id,main_thread_id,parent_thread_id,worker_name. A worker created inside a worker also works. The inner worker reports directly to the page.CDN users load two extra script tags and call
DD_RUM_WORKER_PROFILING.profileWorker(worker)on the page, andself.DD_RUM_WORKER_PROFILING.startWorkerProfiling()in the worker. The README has the full example.Changes
How the pieces connect. White dashed nodes are the customer's code. Everything else is the SDK. The thick arrow is the only new thing crossing between page and worker:
%%{init: { "theme": "base", "themeVariables": { "fontSize": "14px", "lineColor": "#94a3b8" } } }%% flowchart LR subgraph PAGE["🖥 Page — main thread"] direction TB APP1("🧑 your page code") INIT["datadogRum.init()"] PW("profileWorker(worker)") HOOK["plugin hook<br>registerProfilerExtension"] COORD["worker profiler coordinator<br>thread tags · clock shift"] PIPE["profile pipeline<br>same as main-thread profiles"] APP1 --> INIT APP1 --> PW INIT --> HOOK PW -.->|"registers itself"| HOOK HOOK --> COORD COORD --> PIPE end subgraph WORKER["⚙ Worker — new Worker()"] direction TB APP2("🧑 your worker code") START["startWorkerProfiling()"] PROF["self.Profiler<br>10 ms samples"] APP2 --> START START --> PROF end PW ==>|"handshake + MessagePort"| START START <-->|"profiling data over the port"| COORD classDef app fill:#ffffff,stroke:#64748b,stroke-dasharray:5 5,color:#334155 classDef existing fill:#f1f5f9,stroke:#94a3b8,color:#334155 classDef new fill:#eff6ff,stroke:#60a5fa,color:#1e3a8a classDef optin fill:#fffbeb,stroke:#f59e0b,color:#92400e class APP1,APP2 app class INIT,PIPE existing class HOOK,COORD,START,PROF new class PW optin style PAGE fill:#f8fafc,stroke:#cbd5e1,color:#334155 style WORKER fill:#f0fdf4,stroke:#86efac,color:#14532dNew import paths (opt-in)
@datadog/browser-rum/worker-profilingexportsprofileWorker,createWorkerProfilingPortandworkerProfilingPlugin. It runs on the page.@datadog/browser-rum/worker-profiling/workerexportsstartWorkerProfiling. It runs inside the worker. It has zero runtime dependencies.datadog-rum-worker-profiling.js(9.8 KB, 4.0 KB gzipped) anddatadog-rum-worker-profiling-worker.js(6.2 KB, 2.3 KB gzipped). They expose theDD_RUM_WORKER_PROFILINGglobal.How it plugs into RUM
registerProfilerExtensioninpackages/browser-rum-core/src/domain/plugins.ts(plusstartRum.ts,preStartRum.ts,rumPublicApi.ts). The hook is optional and marked@internal.browser-rum-slimdoes not get it.datadogRum.init()runs, or when the app callsprofileWorker().What the worker side does
worker.terminate()directly.What we do not do
Worker,terminate,closeandonmessagestay untouched.Bundle size (measured against #5368, before the merge of
main)datadog-rum.js: -1,078 B raw / -408 B gzippeddatadog-rum-slim.js: -327 B raw / -98 B gzippedchunks/datadogProfiler-*.js: -4,322 B raw / -1,388 B gzipped (-32%)Merge of
mainmainrefactored the profiler lifecycle in #5364. This branch now builds on that structure. The extension hook sits on the new start/stop/flush flow.Known limitation
On Safari 14 the final worker trace can be lost when the app closes the worker with
workerProfiler.close(). Safari 14 drops queued port messages when a port closes. The SDK skips all profiling tests on Safari. Follow-up: keep the port open one tick longer before closing.Test instructions
Automated:
The last command runs 9 E2E tests. Four of them are new:
They run real Chromium with the two flags above.
Manual (optional, to see it yourself):
yarn test:e2e --ui -g "worker profiling".wall-time.jsontrace and thethread_typetags.Checklist