11import { uuid4 } from '@sentry/utils' ;
22
3- import { SampleRates } from '../types' ;
43import { isSampled } from '../util/isSampled' ;
54
65type Sampled = false | 'session' | 'error' ;
76
8- interface SessionObject {
7+ export interface Session {
98 id : string ;
109
1110 /**
@@ -24,59 +23,41 @@ interface SessionObject {
2423 segmentId : number ;
2524
2625 /**
27- * Is the session sampled? `null` if the sampled, otherwise, `session` or `error`
26+ * The ID of the previous session.
27+ * If this is empty, there was no previous session.
2828 */
29- sampled : Sampled ;
30- }
31-
32- export class Session {
33- /**
34- * Session ID
35- */
36- public readonly id : string ;
37-
38- /**
39- * Start time of current session
40- */
41- public started : number ;
42-
43- /**
44- * Last known activity of the session
45- */
46- public lastActivity : number ;
47-
48- /**
49- * Sequence ID specific to replay updates
50- */
51- public segmentId : number ;
29+ previousSessionId ?: string ;
5230
5331 /**
54- * Previous session ID
32+ * Is the session sampled? `false` if not sampled, otherwise, `session` or `error`
5533 */
56- public previousSessionId : string | undefined ;
57-
58- /**
59- * Is the Session sampled?
60- */
61- public readonly sampled : Sampled ;
34+ sampled : Sampled ;
35+ }
6236
63- public constructor ( session : Partial < SessionObject > = { } , { sessionSampleRate, errorSampleRate } : SampleRates ) {
64- const now = new Date ( ) . getTime ( ) ;
65- this . id = session . id || uuid4 ( ) ;
66- this . started = session . started ?? now ;
67- this . lastActivity = session . lastActivity ?? now ;
68- this . segmentId = session . segmentId ?? 0 ;
69- this . sampled =
70- session . sampled ?? ( isSampled ( sessionSampleRate ) ? 'session' : isSampled ( errorSampleRate ) ? 'error' : false ) ;
71- }
37+ /**
38+ * Get a session with defaults & applied sampling.
39+ */
40+ export function makeSession ( session : Partial < Session > & { sampled : Sampled } ) : Session {
41+ const now = new Date ( ) . getTime ( ) ;
42+ const id = session . id || uuid4 ( ) ;
43+ // Note that this means we cannot set a started/lastActivity of `0`, but this should not be relevant outside of tests.
44+ const started = session . started || now ;
45+ const lastActivity = session . lastActivity || now ;
46+ const segmentId = session . segmentId || 0 ;
47+ const sampled = session . sampled ;
48+
49+ return {
50+ id,
51+ started,
52+ lastActivity,
53+ segmentId,
54+ sampled,
55+ } ;
56+ }
7257
73- toJSON ( ) : SessionObject {
74- return {
75- id : this . id ,
76- started : this . started ,
77- lastActivity : this . lastActivity ,
78- segmentId : this . segmentId ,
79- sampled : this . sampled ,
80- } as SessionObject ;
81- }
58+ /**
59+ * Get the sampled status for a session based on sample rates & current sampled status.
60+ */
61+ export function getSessionSampleType ( sessionSampleRate : number , errorSampleRate : number ) : Sampled {
62+ return isSampled ( sessionSampleRate ) ? 'session' : isSampled ( errorSampleRate ) ? 'error' : false ;
8263}
0 commit comments