♻️ Make session manager injectable and isolate for v7 rewrite#4157
♻️ Make session manager injectable and isolate for v7 rewrite#4157thomas-lebeau wants to merge 5 commits intov7from
Conversation
Bundles Sizes Evolution
🚀 CPU PerformancePending... 🧠 Memory PerformancePending... |
|
✅ Tests 🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: e356ec1 | Docs | Datadog PR Page | Was this helpful? Give us feedback! |
7ccc7fc to
c6ccf8d
Compare
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).
c6ccf8d to
b9b9897
Compare
…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
b9b9897 to
e356ec1
Compare
There was a problem hiding this comment.
💡 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".
| onReady: (sessionManager: LogsSessionManager) => void | ||
| ): void { | ||
| onReady({ | ||
| findTrackedSession: () => ({}), |
There was a problem hiding this comment.
Honor sessionSampleRate in bridge session stub
The bridge stub now always reports a tracked session via findTrackedSession: () => ({}), so Logs events are never discarded by startSessionContext even when sessionSampleRate is 0. Before this change, the stub used sampling logic and could return undefined for non-tracked sessions. In event-bridge/WebView integrations, this regresses sampling behavior and will forward logs that should be dropped.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch. Sampling decision should be handled by the other side of bridge (i.e. the mobile SDK), similarly than RUM
| }) | ||
| } else { | ||
| sessionManager = startLogsSessionManagerStub(configuration) | ||
| startLogsSessionManagerImpl(configuration, trackingConsentState, (newSessionManager) => { |
There was a problem hiding this comment.
Guard session manager start when no storage strategy exists
This path now always starts the logs session manager after consent, but configuration.sessionStoreStrategyType can still be undefined when cookies/localStorage are unavailable. In that case startSessionManager later dereferences configuration.sessionStoreStrategyType! and throws before tryStartLogs() can run, so initialization fails through a monitored exception instead of a controlled no-session behavior.
Useful? React with 👍 / 👎.
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
createPreStartStrategyto 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]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.createPreStartStrategyreceives one factory, just calls it — no branching logiccanUseEventBridge() ? stub : real2. 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
preStartRum.spec,preStartLogs.spec) inject mock factories — no morestopSessionManager()cleanup or cookie manipulationrumPublicApi.spec,logsPublicApi.spec) inject mock session managers via optional parameter onmakeRumPublicApi/makeLogsPublicApimockRumSessionManager/mockLogsSessionManagerupdated with factory wrappers4. Worker environment support
Use a defense-in-depth strategy for worker environments, handling workers at multiple layers:
selectSessionStoreStrategyTypeforcesSessionPersistence.MEMORYin workers — cookies and localStorage are unavailable in worker contextstrackActivity,trackVisibility,trackResume) in workers — no DOM events to listen topreStartRumwith an explicit warning — RUM features (DOM observation, user interactions) are not applicable in workersNew exports from
@datadog/browser-core:selectSessionStoreStrategyType,SessionStoreStrategyTypestartSessionManagernow takessessionStoreStrategyTypeas a direct parameter.Checklist