Skip to content

Commit

Permalink
[mq] working branch - merge 768b4a4 on top of staging-07 at 5882947
Browse files Browse the repository at this point in the history
{"baseBranch":"staging-07","baseCommit":"58829477053f321b7e24835cfccb347db19a47d0","createdAt":"2024-02-13T08:58:57.818783Z","headSha":"768b4a46fc40eff6fe1224eeb5099a0002c16aae","id":"2b13cb01-a654-4f05-b6c4-f67608c00db1","isDirectMerge":true,"priority":"200","pullRequestNumber":"2598","queuedAt":"2024-02-13T08:58:57.818421Z","status":"STATUS_QUEUED"}
  • Loading branch information
dd-mergequeue[bot] committed Feb 13, 2024
2 parents 809b737 + 768b4a4 commit 2a4dad1
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 105 deletions.
23 changes: 14 additions & 9 deletions packages/core/src/domain/session/sessionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,23 +242,28 @@ describe('startSessionManager', () => {

it('should notify each expire and renew observables', () => {
const firstSessionManager = startSessionManagerWithDefaults({ productKey: FIRST_PRODUCT_KEY })
const calls: string[] = []
firstSessionManager.beforeExpireObservable.subscribe(() => calls.push('beforeExpireA'))
firstSessionManager.expireObservable.subscribe(() => calls.push('expireA'))
firstSessionManager.renewObservable.subscribe(() => calls.push('renewA'))
const expireSessionASpy = jasmine.createSpy()
firstSessionManager.expireObservable.subscribe(expireSessionASpy)
const renewSessionASpy = jasmine.createSpy()
firstSessionManager.renewObservable.subscribe(renewSessionASpy)

const secondSessionManager = startSessionManagerWithDefaults({ productKey: SECOND_PRODUCT_KEY })
secondSessionManager.beforeExpireObservable.subscribe(() => calls.push('beforeExpireB'))
secondSessionManager.expireObservable.subscribe(() => calls.push('expireB'))
secondSessionManager.renewObservable.subscribe(() => calls.push('renewB'))
const expireSessionBSpy = jasmine.createSpy()
secondSessionManager.expireObservable.subscribe(expireSessionBSpy)
const renewSessionBSpy = jasmine.createSpy()
secondSessionManager.renewObservable.subscribe(renewSessionBSpy)

expireSessionCookie()

expect(calls).toEqual(['beforeExpireA', 'expireA', 'beforeExpireB', 'expireB'])
expect(expireSessionASpy).toHaveBeenCalled()
expect(expireSessionBSpy).toHaveBeenCalled()
expect(renewSessionASpy).not.toHaveBeenCalled()
expect(renewSessionBSpy).not.toHaveBeenCalled()

document.dispatchEvent(createNewEvent(DOM_EVENT.CLICK))

expect(calls).toEqual(['beforeExpireA', 'expireA', 'beforeExpireB', 'expireB', 'renewA', 'renewB'])
expect(renewSessionASpy).toHaveBeenCalled()
expect(renewSessionBSpy).toHaveBeenCalled()
})
})

Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/domain/session/sessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { startSessionStore } from './sessionStore'
export interface SessionManager<TrackingType extends string> {
findActiveSession: (startTime?: RelativeTime) => SessionContext<TrackingType> | undefined
renewObservable: Observable<void>
beforeExpireObservable: Observable<void>
expireObservable: Observable<void>
expire: () => void
}
Expand All @@ -35,7 +34,6 @@ export function startSessionManager<TrackingType extends string>(
): SessionManager<TrackingType> {
const renewObservable = new Observable<void>()
const expireObservable = new Observable<void>()
const beforeExpireObservable = new Observable<void>()

// TODO - Improve configuration type and remove assertion
const sessionStore = startSessionStore(configuration.sessionStoreStrategyType!, productKey, computeSessionState)
Expand All @@ -49,9 +47,8 @@ export function startSessionManager<TrackingType extends string>(
renewObservable.notify()
})
sessionStore.expireObservable.subscribe(() => {
beforeExpireObservable.notify()
sessionContextHistory.closeActive(relativeNow())
expireObservable.notify()
sessionContextHistory.closeActive(relativeNow())
})

