Skip to content

♻️ Make session manager injectable and isolate for v7 rewrite#4157

Merged
thomas-lebeau merged 10 commits intov7from
thomas.lebeau/v7-session-manager-from-scratch-clean
Feb 17, 2026
Merged

♻️ Make session manager injectable and isolate for v7 rewrite#4157
thomas-lebeau merged 10 commits intov7from
thomas.lebeau/v7-session-manager-from-scratch-clean

Conversation

@thomas-lebeau
Copy link
Copy Markdown
Collaborator

@thomas-lebeau thomas-lebeau commented Feb 10, 2026

Motivation

Prepare the session manager layer for a from-scratch rewrite in v7. The existing session manager internals (cookies, storage, core session store) are tightly coupled to boot-level tests, making it hard to replace the implementation without breaking unrelated tests.

This PR decouples all non-session-manager code from session internals, so the session manager can be rewritten in isolation.

Changes

1. Injectable session manager factory

Move the stub-vs-real session manager branching from createPreStartStrategy to the caller (rumPublicApi / logsPublicApi) via a factory parameter:

graph LR
    A[rumPublicApi / logsPublicApi] -->|ternary: bridge ? stub : real| B[factory]
    B -->|passed as param| C[createPreStartStrategy]
    C -->|calls factory| D[session manager instance]
Loading
  • BREAKING CHANGE: When no storage strategy is available (cookies/localStorage unavailable), neither SDK will send data. Previously Logs would still start without storage — now it requires explicit sessionPersistence: 'memory' to work without storage. In worker environments, Logs uses the real session manager with memory persistence (we can revisit that when we star using CookieStore as this seems supported in service workers). RUM is explicitly blocked with a warning.
  • createPreStartStrategy receives one factory, just calls it — no branching logic
  • Callers use: canUseEventBridge() ? stub : real

2. Prove isolation via gut-then-restore

To verify that all non-session-manager tests use mocks (not real implementations), the session manager bodies were temporarily gutted and their unit tests skipped (xdescribe). All 3071 remaining tests passed with zero failures, proving the isolation is complete. The implementations and tests were then fully restored.

3. Mock infrastructure

  • Boot tests (preStartRum.spec, preStartLogs.spec) inject mock factories — no more stopSessionManager() cleanup or cookie manipulation
  • Public API tests (rumPublicApi.spec, logsPublicApi.spec) inject mock session managers via optional parameter on makeRumPublicApi / makeLogsPublicApi
  • mockRumSessionManager / mockLogsSessionManager updated with factory wrappers

4. Worker environment support

Use a defense-in-depth strategy for worker environments, handling workers at multiple layers:

  • Storage layer: selectSessionStoreStrategyType forces SessionPersistence.MEMORY in workers — cookies and localStorage are unavailable in worker contexts
  • Session manager: Skip DOM event tracking (trackActivity, trackVisibility, trackResume) in workers — no DOM events to listen to
  • RUM: Block entirely in preStartRum with an explicit warning — RUM features (DOM observation, user interactions) are not applicable in workers
  • BREAKING CHANGE: Logs Uses the real session manager (not a stub) — the storage and session manager layers handle the worker context, so Logs works normally with memory-backed sessions. this means that now logs will have a proper session Id

New exports from @datadog/browser-core: selectSessionStoreStrategyType, SessionStoreStrategyType
startSessionManager now takes sessionStoreStrategyType as a direct parameter.

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
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented Feb 10, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.85 KiB 169.95 KiB +109 B +0.06%
Rum Profiler 4.29 KiB 4.29 KiB 0 B 0.00%
Rum Recorder 24.71 KiB 24.71 KiB 0 B 0.00%
Logs 56.87 KiB 56.89 KiB +20 B +0.03%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 126.47 KiB 126.56 KiB +91 B +0.07%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.0047 0.0045 -4.26%
RUM - add action 0.0133 0.0134 +0.75%
RUM - add error 0.0147 0.0125 -14.97%
RUM - add timing 0.0035 0.0026 -25.71%
RUM - start view 0.0158 0.0121 -23.42%
RUM - start/stop session replay recording 0.001 0.0007 -30.00%
Logs - log message 0.0191 0.0127 -33.51%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 28.42 KiB 27.61 KiB -832 B
RUM - add action 103.20 KiB 91.88 KiB -11.32 KiB
RUM - add timing 26.37 KiB 26.92 KiB +556 B
RUM - add error 103.65 KiB 104.09 KiB +453 B
RUM - start/stop session replay recording 25.65 KiB 25.97 KiB +326 B
RUM - start view 474.72 KiB 461.18 KiB -13.54 KiB
Logs - log message 46.89 KiB 46.57 KiB -322 B

