Skip to content

⚗️ [PROF-16224] start early collection of errors and profiling before the SDK is loaded - #5192

Draft
thomasbertet wants to merge 2 commits into
mainfrom
thomas.bertet/PROF-16224-early-collection-errors-profiling
Draft

thomasbertet wants to merge 2 commits into
mainfrom
thomas.bertet/PROF-16224-early-collection-errors-profiling

Conversation

@thomasbertet

@thomasbertet thomasbertet commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The SDK only sees its data sources once its bundle is evaluated. startBufferingData() already buffers errors, fetch, XHR and console from bundle evaluation until init() (RUM-10145 #3622, extended by #4470/#4489), but everything between HTML parse and bundle evaluation stays dark, and the JS Self-Profiling Profiler only starts when the profiler chunk loads.

PROF-16224 RFC proposes an early collection package: a tiny script evaluated as the first script of the page. This implements its first slice: raw errors and profiling.

Changes

  • New package @datadog/browser-early-collection: standalone, dependency-free script (bundle datadog-early-collection.js). Evaluating it starts:
    • raw uncaught errors and unhandled rejections (original Error objects and messages: no stack parsing, no formatting at collection time), and
    • a JS Self-Profiling API Profiler session (sampleInterval: 10, maxBufferSize: 9000, fails silently when unsupported or when the Document-Policy: js-profiling header is missing).
    • Data is published on window._DD_EARLY_COLLECTION (version, startClocks, buffer capped at 500 entries, profiler, idempotent stop()).
    • npm surface is side-effect-free (explicit startEarlyCollection()); the auto-start entry lives in entries/main.ts, outside src/, because the disallow-side-effects rule scopes to packages/*/src.
  • Eval time: startBufferingData() skips the runtime-error instrumentation when the early global is present (the early listeners keep collecting); fetch, XHR and console are unchanged.
  • Init time: takeOverEarlyCollection() installs the SDK error instrumentation first, then drains the early buffer and processes entries with computeRawError (stack parsing moved to init time), replays them as RUM error events of the initial view, then seals the early collection and deletes the global.
  • Profiler: the profiler chunk adopts the early instance as its first periodic instance (first profile covers the early window, collection_type: 'early'), and profilerApi stops it on all not-profiling paths (no session, not sampled, unsupported browser, chunk load failure, SDK stop), following the pattern of open ✨ [PROF-16083] start profiler collection before the SDK and profiler chunk are loaded #5069.

Not included: Logs SDK and browser-rum-slim handoff (the early global is only reclaimed by the RUM SDK), setup UI / CDN distribution, console/fetch/XHR early collectors, sandbox dogfood injection. Tracked as open questions in the RFC.

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 earlyCollectionTakeover.ts (init ordering and draining) and the datadogProfiler.ts adoption wiring.

Test instructions

  • yarn workspace @datadog/browser-early-collection build produces packages/browser-early-collection/bundle/datadog-early-collection.js
  • yarn test:unit --spec packages/browser-early-collection/src/index.spec.ts
  • yarn test:unit --spec packages/browser-core/src/domain/earlyCollection/earlyCollection.spec.ts --spec packages/browser-core/src/domain/earlyCollection/earlyCollectionTakeover.spec.ts
  • yarn test:unit --spec packages/browser-core/src/domain/bufferedData.spec.ts --spec packages/browser-rum-core/src/domain/error/errorCollection.spec.ts
  • yarn test:unit --spec packages/browser-rum/src/boot/profilerApi.spec.ts --spec packages/browser-rum/src/domain/profiling/datadogProfiler.spec.ts
  • Dogfood: inject the built snippet as the first script of a page, load the SDK async, then check that early errors appear in the initial view and that the first profile covers the early window (collection_type: 'early').

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 188.93 KiB 190.79 KiB +1.86 KiB +0.99% ✅
Rum 188.91 KiB 190.77 KiB +1.86 KiB +0.99% ✅
Rum Salesforce Profiler 8.35 KiB 8.46 KiB +103 B +1.20% ✅
Rum Profiler 8.35 KiB 8.46 KiB +103 B +1.20% ✅
Rum Salesforce Recorder 32.04 KiB 32.04 KiB 0 B 0.00% ✅
Rum Recorder 32.04 KiB 32.04 KiB 0 B 0.00% ✅
Logs 59.37 KiB 59.54 KiB +177 B +0.29% ✅
Rum Slim 146.52 KiB 147.40 KiB +906 B +0.60% ✅
Worker 22.96 KiB 22.96 KiB 0 B 0.00% ✅
Rum Shopify 219.35 KiB 221.21 KiB +1.86 KiB +0.85% ✅
Rum Shopify Profiler 8.35 KiB 8.46 KiB +103 B +1.20% ✅
Rum Shopify Recorder 4.85 KiB 4.85 KiB 0 B 0.00% ✅
Early Collection N/A 552 B N/A N/A N/A

@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

✨ Unblock PR with BitsAI

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

DataDog/browser-sdk | format — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 76.24%
• Overall Coverage: 77.32% (+0.05%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1f3872a | Docs | View more details | Give us feedback!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant