Skip to content

Commit

Permalink
🐛 [RUMF-1421] keep updating the view event counters after view end
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Dec 9, 2022
1 parent 399c85c commit 060a6dd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import type { Context } from '@datadog/browser-core'
import { noop } from '@datadog/browser-core'
import type { RumResourceEvent } from '../../../rumEvent.types'
import { RumEventType } from '../../../rawRumEvent.types'
import type { Clock } from '../../../../../core/test/specHelper'
import { mockClock } from '../../../../../core/test/specHelper'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
import { trackViewEventCounts } from './trackViewEventCounts'
import { KEEP_TRACKING_EVENT_COUNTS_AFTER_VIEW_DELAY, trackViewEventCounts } from './trackViewEventCounts'

describe('trackViewEventCounts', () => {
const VIEW_ID = 'a'
const OTHER_VIEW_ID = 'b'
let lifeCycle: LifeCycle
let clock: Clock | undefined

beforeEach(() => {
lifeCycle = new LifeCycle()
})

afterEach(() => {
if (clock) clock.cleanup()
})

it('initializes eventCounts to 0', () => {
const { eventCounts } = trackViewEventCounts(lifeCycle, VIEW_ID, noop)

Expand Down Expand Up @@ -42,6 +49,23 @@ describe('trackViewEventCounts', () => {
expect(eventCounts.resourceCount).toBe(0)
})

it('when calling scheduleStop, it keeps counting events for a bit of time', () => {
clock = mockClock()
const { scheduleStop, eventCounts } = trackViewEventCounts(lifeCycle, VIEW_ID, noop)

scheduleStop()

clock.tick(KEEP_TRACKING_EVENT_COUNTS_AFTER_VIEW_DELAY - 1)
notifyResourceEvent()

expect(eventCounts.resourceCount).toBe(1)

clock.tick(1)
notifyResourceEvent()

expect(eventCounts.resourceCount).toBe(1)
})

function notifyResourceEvent(viewId = VIEW_ID) {
lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.RESOURCE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import { ONE_MINUTE } from '@datadog/browser-core'
import type { LifeCycle } from '../../lifeCycle'
import { trackEventCounts } from '../../trackEventCounts'

// Arbitrary delay for stopping event counting after the view ends. Ideally, we would not stop and
// keep counting events until the end of the session. But this might have a small performance impact
// if there are many many views: we would need to go through each event to see if the related view
// matches. So let's have a fairly short delay to avoid impacting performances too much.
//
// In the future, we could have views stored in a data structure similar to ContextHistory. Whenever
// a child event is collected, we could look into this history to find the matching view and
// increase the associated and increase its counter. Having a centralized data structure for it
// would allow us to look for views more efficiently.
//
// For now, having a small cleanup delay will already improve the situation in most cases.

export const KEEP_TRACKING_EVENT_COUNTS_AFTER_VIEW_DELAY = 5 * ONE_MINUTE

export function trackViewEventCounts(lifeCycle: LifeCycle, viewId: string, onChange: () => void) {
const { stop, eventCounts } = trackEventCounts({
lifeCycle,
isChildEvent: (event) => event.view.id === viewId,
onChange,
})

return { stop, eventCounts }
return {
scheduleStop: () => {
setTimeout(stop, KEEP_TRACKING_EVENT_COUNTS_AFTER_VIEW_DELAY)
},
eventCounts,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ function newView(
viewMetrics,
} = trackViewMetrics(lifeCycle, domMutationObservable, configuration, scheduleViewUpdate, loadingType, startClocks)

const { stop: stopEventCountsTracking, eventCounts } = trackViewEventCounts(lifeCycle, id, scheduleViewUpdate)
const { scheduleStop: scheduleStopEventCountsTracking, eventCounts } = trackViewEventCounts(
lifeCycle,
id,
scheduleViewUpdate
)

// Initial view update
triggerViewUpdate()
Expand Down Expand Up @@ -269,7 +273,7 @@ function newView(
endClocks = clocks
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks })
stopViewMetricsTracking()
stopEventCountsTracking()
scheduleStopEventCountsTracking()
},
triggerUpdate() {
// cancel any pending view updates execution
Expand Down

0 comments on commit 060a6dd

Please sign in to comment.