// We expand/renew session unconditionally as tracking consent is always granted when the session
Expand Down Expand Up @@ -85,7 +82,6 @@ export function startSessionManager<TrackingType extends string>(
findActiveSession: (startTime) => sessionContextHistory.find(startTime),
renewObservable,
expireObservable,
beforeExpireObservable,
expire: sessionStore.expire,
}
}
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/tools/instrumentMethod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setTimeout } from './timer'
import type { TimeoutId } from './timer'
import { setTimeout, clearTimeout } from './timer'
import { callMonitored } from './monitor'
import { noop } from './utils/functionUtils'

Expand Down Expand Up @@ -125,11 +126,14 @@ export function instrumentSetter<TARGET extends { [key: string]: any }, PROPERTY
return { stop: noop }
}

const timeoutIds = new Set<TimeoutId>()
let instrumentation = (target: TARGET, value: TARGET[PROPERTY]) => {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
const timeoutId = setTimeout(() => {
timeoutIds.delete(timeoutId)
after(target, value)
}, 0)
timeoutIds.add(timeoutId)
}

const instrumentationWrapper = function (this: TARGET, value: TARGET[PROPERTY]) {
Expand All @@ -148,6 +152,10 @@ export function instrumentSetter<TARGET extends { [key: string]: any }, PROPERTY
} else {
instrumentation = noop
}
if (timeoutIds) {
timeoutIds.forEach(clearTimeout)
timeoutIds.clear()
}
},
}
}
11 changes: 5 additions & 6 deletions packages/rum-core/src/boot/startRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('rum session', () => {
expect(serverRumEvents[0].type).toEqual('view')
expect(serverRumEvents[0].session.id).toEqual('42')

lifeCycle.notify(LifeCycleEventType.BEFORE_SESSION_EXPIRED)
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
expect(serverRumEvents.length).toEqual(2)

session.setId('43')
Expand Down Expand Up @@ -314,10 +314,10 @@ describe('rum events url', () => {
describe('view events', () => {
let setupBuilder: TestSetupBuilder
let interceptor: ReturnType<typeof interceptRequests>
let startRumResult: ReturnType<typeof startRum>

beforeEach(() => {
setupBuilder = setup().beforeBuild(({ configuration, customerDataTrackerManager }) => {
startRumResult = startRum(
setupBuilder = setup().beforeBuild(({ configuration, customerDataTrackerManager }) =>
startRum(
{} as RumInitConfiguration,
configuration,
noopRecorderApi,
Expand All @@ -327,8 +327,7 @@ describe('view events', () => {
createIdentityEncoder,
createTrackingConsentState(TrackingConsent.GRANTED)
)
return startRumResult
})
)
interceptor = interceptRequests()
})

Expand Down
1 change: 0 additions & 1 deletion packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export function startRum(

const pageExitObservable = createPageExitObservable(configuration)
const pageExitSubscription = pageExitObservable.subscribe((event) => {
lifeCycle.notify(LifeCycleEventType.BEFORE_PAGE_EXITED, event)
lifeCycle.notify(LifeCycleEventType.PAGE_EXITED, event)
})
cleanupTasks.push(() => pageExitSubscription.unsubscribe())
Expand Down
5 changes: 3 additions & 2 deletions packages/rum-core/src/domain/action/trackClickActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('trackClickActions', () => {
let input: HTMLInputElement
let setupBuilder: TestSetupBuilder
let findActionId: ActionContexts['findActionId']

beforeEach(() => {
button = document.createElement('button')
button.type = 'button'
Expand Down Expand Up @@ -175,13 +176,13 @@ describe('trackClickActions', () => {
expect(findActionId()).toEqual([])
})

it('ongoing click action is stopped before the view is ended', () => {
it('ongoing click action is stopped on view end', () => {
const { lifeCycle, clock } = setupBuilder.build()
emulateClick({ activity: { delay: BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY } })

clock.tick(BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY)

lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
endClocks: clocksNow(),
})

Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/domain/action/trackClickActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function trackClickActions(
history.reset()
})

lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_ENDED, stopClickChain)
lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, stopClickChain)

const { stop: stopActionEventsListener } = listenActionEvents<{
clickActionBase: ClickActionBase
Expand Down Expand Up @@ -192,7 +192,7 @@ function startClickAction(
CLICK_ACTION_MAX_DURATION
)

const viewEndedSubscription = lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_ENDED, ({ endClocks }) => {
const viewEndedSubscription = lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, ({ endClocks }) => {
click.stop(endClocks.timeStamp)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('featureFlagContexts', () => {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'foo')
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(10 as RelativeTime),
} as ViewEndedEvent)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
Expand All @@ -155,7 +155,7 @@ describe('featureFlagContexts', () => {

clock.tick(10)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'one')
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(10 as RelativeTime),
} as ViewEndedEvent)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function startFeatureFlagContexts(
customerDataTracker.resetCustomerData()
})

lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, ({ endClocks }) => {
lifeCycle.subscribe(LifeCycleEventType.AFTER_VIEW_ENDED, ({ endClocks }) => {
featureFlagContexts.closeActive(endClocks.relative)
})

Expand Down
8 changes: 4 additions & 4 deletions packages/rum-core/src/domain/contexts/urlContexts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('urlContexts', () => {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
changeLocation('/foo')
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(10 as RelativeTime),
} as ViewEndedEvent)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
Expand All @@ -88,7 +88,7 @@ describe('urlContexts', () => {

clock.tick(10)
changeLocation('/foo')
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(10 as RelativeTime),
} as ViewEndedEvent)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
Expand All @@ -100,7 +100,7 @@ describe('urlContexts', () => {

clock.tick(10)
changeLocation('/qux')
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(30 as RelativeTime),
} as ViewEndedEvent)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('urlContexts', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(10 as RelativeTime),
} as ViewEndedEvent)

Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/contexts/urlContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function startUrlContexts(
previousViewUrl = viewUrl
})

lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, ({ endClocks }) => {
lifeCycle.subscribe(LifeCycleEventType.AFTER_VIEW_ENDED, ({ endClocks }) => {
urlContextHistory.closeActive(endClocks.relative)
})

Expand Down
12 changes: 6 additions & 6 deletions packages/rum-core/src/domain/contexts/viewContexts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ describe('viewContexts', () => {
LifeCycleEventType.BEFORE_VIEW_CREATED,
buildViewCreatedEvent({ startClocks: relativeToClocks(10 as RelativeTime), id: 'view 1' })
)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })

lifeCycle.notify(
LifeCycleEventType.BEFORE_VIEW_CREATED,
buildViewCreatedEvent({ startClocks: relativeToClocks(20 as RelativeTime), id: 'view 2' })
)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks: relativeToClocks(30 as RelativeTime) })
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, { endClocks: relativeToClocks(30 as RelativeTime) })

lifeCycle.notify(
LifeCycleEventType.BEFORE_VIEW_CREATED,
Expand All @@ -79,12 +79,12 @@ describe('viewContexts', () => {
LifeCycleEventType.BEFORE_VIEW_CREATED,
buildViewCreatedEvent({ startClocks: relativeToClocks(10 as RelativeTime), id: 'view 1' })
)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })
lifeCycle.notify(
LifeCycleEventType.BEFORE_VIEW_CREATED,
buildViewCreatedEvent({ startClocks: relativeToClocks(20 as RelativeTime), id: 'view 2' })
)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })

expect(viewContexts.findView(5 as RelativeTime)).not.toBeDefined()
})
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('viewContexts', () => {
startClocks: relativeToClocks(10 as RelativeTime),
})
)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, { endClocks: relativeToClocks(20 as RelativeTime) })
lifeCycle.notify(
LifeCycleEventType.BEFORE_VIEW_CREATED,
buildViewCreatedEvent({
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('viewContexts', () => {
startClocks: originalClocks,
})
)
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks((originalTime + 10) as RelativeTime),
})
lifeCycle.notify(
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/contexts/viewContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function startViewContexts(lifeCycle: LifeCycle): ViewContexts {
viewContextHistory.add(buildViewContext(view), view.startClocks.relative)
})

lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, ({ endClocks }) => {
lifeCycle.subscribe(LifeCycleEventType.AFTER_VIEW_ENDED, ({ endClocks }) => {
viewContextHistory.closeActive(endClocks.relative)
})

Expand Down
21 changes: 6 additions & 15 deletions packages/rum-core/src/domain/lifeCycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ import type { AutoAction } from './action/actionCollection'
import type { ViewEvent, ViewCreatedEvent, ViewEndedEvent } from './view/trackViews'

export const enum LifeCycleEventType {
// BEFORE_XXX events have been introduced to provide hooks just before significant actions or transitions in the application lifecycle.
// The following three events allow for performing tasks, such as sending last events, since the SDK contexts (viewContext, etc.) are still active.
BEFORE_VIEW_ENDED,
BEFORE_SESSION_EXPIRED,
BEFORE_PAGE_EXITED,
// The BEFORE_VIEW_CREATED event has been introduced to provide a hook just before a view is created,
// ensuring that the context related to the view (viewContexts, urlContexts) exists at VIEW_CREATED.
BEFORE_VIEW_CREATED,

// Contexts (like viewContexts) should be opened using prefixed BEFORE_XXX events and closed using prefixed AFTER_XXX events
// It ensures the context is available during the non prefixed event callbacks
PERFORMANCE_ENTRIES_COLLECTED,
AUTO_ACTION_COMPLETED,
BEFORE_VIEW_CREATED,
VIEW_CREATED,
VIEW_UPDATED,
VIEW_ENDED,
AFTER_VIEW_ENDED,
REQUEST_STARTED,
REQUEST_COMPLETED,
// The SESSION_EXPIRED lifecycle event has been introduced to represent when a session has expired
Expand Down Expand Up @@ -63,14 +58,12 @@ declare const LifeCycleEventTypeAsConst: {
BEFORE_VIEW_CREATED: LifeCycleEventType.BEFORE_VIEW_CREATED
VIEW_CREATED: LifeCycleEventType.VIEW_CREATED
VIEW_UPDATED: LifeCycleEventType.VIEW_UPDATED
BEFORE_VIEW_ENDED: LifeCycleEventType.BEFORE_VIEW_ENDED
VIEW_ENDED: LifeCycleEventType.VIEW_ENDED
AFTER_VIEW_ENDED: LifeCycleEventType.AFTER_VIEW_ENDED
REQUEST_STARTED: LifeCycleEventType.REQUEST_STARTED
REQUEST_COMPLETED: LifeCycleEventType.REQUEST_COMPLETED
BEFORE_SESSION_EXPIRED: LifeCycleEventType.BEFORE_SESSION_EXPIRED
SESSION_EXPIRED: LifeCycleEventType.SESSION_EXPIRED
SESSION_RENEWED: LifeCycleEventType.SESSION_RENEWED
BEFORE_PAGE_EXITED: LifeCycleEventType.BEFORE_PAGE_EXITED
PAGE_EXITED: LifeCycleEventType.PAGE_EXITED
RAW_RUM_EVENT_COLLECTED: LifeCycleEventType.RAW_RUM_EVENT_COLLECTED
RUM_EVENT_COLLECTED: LifeCycleEventType.RUM_EVENT_COLLECTED
Expand All @@ -85,14 +78,12 @@ export interface LifeCycleEventMap {
[LifeCycleEventTypeAsConst.BEFORE_VIEW_CREATED]: ViewCreatedEvent
[LifeCycleEventTypeAsConst.VIEW_CREATED]: ViewCreatedEvent
[LifeCycleEventTypeAsConst.VIEW_UPDATED]: ViewEvent
[LifeCycleEventTypeAsConst.BEFORE_VIEW_ENDED]: ViewEndedEvent
[LifeCycleEventTypeAsConst.VIEW_ENDED]: ViewEndedEvent
[LifeCycleEventTypeAsConst.AFTER_VIEW_ENDED]: ViewEndedEvent
[LifeCycleEventTypeAsConst.REQUEST_STARTED]: RequestStartEvent
[LifeCycleEventTypeAsConst.REQUEST_COMPLETED]: RequestCompleteEvent
[LifeCycleEventTypeAsConst.BEFORE_SESSION_EXPIRED]: void
[LifeCycleEventTypeAsConst.SESSION_EXPIRED]: void
[LifeCycleEventTypeAsConst.SESSION_RENEWED]: void
[LifeCycleEventTypeAsConst.BEFORE_PAGE_EXITED]: PageExitEvent
[LifeCycleEventTypeAsConst.PAGE_EXITED]: PageExitEvent
[LifeCycleEventTypeAsConst.RAW_RUM_EVENT_COLLECTED]: RawRumEventCollectedData
[LifeCycleEventTypeAsConst.RUM_EVENT_COLLECTED]: RumEvent & Context
Expand Down
Loading

0 comments on commit 2a4dad1

Please sign in to comment.