Skip to content

Commit 0afd75e

Browse files
committed
🔊 Add debug log on events sent after session expiration
1 parent e059b81 commit 0afd75e

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
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.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/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.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,

0 commit comments

Comments
 (0)