@@ -18,7 +18,7 @@ import { handleKeyboardEvent } from './coreHandlers/handleKeyboardEvent';
1818import { setupPerformanceObserver } from './coreHandlers/performanceObserver' ;
1919import { createEventBuffer } from './eventBuffer' ;
2020import { clearSession } from './session/clearSession' ;
21- import { getSession } from './session/getSession' ;
21+ import { loadOrCreateSession , maybeRefreshSession } from './session/getSession' ;
2222import { saveSession } from './session/saveSession' ;
2323import type {
2424 AddEventResult ,
@@ -228,28 +228,22 @@ export class ReplayContainer implements ReplayContainerInterface {
228228
229229 // Otherwise if there is _any_ sample rate set, try to load an existing
230230 // session, or create a new one.
231- const isSessionSampled = this . _loadAndCheckSession ( ) ;
232-
233- if ( ! isSessionSampled ) {
234- // This should only occur if `errorSampleRate` is 0 and was unsampled for
235- // session-based replay. In this case there is nothing to do.
236- return ;
237- }
231+ this . _initializeSessionForSampling ( ) ;
238232
239233 if ( ! this . session ) {
240234 // This should not happen, something wrong has occurred
241235 this . _handleException ( new Error ( 'Unable to initialize and create session' ) ) ;
242236 return ;
243237 }
244238
245- if ( this . session . sampled && this . session . sampled !== 'session' ) {
246- // If not sampled as session-based, then recording mode will be `buffer`
247- // Note that we don't explicitly check if `sampled === 'buffer'` because we
248- // could have sessions from Session storage that are still `error` from
249- // prior SDK version.
250- this . recordingMode = 'buffer' ;
239+ if ( this . session . sampled === false ) {
240+ // This should only occur if `errorSampleRate` is 0 and was unsampled for
241+ // session-based replay. In this case there is nothing to do.
242+ return ;
251243 }
252244
245+ this . recordingMode = this . session . sampled === 'buffer' ? 'buffer' : 'session' ;
246+
253247 logInfoNextTick (
254248 `[Replay] Starting replay in ${ this . recordingMode } mode` ,
255249 this . _options . _experiments . traceInternals ,
@@ -276,19 +270,20 @@ export class ReplayContainer implements ReplayContainerInterface {
276270
277271 logInfoNextTick ( '[Replay] Starting replay in session mode' , this . _options . _experiments . traceInternals ) ;
278272
279- const previousSessionId = this . session && this . session . id ;
280-
281- const { session } = getSession ( {
282- timeouts : this . timeouts ,
283- stickySession : Boolean ( this . _options . stickySession ) ,
284- currentSession : this . session ,
285- // This is intentional: create a new session-based replay when calling `start()`
286- sessionSampleRate : 1 ,
287- allowBuffering : false ,
288- traceInternals : this . _options . _experiments . traceInternals ,
289- } ) ;
273+ const session = loadOrCreateSession (
274+ this . session ,
275+ {
276+ timeouts : this . timeouts ,
277+ traceInternals : this . _options . _experiments . traceInternals ,
278+ } ,
279+ {
280+ stickySession : this . _options . stickySession ,
281+ // This is intentional: create a new session-based replay when calling `start()`
282+ sessionSampleRate : 1 ,
283+ allowBuffering : false ,
284+ } ,
285+ ) ;
290286
291- session . previousSessionId = previousSessionId ;
292287 this . session = session ;
293288
294289 this . _initializeRecording ( ) ;
@@ -305,18 +300,19 @@ export class ReplayContainer implements ReplayContainerInterface {
305300
306301 logInfoNextTick ( '[Replay] Starting replay in buffer mode' , this . _options . _experiments . traceInternals ) ;
307302
308- const previousSessionId = this . session && this . session . id ;
309-
310- const { session } = getSession ( {
311- timeouts : this . timeouts ,
312- stickySession : Boolean ( this . _options . stickySession ) ,
313- currentSession : this . session ,
314- sessionSampleRate : 0 ,
315- allowBuffering : true ,
316- traceInternals : this . _options . _experiments . traceInternals ,
317- } ) ;
303+ const session = loadOrCreateSession (
304+ this . session ,
305+ {
306+ timeouts : this . timeouts ,
307+ traceInternals : this . _options . _experiments . traceInternals ,
308+ } ,
309+ {
310+ stickySession : this . _options . stickySession ,
311+ sessionSampleRate : 0 ,
312+ allowBuffering : true ,
313+ } ,
314+ ) ;
318315
319- session . previousSessionId = previousSessionId ;
320316 this . session = session ;
321317
322318 this . recordingMode = 'buffer' ;
@@ -427,7 +423,7 @@ export class ReplayContainer implements ReplayContainerInterface {
427423 * new DOM checkout.`
428424 */
429425 public resume ( ) : void {
430- if ( ! this . _isPaused || ! this . _loadAndCheckSession ( ) ) {
426+ if ( ! this . _isPaused || ! this . _checkSession ( ) ) {
431427 return ;
432428 }
433429
@@ -535,7 +531,7 @@ export class ReplayContainer implements ReplayContainerInterface {
535531 if ( ! this . _stopRecording ) {
536532 // Create a new session, otherwise when the user action is flushed, it
537533 // will get rejected due to an expired session.
538- if ( ! this . _loadAndCheckSession ( ) ) {
534+ if ( ! this . _checkSession ( ) ) {
539535 return ;
540536 }
541537
@@ -634,7 +630,7 @@ export class ReplayContainer implements ReplayContainerInterface {
634630
635631 // --- There is recent user activity --- //
636632 // This will create a new session if expired, based on expiry length
637- if ( ! this . _loadAndCheckSession ( ) ) {
633+ if ( ! this . _checkSession ( ) ) {
638634 return ;
639635 }
640636
@@ -751,31 +747,63 @@ export class ReplayContainer implements ReplayContainerInterface {
751747
752748 /**
753749 * Loads (or refreshes) the current session.
750+ */
751+ private _initializeSessionForSampling ( ) : void {
752+ // Whenever there is _any_ error sample rate, we always allow buffering
753+ // Because we decide on sampling when an error occurs, we need to buffer at all times if sampling for errors
754+ const allowBuffering = this . _options . errorSampleRate > 0 ;
755+
756+ const session = loadOrCreateSession (
757+ this . session ,
758+ {
759+ timeouts : this . timeouts ,
760+ traceInternals : this . _options . _experiments . traceInternals ,
761+ } ,
762+ {
763+ stickySession : this . _options . stickySession ,
764+ sessionSampleRate : this . _options . sessionSampleRate ,
765+ allowBuffering,
766+ } ,
767+ ) ;
768+
769+ this . session = session ;
770+ }
771+
772+ /**
773+ * Checks and potentially refreshes the current session.
754774 * Returns false if session is not recorded.
755775 */
756- private _loadAndCheckSession ( ) : boolean {
757- const { type, session } = getSession ( {
758- timeouts : this . timeouts ,
759- stickySession : Boolean ( this . _options . stickySession ) ,
760- currentSession : this . session ,
761- sessionSampleRate : this . _options . sessionSampleRate ,
762- allowBuffering : this . _options . errorSampleRate > 0 || this . recordingMode === 'buffer' ,
763- traceInternals : this . _options . _experiments . traceInternals ,
764- } ) ;
776+ private _checkSession ( ) : boolean {
777+ // If there is no session yet, we do not want to refresh anything
778+ // This should generally not happen, but to be safe....
779+ if ( ! this . session ) {
780+ return false ;
781+ }
782+
783+ const currentSession = this . session ;
784+
785+ const newSession = maybeRefreshSession (
786+ currentSession ,
787+ {
788+ timeouts : this . timeouts ,
789+ traceInternals : this . _options . _experiments . traceInternals ,
790+ } ,
791+ {
792+ stickySession : Boolean ( this . _options . stickySession ) ,
793+ sessionSampleRate : this . _options . sessionSampleRate ,
794+ allowBuffering : this . _options . errorSampleRate > 0 ,
795+ } ,
796+ ) ;
797+
798+ const isNew = newSession . id !== currentSession . id ;
765799
766800 // If session was newly created (i.e. was not loaded from storage), then
767801 // enable flag to create the root replay
768- if ( type === 'new' ) {
802+ if ( isNew ) {
769803 this . setInitialState ( ) ;
804+ this . session = newSession ;
770805 }
771806
772- const currentSessionId = this . getSessionId ( ) ;
773- if ( session . id !== currentSessionId ) {
774- session . previousSessionId = currentSessionId ;
775- }
776-
777- this . session = session ;
778-
779807 if ( ! this . session . sampled ) {
780808 void this . stop ( { reason : 'session not refreshed' } ) ;
781809 return false ;
0 commit comments