@@ -18,7 +18,7 @@ import { handleKeyboardEvent } from './coreHandlers/handleKeyboardEvent';
18
18
import { setupPerformanceObserver } from './coreHandlers/performanceObserver' ;
19
19
import { createEventBuffer } from './eventBuffer' ;
20
20
import { clearSession } from './session/clearSession' ;
21
- import { getSession } from './session/getSession' ;
21
+ import { loadOrCreateSession , maybeRefreshSession } from './session/getSession' ;
22
22
import { saveSession } from './session/saveSession' ;
23
23
import type {
24
24
AddEventResult ,
@@ -228,28 +228,22 @@ export class ReplayContainer implements ReplayContainerInterface {
228
228
229
229
// Otherwise if there is _any_ sample rate set, try to load an existing
230
230
// 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 ( ) ;
238
232
239
233
if ( ! this . session ) {
240
234
// This should not happen, something wrong has occurred
241
235
this . _handleException ( new Error ( 'Unable to initialize and create session' ) ) ;
242
236
return ;
243
237
}
244
238
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 ;
251
243
}
252
244
245
+ this . recordingMode = this . session . sampled === 'buffer' ? 'buffer' : 'session' ;
246
+
253
247
logInfoNextTick (
254
248
`[Replay] Starting replay in ${ this . recordingMode } mode` ,
255
249
this . _options . _experiments . traceInternals ,
@@ -276,19 +270,20 @@ export class ReplayContainer implements ReplayContainerInterface {
276
270
277
271
logInfoNextTick ( '[Replay] Starting replay in session mode' , this . _options . _experiments . traceInternals ) ;
278
272
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
+ ) ;
290
286
291
- session . previousSessionId = previousSessionId ;
292
287
this . session = session ;
293
288
294
289
this . _initializeRecording ( ) ;
@@ -305,18 +300,19 @@ export class ReplayContainer implements ReplayContainerInterface {
305
300
306
301
logInfoNextTick ( '[Replay] Starting replay in buffer mode' , this . _options . _experiments . traceInternals ) ;
307
302
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
+ ) ;
318
315
319
- session . previousSessionId = previousSessionId ;
320
316
this . session = session ;
321
317
322
318
this . recordingMode = 'buffer' ;
@@ -427,7 +423,7 @@ export class ReplayContainer implements ReplayContainerInterface {
427
423
* new DOM checkout.`
428
424
*/
429
425
public resume ( ) : void {
430
- if ( ! this . _isPaused || ! this . _loadAndCheckSession ( ) ) {
426
+ if ( ! this . _isPaused || ! this . _checkSession ( ) ) {
431
427
return ;
432
428
}
433
429
@@ -535,7 +531,7 @@ export class ReplayContainer implements ReplayContainerInterface {
535
531
if ( ! this . _stopRecording ) {
536
532
// Create a new session, otherwise when the user action is flushed, it
537
533
// will get rejected due to an expired session.
538
- if ( ! this . _loadAndCheckSession ( ) ) {
534
+ if ( ! this . _checkSession ( ) ) {
539
535
return ;
540
536
}
541
537
@@ -634,7 +630,7 @@ export class ReplayContainer implements ReplayContainerInterface {
634
630
635
631
// --- There is recent user activity --- //
636
632
// This will create a new session if expired, based on expiry length
637
- if ( ! this . _loadAndCheckSession ( ) ) {
633
+ if ( ! this . _checkSession ( ) ) {
638
634
return ;
639
635
}
640
636
@@ -751,31 +747,63 @@ export class ReplayContainer implements ReplayContainerInterface {
751
747
752
748
/**
753
749
* 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.
754
774
* Returns false if session is not recorded.
755
775
*/
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 ;
765
799
766
800
// If session was newly created (i.e. was not loaded from storage), then
767
801
// enable flag to create the root replay
768
- if ( type === 'new' ) {
802
+ if ( isNew ) {
769
803
this . setInitialState ( ) ;
804
+ this . session = newSession ;
770
805
}
771
806
772
- const currentSessionId = this . getSessionId ( ) ;
773
- if ( session . id !== currentSessionId ) {
774
- session . previousSessionId = currentSessionId ;
775
- }
776
-
777
- this . session = session ;
778
-
779
807
if ( ! this . session . sampled ) {
780
808
void this . stop ( { reason : 'session not refreshed' } ) ;
781
809
return false ;
0 commit comments