Skip to content

♻️ Move global monitor functions to js-core - #4986

Open
BenoitZugmeyer wants to merge 2 commits into
mainfrom
benoit/move-monitor-to-js-core
Open

♻️ Move global monitor functions to js-core#4986
BenoitZugmeyer wants to merge 2 commits into
mainfrom
benoit/move-monitor-to-js-core

Conversation

@BenoitZugmeyer

@BenoitZugmeyer BenoitZugmeyer commented Aug 26, 2026

Copy link
Copy Markdown
Member

Motivation

As more utilities move from browser-core to js-core (starting with httpRequest), several shared functions need access to the monitor error-collection helpers. These are used pervasively as bare globals — passed to addEventListener, setTimeout, Observable subscriptions, etc. — so they must remain globally available rather than threaded through every call site. Hosting the global monitor in js-core lets shared code there use it directly and unblocks further utility migrations.

When several SDKs share the same js-core module instance, only the first SDK to init receives internal-error telemetry from the global monitor (first-wins). This is a deliberate trade-off for simplicity over per-SDK attribution, which can be revisited in the future.

Alternative considered

I investigated how we could have one "Monitor" instance per SDK using js-core, and pass it implicitly to asynchronous functions via monitor wrappers. But I hit a road block, as there is no good way to apply this strategy to async functions: awaiting a promise creates a new microtask that cannot be wrapped in anyway. As I want to move forward on js-core, I chose the simple way, expose monitor global functions that anyone can use. SDKs can still create their own monitor functions if they want to.

Changes

  • Expose a global monitor from @datadog/js-core/monitor (monitor, callMonitored, monitored, monitorError) alongside the existing createMonitor factory.
  • Add startMonitorErrorCollection(cb) (first-wins, returns false if already claimed) and stopMonitorErrorCollection() (production detach path) to manage the error-collection sink.
  • Remove the global monitor implementation and its re-export from browser-core; all consumers (browser-core internals, browser-rum-core, browser-rum, browser-logs, and framework integrations) now import monitor helpers directly from @datadog/js-core/monitor.
  • Export monitorDisplay from @datadog/js-core/monitor so tests can spy on the monitor's debug output (the global monitor no longer shares a display instance with browser-core).
  • Document the multi-SDK first-wins limitation in the startMonitorErrorCollection JSDoc.

Test instructions

  • In the sandbox (yarn dev), load the SDK and trigger an internal error (e.g. misconfigure an endpoint so a monitored callback throws). Confirm the error is captured in telemetry and no uncaught exception surfaces to the page.
  • Enable debug mode (DD_RUM._setDebug(true) / DD_LOGS._setDebug(true)) and trigger a monitored error; confirm a [MONITOR] error is logged to the console with the Datadog SDK: prefix.
  • Load both RUM and Logs on the same page; confirm internal errors are attributed to the first SDK that initialised and the second does not hijack the sink.

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

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Aug 26, 2026

Copy link
Copy Markdown

Tests

All CI checks and tests passed.

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 90.91%
Overall Coverage: 77.13% (+0.10%)

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

@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/move-monitor-to-js-core branch from e8af479 to 7c4464d Compare August 26, 2026 09:53
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 26, 2026

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 181.52 KiB 181.56 KiB +42 B +0.02%
Rum Profiler 8.43 KiB 8.43 KiB -1 B -0.01%
Rum Recorder 22.31 KiB 22.31 KiB -1 B -0.00%
Logs 57.63 KiB 57.67 KiB +44 B +0.07%
Rum Salesforce N/A 139.64 KiB N/A N/A N/A
Rum Slim 139.59 KiB 139.63 KiB +44 B +0.03%
Worker 22.96 KiB 22.96 KiB 0 B 0.00%
Rum Shopify N/A 202.39 KiB N/A N/A N/A
Rum-shopify Profiler N/A 8.43 KiB N/A N/A N/A
Rum-shopify Recorder N/A 3.72 KiB N/A N/A N/A

@BenoitZugmeyer
BenoitZugmeyer marked this pull request as ready for review August 31, 2026 07:47
@BenoitZugmeyer
BenoitZugmeyer requested review from a team as code owners August 31, 2026 07:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-31T09:37:25.784241Z a644568 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c4464de8b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/js-core/src/entries/monitor.ts Outdated
Comment thread packages/js-core/src/entries/monitor.ts
Comment thread packages/js-core/src/entries/monitor.spec.ts Outdated
@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/move-monitor-to-js-core branch 2 times, most recently from e12d967 to 81ad825 Compare August 31, 2026 08:16

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 81ad825fbb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/js-core/src/entries/monitor.ts
Comment thread packages/js-core/src/entries/monitor.ts Outdated
@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/move-monitor-to-js-core branch from 81ad825 to 93233fa Compare August 31, 2026 08:56
@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/move-monitor-to-js-core branch from 93233fa to a644568 Compare August 31, 2026 09:31

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a644568205

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

*
* @returns void; replaces the method's descriptor value in place.
*/
export const monitored = globalMonitor.monitored

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Document the decorator parameters

issue: Add @param documentation for the decorator target, property name, and descriptor. This newly exported stable alias exposes parameters named _ and __, so consumers cannot infer what they represent, while the package’s non-negotiable export-documentation rule requires parameter descriptions when names are not self-explanatory.

AGENTS.md reference: packages/js-core/AGENTS.md:L24-L31

Useful? React with 👍 / 👎.

@sbarrio
sbarrio requested a review from bcaudan September 1, 2026 07:05
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