1818
1919import { diag } from '@opentelemetry/api'
2020
21+ import { ExternalSessionMetadata } from '../../types/external-session-metadata'
2122import { generateId } from '../../utils'
2223import { isTrustedEvent } from '../../utils/is-trusted-event'
2324import { Observable } from '../../utils/observable'
@@ -64,16 +65,33 @@ export class SessionManager {
6465
6566 private stopCallbacks : Array < ( ) => void > = [ ]
6667
67- constructor ( private readonly storageManager : StorageManager ) {
68- const nativeSessionId = SessionManager . getNativeSessionId ( )
69- if ( nativeSessionId ) {
70- this . _session = {
71- expiresAt : Date . now ( ) + 4 * SESSION_INACTIVITY_TIMEOUT_MS ,
72- id : nativeSessionId ,
73- source : 'native' ,
74- startTime : Date . now ( ) ,
68+ constructor (
69+ private readonly storageManager : StorageManager ,
70+ externalSessionMetadata ?: NonNullable < ExternalSessionMetadata > ,
71+ ) {
72+ if ( externalSessionMetadata ) {
73+ const externalSessionInit : SessionState = {
74+ expiresAt : externalSessionMetadata . sessionLastActivity + SESSION_INACTIVITY_TIMEOUT_MS ,
75+ id : externalSessionMetadata . sessionId ,
76+ source : 'external' ,
77+ startTime : externalSessionMetadata . sessionStart ,
7578 state : 'active' ,
7679 }
80+
81+ if ( SessionManager . canContinueUsingSession ( externalSessionInit ) ) {
82+ this . _session = externalSessionInit
83+ diag . debug ( 'SessionManager: Initialized with provided external session metadata.' , {
84+ state : this . _session ,
85+ } )
86+ return
87+ }
88+ }
89+
90+ const externalSession = SessionManager . getExternalSession ( )
91+ if ( externalSession ) {
92+ this . _session = {
93+ ...externalSession ,
94+ }
7795 } else {
7896 const sessionState = this . getSessionStateFromStorageAndValidate ( )
7997 if ( sessionState ) {
@@ -90,23 +108,74 @@ export class SessionManager {
90108 diag . debug ( 'SessionManager: Initialized. Current session state' , { state : this . _session } )
91109 }
92110
93- static getNativeSessionId ( ) {
94- if ( ! ( typeof window !== 'undefined' && window . SplunkRumNative && window . SplunkRumNative . getNativeSessionId ) ) {
111+ static getExternalSession ( ) : SessionState | null {
112+ if ( typeof window === 'undefined' ) {
95113 return null
96114 }
97115
98- return window . SplunkRumNative . getNativeSessionId ( )
116+ if ( window . SplunkRumExternal && window . SplunkRumExternal . getSessionMetadata ) {
117+ const sessionMetadata = window . SplunkRumExternal . getSessionMetadata ( )
118+ const externalSession : SessionState = {
119+ expiresAt : sessionMetadata . sessionLastActivity + SESSION_INACTIVITY_TIMEOUT_MS ,
120+ id : sessionMetadata . sessionId ,
121+ source : 'external' ,
122+ startTime : sessionMetadata . sessionStart ,
123+ state : 'active' ,
124+ }
125+
126+ if ( SessionManager . canContinueUsingSession ( externalSession ) ) {
127+ return externalSession
128+ } else {
129+ diag . warn (
130+ 'Retrieved session from SplunkRumExternal, but it cannot be continued (it may be expired or have reached its maximum duration). Ignoring the external session.' ,
131+ )
132+ }
133+ } else if ( window . SplunkRumNative && window . SplunkRumNative . getNativeSessionId ) {
134+ const externalSessionId = window . SplunkRumNative . getNativeSessionId ( )
135+
136+ return {
137+ expiresAt : Date . now ( ) + SESSION_INACTIVITY_TIMEOUT_MS ,
138+ id : externalSessionId ,
139+ source : 'external' ,
140+ startTime : Date . now ( ) ,
141+ state : 'active' ,
142+ }
143+ }
144+
145+ return null
99146 }
100147
101- static hasNativeSessionId ( ) {
102- return Boolean (
103- typeof window !== 'undefined' && window . SplunkRumNative && window . SplunkRumNative . getNativeSessionId ,
148+ static hasExternalSession ( ) : boolean {
149+ if ( typeof window === 'undefined' ) {
150+ return false
151+ }
152+
153+ const isNativeSessionPresent = Boolean ( window . SplunkRumNative && window . SplunkRumNative . getNativeSessionId )
154+ const isExternalSessionPresent = Boolean (
155+ window . SplunkRumExternal && window . SplunkRumExternal . getSessionMetadata ( ) ,
104156 )
157+
158+ return isNativeSessionPresent || isExternalSessionPresent
105159 }
106160
107161 getSessionId ( ) {
108162 this . ensureSessionStateIsUpToDate ( )
109- return SessionManager . getNativeSessionId ( ) ?? this . session . id
163+ return SessionManager . getExternalSession ( ) ?. id ?? this . session . id
164+ }
165+
166+ getSessionMetadata ( ) : {
167+ sessionId : string
168+ sessionLastActivity : number
169+ sessionStart : number
170+ } | null {
171+ this . ensureSessionStateIsUpToDate ( )
172+ const session = this . session
173+
174+ return {
175+ sessionId : session . id ,
176+ sessionLastActivity : session . expiresAt - SESSION_INACTIVITY_TIMEOUT_MS ,
177+ sessionStart : session . startTime ,
178+ }
110179 }
111180
112181 getSessionState ( ) {
@@ -121,7 +190,7 @@ export class SessionManager {
121190 }
122191
123192 this . isStarted = true
124- if ( SessionManager . hasNativeSessionId ( ) ) {
193+ if ( SessionManager . hasExternalSession ( ) ) {
125194 this . attachNativeSessionWatch ( )
126195 } else {
127196 this . attachUserActivityListeners ( )
@@ -192,16 +261,13 @@ export class SessionManager {
192261 private attachNativeSessionWatch ( ) {
193262 // eslint-disable-next-line unicorn/consistent-function-scoping
194263 const nativeSessionWatch = ( ) => {
195- const nativeSessionId = SessionManager . getNativeSessionId ( )
264+ const externalSession = SessionManager . getExternalSession ( )
196265 const session = this . session
197266
198- if ( nativeSessionId ) {
267+ if ( externalSession ) {
199268 this . session = {
200269 ...session ,
201- expiresAt : Date . now ( ) + SESSION_INACTIVITY_TIMEOUT_MS ,
202- id : nativeSessionId ,
203- source : 'native' ,
204- state : 'active' ,
270+ ...externalSession ,
205271 }
206272 }
207273 }
@@ -285,7 +351,7 @@ export class SessionManager {
285351 return
286352 }
287353
288- if ( SessionManager . hasNativeSessionId ( ) ) {
354+ if ( SessionManager . hasExternalSession ( ) ) {
289355 diag . debug ( 'SessionManager: Native session ID detected. Session extension or creation is managed natively.' )
290356 return
291357 }
0 commit comments