Skip to content

♻️ Refactor startSessionManager to return a Promise instead of using a callback#4438

Open
thomas-lebeau wants to merge 3 commits intov7from
thomas.lebeau/promise-session-manager
Open

♻️ Refactor startSessionManager to return a Promise instead of using a callback#4438
thomas-lebeau wants to merge 3 commits intov7from
thomas.lebeau/promise-session-manager

Conversation

@thomas-lebeau
Copy link
Copy Markdown
Collaborator

Motivation

The startSessionManager function used a callback pattern (onReady) to signal when the session manager was initialized. This is unnecessary complexity since the function already operates asynchronously -- a Promise-based API is more idiomatic and easier to compose.

Changes

  • Change startSessionManager to return Promise<SessionManager | undefined> instead of accepting an onReady callback
  • Returns undefined when no session store strategy is configured (instead of never calling the callback)
  • Update preStartLogs and preStartRum to await the session manager instead of using callbacks
  • Update all related tests to use the new Promise-based API

Test instructions

  • yarn test:unit --spec packages/core/src/domain/session/sessionManager.spec.ts
  • yarn test:unit --spec packages/logs/src/boot/preStartLogs.spec.ts
  • yarn test:unit --spec packages/rum-core/src/boot/preStartRum.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

… callback

- Replace `onReady` callback with async/await pattern, making the API
  more idiomatic and easier to compose
- startSessionManager now returns Promise<SessionManager | undefined>
- startSessionManagerStub returns Promise<SessionManager>
- Update preStartLogs, preStartRum, and all related tests accordingly
@thomas-lebeau thomas-lebeau changed the title 🎨 Refactor startSessionManager to return a Promise instead of using a callback ♻️ Refactor startSessionManager to return a Promise instead of using a callback Apr 2, 2026
@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da bot commented Apr 2, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 172.34 KiB 172.38 KiB +34 B +0.02%
Rum Profiler 6.19 KiB 6.19 KiB 0 B 0.00%
Rum Recorder 27.23 KiB 27.23 KiB 0 B 0.00%
Logs 55.55 KiB 55.57 KiB +26 B +0.05%
Rum Slim 129.83 KiB 129.84 KiB +13 B +0.01%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
addglobalcontext N/A 0.0042 N/A
addaction N/A 0.0132 N/A
adderror N/A 0.0125 N/A
addtiming N/A 0.0025 N/A
startview N/A 0.0123 N/A
startstopsessionreplayrecording N/A 0.0007 N/A
logmessage N/A 0.0149 N/A
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
addglobalcontext N/A 33.81 KiB N/A
addaction N/A 265.99 KiB N/A
addtiming N/A 37.01 KiB N/A
adderror N/A 274.96 KiB N/A
startstopsessionreplayrecording N/A 36.06 KiB N/A
startview N/A 617.30 KiB N/A
logmessage N/A 243.67 KiB N/A

🔗 RealWorld

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

datadog-prod-us1-6 bot commented Apr 2, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 81.82%
Overall Coverage: 77.86% (+0.01%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b5201b8 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@thomas-lebeau thomas-lebeau marked this pull request as ready for review April 2, 2026 07:03
@thomas-lebeau thomas-lebeau requested a review from a team as a code owner April 2, 2026 07: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: 5e39b36d0a

ℹ️ 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".

expire()
return
}
const initialState = await resolveInitialState()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Catch session init rejection before resolving manager

startSessionManager now awaits resolveInitialState() directly, so any rejection from strategy.setSessionState (for example storage write failures or lock acquisition failures) rejects the returned promise. The current callers in preStartLogs/preStartRum only attach .then(...), so this turns into an unhandled promise rejection and prevents SDK startup in those error paths; previously this path was guarded by monitorError instead of surfacing globally.

Useful? React with 👍 / 👎.

- Catch `resolveInitialState()` rejection so `startSessionManager` resolves
  with `undefined` instead of propagating the error
- Add test for storage failure scenario
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: replace ?. with !. or maybe const sessionManager = (await ...)!.

All those ?. are making tests less precise, because in cases like this:

expect(sessionManager?.findSession()!.anonymousId).toBeUndefined()

the assertion can pass when sessionManager is undefined, but it shouldn't.

- Tighten startSessionManagerWithDefaults return type to Promise<SessionManager>
- Remove ?. operators on sessionManager since it's now guaranteed non-undefined
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.

2 participants