Skip to content

Commit 09c516a

Browse files
committed
πŸ”Š Add debug log on events sent after session expiration
1 parent e059b81 commit 09c516a

File tree

9 files changed

+52
-11
lines changed

9 files changed

+52
-11
lines changed

β€Žpackages/core/src/domain/session/sessionManager.tsβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface SessionContext<TrackingType extends string> extends Context {
3939
trackingType: TrackingType
4040
isReplayForced: boolean
4141
anonymousId: string | undefined
42+
expire: string | undefined
4243
}
4344

4445
export const VISIBILITY_CHECK_DELAY = ONE_MINUTE
@@ -115,6 +116,7 @@ export function startSessionManager<TrackingType extends string>(
115116
trackingType: SESSION_NOT_TRACKED as TrackingType,
116117
isReplayForced: false,
117118
anonymousId: undefined,
119+
expire: undefined,
118120
}
119121
}
120122

@@ -123,6 +125,7 @@ export function startSessionManager<TrackingType extends string>(
123125
trackingType: session[productKey] as TrackingType,
124126
isReplayForced: !!session.forcedReplay,
125127
anonymousId: session.anonymousId,
128+
expire: session.expire,
126129
}
127130
}
128131

β€Žpackages/rum-core/src/boot/startRum.spec.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function startRumStub(
7373
const hooks = createHooks()
7474
const viewHistory = startViewHistory(lifeCycle)
7575
const urlContexts = startUrlContexts(lifeCycle, hooks, locationChangeObservable, location)
76-
startSessionContext(hooks, sessionManager, noopRecorderApi, viewHistory)
76+
startSessionContext(hooks, sessionManager, noopRecorderApi, viewHistory, pageStateHistory)
7777

7878
const { stop: rumEventCollectionStop } = startRumEventCollection(
7979
lifeCycle,

β€Žpackages/rum-core/src/boot/startRum.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function startRum(
136136
const urlContexts = startUrlContexts(lifeCycle, hooks, locationChangeObservable, location)
137137
cleanupTasks.push(() => urlContexts.stop())
138138
const featureFlagContexts = startFeatureFlagContexts(lifeCycle, hooks, configuration)
139-
startSessionContext(hooks, session, recorderApi, viewHistory)
139+
startSessionContext(hooks, session, recorderApi, viewHistory, pageStateHistory)
140140
startConnectivityContext(hooks)
141141
startTrackingConsentContext(hooks, trackingConsentState)
142142
const globalContext = startGlobalContext(hooks, configuration, 'rum', true)

β€Žpackages/rum-core/src/domain/assembly.spec.tsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
mockRumConfiguration,
99
mockViewHistory,
1010
noopRecorderApi,
11+
mockPageStateHistory,
1112
} from '../../test'
1213
import type { RumEventDomainContext } from '../domainContext.types'
1314
import type { RawRumEvent } from '../rawRumEvent.types'
@@ -574,7 +575,7 @@ function setupAssemblyTestWithDefaults({
574575
const recorderApi = noopRecorderApi
575576
const viewHistory = { ...mockViewHistory(), findView: () => findView() }
576577
startGlobalContext(hooks, mockRumConfiguration(), 'rum', true)
577-
startSessionContext(hooks, rumSessionManager, recorderApi, viewHistory)
578+
startSessionContext(hooks, rumSessionManager, recorderApi, viewHistory, mockPageStateHistory())
578579
startRumAssembly(mockRumConfiguration(partialConfiguration), lifeCycle, hooks, reportErrorSpy)
579580

580581
registerCleanupTask(() => {

β€Žpackages/rum-core/src/domain/contexts/pageStateHistory.tsβ€Ž

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface PageStateEntry {
3939
export interface PageStateHistory {
4040
wasInPageStateDuringPeriod: (state: PageState, startTime: RelativeTime, duration: Duration) => boolean
4141
addPageState(nextPageState: PageState, startTime?: RelativeTime): void
42+
findPageStatesForPeriod: (startTime: RelativeTime, duration: Duration) => PageStateServerEntry[] | undefined
4243
stop: () => void
4344
}
4445

@@ -99,14 +100,18 @@ export function startPageStateHistory(
99100
return pageStateEntryHistory.findAll(startTime, duration).some((pageState) => pageState.state === state)
100101
}
101102

103+
function findPageStatesForPeriod(startTime: RelativeTime, duration: Duration) {
104+
const pageStateEntries = pageStateEntryHistory.findAll(startTime, duration)
105+
return processPageStates(pageStateEntries, startTime, maxPageStateEntriesSelectable)
106+
}
107+
102108
hooks.register(
103109
HookNames.Assemble,
104110
({ startTime, duration = 0 as Duration, eventType }): DefaultRumEventAttributes | SKIPPED => {
105111
if (eventType === RumEventType.VIEW) {
106-
const pageStates = pageStateEntryHistory.findAll(startTime, duration)
107112
return {
108113
type: eventType,
109-
_dd: { page_states: processPageStates(pageStates, startTime, maxPageStateEntriesSelectable) },
114+
_dd: { page_states: findPageStatesForPeriod(startTime, duration) },
110115
}
111116
}
112117

@@ -124,6 +129,7 @@ export function startPageStateHistory(
124129
return {
125130
wasInPageStateDuringPeriod,
126131
addPageState,
132+
findPageStatesForPeriod,
127133
stop: () => {
128134
stopEventListeners()
129135
pageStateEntryHistory.stop()

β€Žpackages/rum-core/src/domain/contexts/sessionContext.spec.tsβ€Ž

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { RelativeTime } from '@datadog/browser-core'
22
import { clocksNow, DISCARDED, HookNames } from '@datadog/browser-core'
3-
import type { RumSessionManagerMock } from '../../../test'
4-
import { createRumSessionManagerMock, noopRecorderApi } from '../../../test'
3+
import {
4+
type RumSessionManagerMock,
5+
mockPageStateHistory,
6+
createRumSessionManagerMock,
7+
noopRecorderApi,
8+
} from '../../../test'
59
import { SessionType } from '../rumSessionManager'
610
import type { DefaultRumEventAttributes, DefaultTelemetryEventAttributes, Hooks } from '../hooks'
711
import { createHooks } from '../hooks'
@@ -37,7 +41,7 @@ describe('session context', () => {
3741
getReplayStatsSpy = spyOn(recorderApi, 'getReplayStats')
3842
findViewSpy = spyOn(viewHistory, 'findView').and.returnValue(fakeView)
3943

40-
startSessionContext(hooks, sessionManager, recorderApi, viewHistory)
44+
startSessionContext(hooks, sessionManager, recorderApi, viewHistory, mockPageStateHistory())
4145
})
4246

4347
it('should set id and type', () => {

β€Žpackages/rum-core/src/domain/contexts/sessionContext.tsβ€Ž

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
import { DISCARDED, HookNames, SKIPPED } from '@datadog/browser-core'
2-
import { SessionReplayState, SessionType } from '../rumSessionManager'
1+
import type { ContextValue } from '@datadog/browser-core'
2+
import {
3+
DISCARDED,
4+
HookNames,
5+
SKIPPED,
6+
dateNow,
7+
ONE_MINUTE,
8+
addTelemetryDebug,
9+
elapsed,
10+
relativeNow,
11+
} from '@datadog/browser-core'
312
import type { RumSessionManager } from '../rumSessionManager'
13+
import { SessionReplayState, SessionType } from '../rumSessionManager'
414
import { RumEventType } from '../../rawRumEvent.types'
515
import type { RecorderApi } from '../../boot/rumPublicApi'
616
import type { DefaultRumEventAttributes, DefaultTelemetryEventAttributes, Hooks } from '../hooks'
717
import type { ViewHistory } from './viewHistory'
18+
import type { PageStateHistory } from './pageStateHistory'
819

920
export function startSessionContext(
1021
hooks: Hooks,
1122
sessionManager: RumSessionManager,
1223
recorderApi: RecorderApi,
13-
viewHistory: ViewHistory
24+
viewHistory: ViewHistory,
25+
pageStateHistory: PageStateHistory
1426
) {
1527
hooks.register(HookNames.Assemble, ({ eventType, startTime }): DefaultRumEventAttributes | DISCARDED => {
1628
const session = sessionManager.findTrackedSession(startTime)
@@ -19,6 +31,18 @@ export function startSessionContext(
1931
if (!session || !view) {
2032
return DISCARDED
2133
}
34+
if (session.expire && dateNow() - Number(session.expire) > ONE_MINUTE) {
35+
const duration = elapsed(startTime, relativeNow())
36+
// monitor-until: 2026-01-01
37+
addTelemetryDebug('Event sent after session expiration', {
38+
debug: {
39+
duration,
40+
eventType,
41+
expired_since: dateNow() - Number(session.expire),
42+
page_state: pageStateHistory.findPageStatesForPeriod(startTime, duration) as ContextValue,
43+
},
44+
})
45+
}
2246

2347
let hasReplay
2448
let sampledForReplay

β€Žpackages/rum-core/src/domain/rumSessionManager.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface RumSession {
3131
id: string
3232
sessionReplay: SessionReplayState
3333
anonymousId?: string
34+
expire?: string
3435
}
3536

3637
export const enum RumTrackingType {
@@ -88,6 +89,7 @@ export function startRumSessionManager(
8889
? SessionReplayState.FORCED
8990
: SessionReplayState.OFF,
9091
anonymousId: session.anonymousId,
92+
expire: session.expire,
9193
}
9294
},
9395
expire: sessionManager.expire,

β€Žpackages/rum-core/test/mockPageStateHistory.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function mockPageStateHistory(partialPageStateHistory?: Partial<PageState
55
const pageStateHistory: PageStateHistory = {
66
addPageState: noop,
77
stop: noop,
8+
findPageStatesForPeriod: () => [],
89
wasInPageStateDuringPeriod: () => false,
910
...partialPageStateHistory,
1011
}

0 commit comments

Comments
Β (0)