Skip to content

refactor(logs,core): Let the Logs contexts take RUM's sources - #5349

Draft
mormubis wants to merge 5 commits into
adlrb/RUM-19971-logs-session-sample-ratefrom
adlrb/RUM-19971-logs-rum-contexts
Draft

mormubis wants to merge 5 commits into
adlrb/RUM-19971-logs-session-sample-ratefrom
adlrb/RUM-19971-logs-rum-contexts

Conversation

@mormubis

@mormubis mormubis commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The Logs plugin from the RFC takes the user, the account, the global context, the correlation ids and the consent from RUM, instead of its own contexts, window.DD_RUM and its own consent. A log should look the same in both modes.

This is the third of 4 stacked PRs. It lets the contexts take those sources, with no change for datadogLogs.init() or for RUM.

Stacked on #5338.

Refs: RUM-19971

Changes

startUserContext() and startAccountContext() in core now call startUserContextHook() and startAccountContextHook(), which take a getter. The plugin will pass RUM's getUser() and getAccount(), with today's rules: anonymous_id from the session, empty contexts skipped, and an account needs an id.

startGlobalContext() gets the same split with startGlobalContextHook(). The plugin will pass RUM's getGlobalContext() with useContextNamespace set to false, so RUM's global context lands at the top level of the log, like Logs' own today.

The anonymous_id lookup passes configuration.sessionSampleRate, like the session context in #5338. RUM and Logs build their session manager from that same configuration, so their rate doesn't change.

startRUMInternalContext() takes the getInternalContext lookup. startLogs() passes getRUMInternalContextFromGlobal(), today's window.DD_RUM lookup, and the plugin will pass RUM's. startTrackingConsentContext() only needs isGranted(), so the plugin can pass isTrackingConsentGranted().

The mock from #5338 that samples at its own rate moves to core's test helpers, so the user context tests can use it too.

Bundle size: rum and rum_slim grow by 101 B (rum goes from 195.70 KiB to 195.80 KiB). logs grows by 47 B (60.41 KiB to 60.46 KiB).

Test instructions

The same page sends the same logs on #5338's branch and on this one, apart from ids and dates:

yarn dev-server start
cat > sandbox/test-logs-payload.html << 'EOF'
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Test Logs payload</title>
    <script src="/datadog-rum.js"></script>
    <script src="/datadog-logs.js"></script>
    <script>
      DD_RUM.init({ clientToken: 'xxx', applicationId: 'app-id', proxy: '/proxy' })
      DD_LOGS.init({ clientToken: 'xxx', service: 'manual-test', proxy: '/proxy', forwardConsoleLogs: ['warn'] })
      DD_LOGS.setUser({ id: 'user-id' })
      DD_LOGS.setAccount({ id: 'account-id' })
      DD_LOGS.setGlobalContext({ global: 'value' })
      DD_LOGS.logger.warn('from logger', { foo: 'bar' })
      console.warn('from console')
    </script>
  </head>
  <body></body>
</html>
EOF
yarn dev-server intake clear
agent-browser open http://localhost:8080/test-logs-payload.html
agent-browser tab new
yarn dev-server intake logs-events | jq -S -c 'del(.date, .session_id, .session.id, .tab.id) | if .usr then .usr.anonymous_id |= (. != null) else . end | if .view.id then .view.id = "<id>" else . end' | sort

Expected output, on both branches:

{"_dd":{},"account":{"id":"account-id"},"application_id":"app-id","ddtags":"sdk_version:dev,service:manual-test","foo":"bar","global":"value","message":"from logger","origin":"logger","service":"manual-test","session":{},"status":"warn","tab":{},"user_action":{"id":[]},"usr":{"anonymous_id":true,"id":"user-id"},"view":{"id":"<id>","referrer":"","url":"http://localhost:8080/test-logs-payload.html"}}
{"application_id":"app-id","ddtags":"sdk_version:dev,service:manual-test","message":"from console","origin":"console","service":"manual-test","session":{},"status":"warn","tab":{},"user_action":{"id":[]},"usr":{"anonymous_id":true},"view":{"id":"<id>","referrer":"","url":"http://localhost:8080/test-logs-payload.html"}}

Then yarn dev-server stop and rm sandbox/test-logs-payload.html.

Unit tests:

yarn test:unit packages/browser-core/src/domain/contexts packages/browser-logs/src/domain/contexts

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

@mormubis
mormubis added this pull request to stack #5324 November 27, 2026 15:19
@cit-pr-commenter-54b7da

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

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 195.72 KiB 195.82 KiB +101 B +0.05% ✅
Rum 195.70 KiB 195.80 KiB +101 B +0.05% ✅
Rum Salesforce Profiler 7.93 KiB 7.93 KiB 0 B 0.00% ✅
Rum Profiler 7.93 KiB 7.93 KiB 0 B 0.00% ✅
Rum Salesforce Recorder 32.80 KiB 32.80 KiB 0 B 0.00% ✅
Rum Recorder 32.80 KiB 32.80 KiB 0 B 0.00% ✅
Logs 60.41 KiB 60.46 KiB +47 B +0.08% ✅
Rum Slim 151.09 KiB 151.19 KiB +101 B +0.07% ✅
Worker 24.23 KiB 24.23 KiB 0 B 0.00% ✅
Rum Shopify 227.25 KiB 227.34 KiB +101 B +0.04% ✅
Rum Shopify Profiler 7.93 KiB 7.93 KiB 0 B 0.00% ✅
Rum Shopify Recorder 4.85 KiB 4.85 KiB 0 B 0.00% ✅
Early Collection 309 B 309 B 0 B 0.00% ✅

@datadog-official

datadog-official Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🔁 Datadog re-ran unit-bs: [edge] after it failed. The re-run passed. View in Datadog

🎯 Code Coverage (details)
• Patch Coverage: 94.12%
• Overall Coverage: 78.09% (+0.02%)

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

…wn rate

Core's user context tests need the same mock as Logs' session context tests. Moved as is, the sessionContext spec passes 6/6.
The Logs plugin reads the user and account from RUM's getUser() and getAccount(), with the same rules as today. The hooks move into startUserContextHook and startAccountContextHook, which startUserContext and startAccountContext call with their own contexts. The anonymous_id lookup passes the configured rate, which each SDK's own session manager already defaults to, so RUM and standalone Logs don't change. The 4 new tests failed first, and the rate test failed until the rate was passed.
…source

The Logs plugin reads correlation ids from RUM's getInternalContext() and consent from RUM's isTrackingConsentGranted(). startRUMInternalContext takes the lookup, and startLogs passes today's window.DD_RUM one. startTrackingConsentContext only needs isGranted(). The new test failed first, reading the global instead of the given lookup.
getInternalContextFromRumGlobal had one caller and only guarded an optional
call. getRUMInternalContextFromGlobal now ends with an optional call instead,
with the same behavior.

Proved by rumInternalContext.spec.ts (7/7), which goes red when the lookup
ignores DD_RUM_SYNTHETICS, plus the full unit suite (5605/5605), typecheck
and lint.
The Logs plugin reads the global context from RUM's getGlobalContext(), so customers set it once for both. The hook moves into startGlobalContextHook, which startGlobalContext calls with its own context, so RUM and standalone Logs don't change. The new test failed first.
@mormubis
mormubis force-pushed the adlrb/RUM-19971-logs-rum-contexts branch from 8856028 to d5d032b Compare December 3, 2026 16:08

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