Repository navigation
⚗️ [PROF-16224] start early profiling before the SDK is loaded - #5192
thomasbertet wants to merge 15 commits into
Conversation
… the SDK is loaded
Bundles Sizes Evolution
|
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 1 Pipeline job failed
ℹ️ InfoNo other issues found (see more)🧪 All tests passed 🎯 Code Coverage (details) Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: e0e7a0f | Docs | View more details | Give us feedback! |
…ded takeover, slimmer validation
… collection global
There was a problem hiding this comment.
An early profiler that fills before RUM starts can be adopted without resuming sampling, creating up to a minute of additional missing data. The standalone bundle is also absent from the CDN deployment list.
🤖 Bits Code Review · Commit dce7158
There was a problem hiding this comment.
The PR adds pre-SDK profiling and handoff to RUM, but package version 7.16.2 fails validation against release 7.18.0. Both earlier issues remain unfixed: exhausted early buffers can leave sampling idle for another minute, and the standalone bundle is missing from CDN deployment registration.
🤖 Bits Code Review · Commit 7b3e9ac
The early collection snippet now records a bufferFull flag via its own samplebufferfull listener. When the SDK adopts an instance whose buffer filled before load, the profiler chunk collects it immediately and starts a fresh instance instead of waiting for the first collection interval, preserving the early samples.
Register browser-early-collection in the deploy and source map upload package lists so the 345 B snippet ships to the CDN (prod, datacenters, staging, canary and pull-request previews) with cache invalidation.
There was a problem hiding this comment.
Pre-SDK profiling now handles exhausted buffers and CDN deployment, and the package version matches the release. Immediate rollover introduces a quota race: an early profile can be uploaded before a pending quota check denies the session.
🤖 Bits Code Review · Commit 96e7f97
…rs-profiling Conflict in datadogProfiler.ts resolved: main's RUM-20021 refactor moved profile assembly (histories, buildProfileEvent) out of the orchestrator into contexts/ and profileAssembly.ts; the early-collection adoption branch (adopted Profiler instance, ClocksState, bufferFull immediate rollover) is kept on main's structure.
…ontract Reverts the bufferFull contract field and the immediate rollover of an exhausted early Profiler. Buffer exhaustion stays undetected at adoption: the exhausted instance is adopted as running and the normal collection interval does the rollover. The limitation is now documented explicitly at the adoption site: in the rare case where the SDK evaluates more than ~90 s after page load, the buffered samples are preserved but no new sample is taken until adoption + collectIntervalMs.
There was a problem hiding this comment.
More details
The PR starts profiling before SDK load and hands the instance to RUM, with CDN and package-version fixes retained. Removing immediate rollover closes the reported quota race; it explicitly accepts up to another minute without new samples when the early buffer has already filled.
🤖 Bits Code Review · Commit 5e313b6
Per review feedback on PR #5192: the profiling context no longer carries collection_type 'standard' for every profiled session. The field is now optional in ProfilingInternalContextSchema and set to 'early' only while the Profiler instance started by the early collection package is running; it is absent for instances the SDK starts itself, so only early collection is reported in _dd.profiling.
There was a problem hiding this comment.
Pre-SDK profiling retains the CDN/version fixes and avoids the immediate-rollover quota race, while explicitly accepting delayed sampling after buffer exhaustion. The new early-only collection tag is added directly to generated types without updating the pinned source schema, so schema consistency CI will fail.
🤖 Bits Code Review · Commit 15aa5a5
| * first collected profile covers the early collection period, from snippet evaluation to SDK takeover. | ||
| * The attribute is absent when the SDK started the Profiler instance itself. | ||
| */ | ||
| readonly collection_type?: 'early' |
There was a problem hiding this comment.
Generate the new property from the source schema
The pinned rum-events-format source schema lacks collection_type. CI regenerates this file in check-schemas, which will remove this manually added property and fail the consistency check. Add the property to the source schema, update the pinned dependency, and regenerate the types.
React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it
What each reaction means
- 👍 Useful: correct, and you're fixing it or will.
- 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
- 😕 Unclear: you can't tell what it means or why it matters.
- 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.
🤖 Bits Code Review · Open Bits AI session
Per review feedback on PR #5192: a shared layout plus a page template, or a tag manager, can evaluate the script twice. The second evaluation now returns early when window._DD_EARLY_COLLECTION is already published: overwriting the global would orphan the first running Profiler instance, which keeps sampling until its buffer fills and nothing can stop it. The earliest session wins, as it captures the most of the pre-SDK window.
mormubis
left a comment
There was a problem hiding this comment.
LGTM! I've approved the rum events format too!
Motivation
The JS Self-Profiling Profiler only starts when the profiler chunk loads, so everything between HTML parse and chunk load is invisible in profiles: typically the most CPU-intensive window of the page (bundle evaluation, hydration, first paint). Staging measurements in PROF-16083 showed the early snippet covering 74% of the page-load window vs 32% for an SDK-internal early start.
PROF-16224 RFC proposes an early collection package: a tiny script evaluated as the first script of the page. This implements its first slice: profiling only. Other RUM events collectors (raw errors, fetch/XHR, console) may be added to the same mechanism in later iterations, behind the same
window._DD_EARLY_COLLECTIONcontract.Changes
@datadog/browser-early-collection: a standalone, dependency-free script (bundledatadog-early-collection.js, 257 B min / 257 B gz). Evaluating it starts a JS Self-Profiling API Profiler session (sampleInterval: 10,maxBufferSize: 9000; fails silently when the API is unsupported or theDocument-Policy: js-profilingresponse header is missing) and publisheswindow._DD_EARLY_COLLECTION = { version, startClocks, profiler }. No API, no config, no network. The npm surface is side-effect-free (explicitstartEarlyCollection()); the auto-start entry lives inentries/main.ts, outsidesrc/, because thedisallow-side-effectsrule scopes topackages/*/src.earlyCollectionProfiler.ts):collection_type: 'early'in the profiling context; the instance is stopped by the normal periodic lifecycle at its first rollover). Already-stopped instances are skipped, and the global is deleted on claim so it can be taken only once.profilerApistops the early profiler on all not-profiling paths (no session, not sampled, unsupported browser, chunk load failure or throw, SDK stop). These paths run before the chunk is ever requested, so no race is possible.datadog-logs.jsanddatadog-rum-slim.jsship zero early-collection bytes (byte-identical builds vs main).119e0c72(min / gzip -9):datadog-rum.js+449 B / +142 B, profiler chunk +103 B,datadog-logs.jsanddatadog-rum-slim.js+0, snippet 257 B new.Note for reviewers: this implementation was written by an AI coding agent and reviewed by a human; the files most worth a careful pass are
earlyCollectionProfiler.ts(the claim/stop helper) and thedatadogProfiler.tsadoption wiring.Test instructions
yarn workspace @datadog/browser-early-collection buildproducespackages/browser-early-collection/bundle/datadog-early-collection.jsyarn test:unit --spec packages/browser-early-collection/src/index.spec.tsyarn test:unit --spec packages/browser-rum/src/domain/profiling/earlyCollectionProfiler.spec.tsyarn test:unit --spec packages/browser-rum/src/boot/profilerApi.spec.ts --spec packages/browser-rum/src/domain/profiling/datadogProfiler.spec.tsDocument-Policy: js-profilingheader), load the SDK async, and check that the first profile covers the early window withcollection_type: 'early'.Checklist