🔗 RealWorld

@datadog-datadog-prod-us1
Copy link
Copy Markdown

datadog-datadog-prod-us1 Bot commented Feb 10, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 77.78%
Overall Coverage: 77.32% (+0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 8e76c0c | Docs | Datadog PR Page | Was this helpful? Give us feedback!

@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/v7-session-manager-from-scratch-clean branch 3 times, most recently from b9b9897 to e356ec1 Compare February 13, 2026 17:02
@thomas-lebeau thomas-lebeau marked this pull request as ready for review February 13, 2026 17:03
@thomas-lebeau thomas-lebeau requested a review from a team as a code owner February 13, 2026 17:03
Copy link
Copy Markdown

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

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: e356ec178a

ℹ️ 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/logs/src/domain/logsSessionManager.ts
Comment thread packages/logs/src/boot/preStartLogs.ts Outdated
Move the stub-vs-real session manager branching from createPreStartStrategy to the caller (rumPublicApi/logsPublicApi) via a factory parameter. This decouples boot-level tests from session internals (cookies, storage).

- Unify stub signatures to callback pattern (config, consent, onReady)
- Accept startSessionManagerImpl in createPreStartStrategy
- Callers use ternary: canUseEventBridge() ? stub : real
- Boot tests inject mock factories, removing stopSessionManager cleanup
Gut the real session manager implementations (core, rum, logs) and skip their unit tests. This ensures all non-session-manager tests use mocks, isolating the session manager work for the upcoming rewrite.
The gutting confirmed all non-session-manager tests use mocks. Now restore the real implementations so the full test suite passes (3174 tests, 0 failures).
…iguration

Move the isWorkerEnvironment check from configuration (sessionStoreStrategyType) to the public APIs, using session manager stubs in worker environments.
  This allows the Logs SDK to load in worker environments by aligning worker handling with the event bridge pattern.
- Force memory persistence at the storage level for workers
- Block RUM entirely in workers with explicit warning
- Let Logs use the real session manager (storage handles memory)
- Skip DOM event tracking in session manager for workers
Mirror the RUM pattern: when no event bridge is available and no session
store strategy type exists, warn and bail out instead of crashing on
`sessionStoreStrategyType!` dereference.
@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/v7-session-manager-from-scratch-clean branch from e356ec1 to bbbd93c Compare February 16, 2026 14:08
- Remove isWorkerEnvironment check that forced the session manager stub
  in worker contexts; only canUseEventBridge() now triggers the stub
- Add session_id assertions to service worker E2E tests to verify
  workers produce proper session IDs with the real session manager
Comment thread packages/core/src/domain/session/sessionStore.ts Outdated
Comment thread packages/logs/src/boot/preStartLogs.ts Outdated
Comment thread packages/logs/src/boot/preStartLogs.ts Outdated
…nager

- Guard against missing sessionStoreStrategyType directly in startSessionManager
  with an early return and warning, removing the non-null assertion
- Remove duplicate "no storage available" guards from preStartLogs and preStartRum
- Update tests accordingly
- Move the isWorkerEnvironment check from selectSessionStoreStrategyType
  into normalizePersistenceList so it only applies as a default when no
  explicit sessionPersistence is configured by the user
})
trackVisibility(configuration, () => sessionStore.expandSession())
trackResume(configuration, () => sessionStore.restartSession())
if (!isWorkerEnvironment) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Enabling the session manager to work on service workers seem to increase the complexity.

💬 suggestion: Should we move the trackActivity/trackVisibility/trackResume to an upper level, an call sessionManager.expandOrRenewSession?‏

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The complexity was already here, it has just been moved from preStartRum and preStartLogs to the session Manager (we were using a stub session Manager instead of the real one)

Moving that back one level up will risk duplicating the logic (LOGS vs RUM, even though RUM does not work in worker env). I think it's fine, so far it's just one if statement but we will need to keep an eye on this.

In v8, is we manage to reconcile session manager and only have one instance, we might want to do differently then.

I will keep note on this though, maybe we can be smarter and guard by feature instead of isWorkerEnvironment (i.e. document is present,...)

@thomas-lebeau thomas-lebeau merged commit ae370c4 into v7 Feb 17, 2026
19 checks passed
@thomas-lebeau thomas-lebeau deleted the thomas.lebeau/v7-session-manager-from-scratch-clean branch February 17, 2026 11:55
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants