♻️ Simplify session manager architecture#4144
Draft
thomas-lebeau wants to merge 8 commits intothomas.lebeau/asyc-session-manager-with-telemetry-merge-mainfrom
Draft
♻️ Simplify session manager architecture#4144thomas-lebeau wants to merge 8 commits intothomas.lebeau/asyc-session-manager-with-telemetry-merge-mainfrom
thomas-lebeau wants to merge 8 commits intothomas.lebeau/asyc-session-manager-with-telemetry-merge-mainfrom
Conversation
- Add createCookieObservable to core/browser/storageObservable.ts - Add createLocalStorageObservable using window storage event for cross-tab sync - Export from core index.ts
- Delete cookieObservable.ts and cookieObservable.spec.ts from rum-core - Update ciVisibilityContext imports to use @datadog/browser-core
- Use storage observables for instant cross-tab notification - Cookie persistence uses CookieStore API (with polling fallback) - LocalStorage uses window storage event - Keep polling as fallback for same-tab changes
Simplify session store operations by replacing the complex cookie-based lock mechanism with the native Web Locks API. This removes ~60 lines of retry/timeout logic while providing better cross-tab synchronization. Key changes: - Use navigator.locks.request() for exclusive access - Queue operations when locks are enabled, process sequentially - Fall back to synchronous execution when locks unavailable - Strip legacy 'lock' field from sessions for backwards compatibility
Simplify session management by removing the intermediate SessionContext type and having products (RUM, Logs) work directly with SessionState. Key changes: - Remove SessionContext interface and buildSessionContext() function - Rename findSession() -> findSessionState() returning SessionState - SessionManager no longer generic (no TrackingType parameter) - RumSessionManager/LogsSessionManager access tracking type via productKey - Session history entries updated in place when sessionState changes - Add Web Locks mocks to rumSessionManager/logsSessionManager tests
Bundles Sizes Evolution
🚀 CPU Performance
🧠 Memory Performance
|
|
Deduplicate the identical ~20-line mock that was copy-pasted across sessionManager, logsSessionManager, and rumSessionManager spec files into a single reusable helper in core/test/emulate/mockWebLocks.ts.
The operationQueue/isProcessing/processNextOperation pattern manually reimplements what navigator.locks.request with exclusive mode already provides natively — requests are automatically queued by the browser. Replace with a direct navigator.locks.request call and simplify tests by using the shared mockWebLocksForSyncExecution helper, removing the polling-based waitForAfterCallback.
- Extract computeSessionReplayState helper to replace nested ternary - Inline SESSION_CONTEXT_TIMEOUT_DELAY (stale alias for SESSION_TIME_OUT_DELAY) - Fix comment referencing removed "cookie lock" → "Web Lock" - Remove unnecessary Operations export from sessionStoreOperations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The session manager has accumulated complexity over time that makes it harder to maintain and debug. This PR simplifies the architecture by:
Changes
Architecture Changes
flowchart TB subgraph Before["Before"] direction TB SS1[SessionState] --> |"buildSessionContext()"| SC1[SessionContext] SC1 --> |"findSession()"| RSM1[RumSession] Poll1[("⏱️ 1s polling")] --> |"watchSession()"| SS1 Lock1[("🔒 Cookie lock\n100 retries × 10ms")] --> SS1 end subgraph After["After"] direction TB SS2[SessionState] --> |"findSessionState()"| RSM2[RumSession] Events[("📡 Storage events\nCookieStore / localStorage")] --> |"instant sync"| SS2 Lock2[("🔒 Web Locks API\nnative mutex")] --> SS2 endKey Changes
SessionState→SessionContext<T>→RumSessionSessionState→RumSessionSessionManager<TrackingType>SessionManagerFiles Modified
Core package:
storageObservable.ts- New file with cookie and localStorage observables (moved from rum-core)sessionStore.ts- Added event-driven sync subscriptionssessionStoreOperations.ts- Replaced custom lock with Web Locks APIsessionManager.ts- Removed SessionContext, simplified to expose SessionState directlyRUM/Logs packages:
rumSessionManager.ts- Updated to work directly with SessionStatelogsSessionManager.ts- Updated to work directly with SessionStateTest instructions
yarn test:unityarn test:e2estopSession()propagates to all tabsChecklist