Skip to content

refactor(logs): Use Logs' sample rate in the session context - #5338

Draft
mormubis wants to merge 2 commits into
adlrb/RUM-19971-split-start-logsfrom
adlrb/RUM-19971-logs-session-sample-rate
Draft

mormubis wants to merge 2 commits into
adlrb/RUM-19971-split-start-logsfrom
adlrb/RUM-19971-logs-session-sample-rate

Conversation

@mormubis

Copy link
Copy Markdown
Contributor

Motivation

The Logs plugin from the RFC uses RUM's session manager instead of starting its own. RUM's findTrackedSession() samples at RUM's sessionSampleRate by default, so logs would be sampled at RUM's rate instead of Logs'.

This is the second of 4 stacked PRs. It makes Logs pass its own rate, with no change for datadogLogs.init().

Stacked on #5320.

Refs: RUM-19971

Changes

startSessionContext() now only needs findTrackedSession, so it accepts RUM's read-only session manager from #5295. Both lookups pass configuration.sessionSampleRate: the one that adds session_id and the one that discards logs from sessions Logs doesn't sample.

Nothing changes for datadogLogs.init(). Its own session manager already defaults to that rate, and the event bridge stub ignores it, as today.

The two new tests use a mock of RUM's manager, with a session only Logs samples and one only RUM samples. Both fail without the change.

Test instructions

Standalone Logs still samples with its own rate. With sessionSampleRate: 0 the log is dropped, and with 100 it's sent with the session id:

yarn dev-server start
cat > sandbox/test-logs-sample-rate.html << 'EOF'
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Test Logs sample rate</title>
    <script src="/datadog-logs.js"></script>
    <script>
      const rate = Number(new URLSearchParams(location.search).get('rate'))
      DD_LOGS.init({ clientToken: 'xxx', service: 'manual-test', proxy: '/proxy', sessionSampleRate: rate })
      DD_LOGS.logger.info(`sampled at ${rate}`)
    </script>
  </head>
  <body></body>
</html>
EOF
yarn dev-server intake clear
agent-browser open 'http://localhost:8080/test-logs-sample-rate.html?rate=0'
agent-browser tab new
yarn dev-server intake logs-events | jq -c '{message, session_id: (.session_id != null), session: (.session.id != null)}'

Expected output: nothing.

yarn dev-server intake clear
agent-browser open 'http://localhost:8080/test-logs-sample-rate.html?rate=100'
agent-browser tab new
yarn dev-server intake logs-events | jq -c '{message, session_id: (.session_id != null), session: (.session.id != null)}'

Expected output:

{"message":"sampled at 100","session_id":true,"session":true}

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

Unit tests:

yarn test:unit packages/browser-logs/src/domain/contexts/sessionContext.spec.ts

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

In the Logs plugin, the session context gets RUM's session manager, which samples at RUM's rate by default. Both lookups now pass Logs' own rate. Standalone Logs doesn't change, because its own manager already defaults to that rate. The two new sessionContext tests failed before this change and pass with it.
The comment ended in "pass Logs' own", which read as unfinished. Comment only: the sessionContext spec passes 6/6, and Prettier and ESLint are clean on the file.
@mormubis
mormubis added this pull request to stack #5324 November 26, 2026 13:56
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 194.85 KiB 194.85 KiB 0 B 0.00% ✅
Rum 194.83 KiB 194.83 KiB 0 B 0.00% ✅
Rum Salesforce Profiler 8.10 KiB 8.10 KiB 0 B 0.00% ✅
Rum Profiler 8.10 KiB 8.10 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.39 KiB 60.44 KiB +56 B +0.09% ✅
Rum Slim 150.21 KiB 150.21 KiB 0 B 0.00% ✅
Worker 24.23 KiB 24.23 KiB 0 B 0.00% ✅
Rum Shopify 226.38 KiB 226.38 KiB 0 B 0.00% ✅
Rum Shopify Profiler 8.10 KiB 8.10 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 26, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 78.10% (+0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 82ab7cc | 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