Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not extend session from discarded session replay spans #939

Merged
merged 2 commits into from
Jan 27, 2025

Conversation

Joozty
Copy link
Member

@Joozty Joozty commented Jan 27, 2025

Description

This update resolves issues with the _experimental_allSpansExtendSession flag, which did not work correctly with the session recorder. Spans created via the session recorder improperly extended sessions in a backgrounded tab.

Issue:

  1. When the session recorder finishes a session in a backgrounded tab, it does not start a new session until the tab returns to the foreground.
  2. During this time, the session recorder emits events that are discarded. Before discarding, we call SplunkRum._internalOnExternalSpanCreated(), which results in either starting a new session or artificially extending the previous session.

The changes in this update ensure that the callback is invoked only after confirming that the events will not be discarded.

Possible Breaking Changes

The SplunkRum.getSessionID() method can return undefined between sessions. It is already typed that way (string | undefined) but the previous implementation would always return string unless agent is not initialized.

Previously, it would always return a string, even if the session was no longer active, by incorrectly referencing the previous session. With this change, if there is no activity and no active session, it will return undefined.
Spans initiate sessions, so if you call getSessionID() between sessions and there is no activity, it will correctly return undefined.

Type of change

Delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

Delete options that are not relevant.

  • Manual testing

@Joozty Joozty force-pushed the fix/session-long-tasks branch from 4da363c to 420bd23 Compare January 27, 2025 09:04
@@ -206,6 +204,13 @@ const SplunkRumRecorder = {
return
}

// We need to call it here before another getSessionID call. This will create a new session
// if the previous one was expired
if (SplunkRum._internalOnExternalSpanCreated) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The hook is called only when we are sure the session recorder chunk is not discarded.

if (hasNativeSessionId()) {
return window['SplunkRumNative'].getNativeSessionId()
}

return rumSessionId
return getCurrentSessionState({ useLocalStorage, forceStoreRead: true })?.id
Copy link
Member Author

Choose a reason for hiding this comment

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

When calling getRumSessionId we always read the fresh value from storage. Explained in the PR descriptions.

@Joozty Joozty self-assigned this Jan 27, 2025
@Joozty Joozty marked this pull request as ready for review January 27, 2025 10:08
@Joozty Joozty requested review from a team as code owners January 27, 2025 10:08
@@ -45,7 +45,6 @@ import { context } from '@opentelemetry/api'
with setting cookies, checking for inactivity, etc.
*/

let rumSessionId: SessionId | undefined
Copy link
Member Author

Choose a reason for hiding this comment

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

we no longer have this global variable

@Joozty Joozty changed the title fix: always read session metadata from storage fix: do not extend session from discarded session replay spans Jan 27, 2025
@Joozty Joozty requested review from potty, amertak and millcek January 27, 2025 11:11
Copy link
Contributor

@potty potty left a comment

Choose a reason for hiding this comment

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

👍

potty
potty approved these changes Jan 27, 2025
@Joozty Joozty merged commit 7fb034b into main Jan 27, 2025
10 checks passed
@Joozty Joozty deleted the fix/session-long-tasks branch January 27, 2025 12:58
@github-actions github-actions bot locked and limited conversation to collaborators Jan 27, 2025
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.

4 participants