Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚗️[RUM-2889] Bootstrap custom vital APIs #2591

Merged
merged 8 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions packages/rum-core/src/domain/vital/vitalCollection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { clocksNow } from '@datadog/browser-core'
import type { TestSetupBuilder } from '../../../test'
import { setup } from '../../../test'
import type { RawRumVitalEvent } from '../../rawRumEvent.types'
import { VitalType, RumEventType } from '../../rawRumEvent.types'
import { startVitalCollection } from './vitalCollection'

describe('vitalCollection', () => {
let setupBuilder: TestSetupBuilder
let vitalCollection: ReturnType<typeof startVitalCollection>

beforeEach(() => {
setupBuilder = setup()
.withFakeClock()
.beforeBuild(({ lifeCycle }) => {
vitalCollection = startVitalCollection(lifeCycle)
})
})

describe('custom duration', () => {
it('should create duration vital from start/stop API', () => {
const { rawRumEvents, clock } = setupBuilder.build()

vitalCollection.startDurationVital({ name: 'foo', startClocks: clocksNow() })
clock.tick(100)
vitalCollection.stopDurationVital({ name: 'foo', stopClocks: clocksNow() })

expect(rawRumEvents.length).toBe(1)
expect((rawRumEvents[0].rawRumEvent as RawRumVitalEvent).vital.custom.foo).toBe(100)
})

it('should not create duration vital without calling the stop API', () => {
const { rawRumEvents } = setupBuilder.build()

vitalCollection.startDurationVital({ name: 'foo', startClocks: clocksNow() })

expect(rawRumEvents.length).toBe(0)
})

it('should not create duration vital without calling the start API', () => {
const { rawRumEvents } = setupBuilder.build()

vitalCollection.stopDurationVital({ name: 'foo', stopClocks: clocksNow() })

expect(rawRumEvents.length).toBe(0)
})

it('should create multiple duration vitals from start/stop API', () => {
const { rawRumEvents, clock } = setupBuilder.build()

vitalCollection.startDurationVital({ name: 'foo', startClocks: clocksNow() })
clock.tick(100)
vitalCollection.startDurationVital({ name: 'bar', startClocks: clocksNow() })
clock.tick(100)
vitalCollection.stopDurationVital({ name: 'bar', stopClocks: clocksNow() })
clock.tick(100)
vitalCollection.stopDurationVital({ name: 'foo', stopClocks: clocksNow() })

expect(rawRumEvents.length).toBe(2)
expect((rawRumEvents[0].rawRumEvent as RawRumVitalEvent).vital.custom.bar).toBe(100)
expect((rawRumEvents[1].rawRumEvent as RawRumVitalEvent).vital.custom.foo).toBe(300)
})

it('should discard a previous start with the same name', () => {
const { rawRumEvents, clock } = setupBuilder.build()

vitalCollection.startDurationVital({ name: 'foo', startClocks: clocksNow() })
clock.tick(100)
vitalCollection.startDurationVital({ name: 'foo', startClocks: clocksNow() })
clock.tick(100)
vitalCollection.stopDurationVital({ name: 'foo', stopClocks: clocksNow() })

expect(rawRumEvents.length).toBe(1)
expect((rawRumEvents[0].rawRumEvent as RawRumVitalEvent).vital.custom.foo).toBe(100)
})
})

it('should collect raw rum event from duration vital', () => {
const { rawRumEvents } = setupBuilder.build()

vitalCollection.startDurationVital({ name: 'foo', startClocks: clocksNow() })
vitalCollection.stopDurationVital({ name: 'foo', stopClocks: clocksNow() })

expect(rawRumEvents[0].startTime).toEqual(jasmine.any(Number))
expect(rawRumEvents[0].rawRumEvent).toEqual({
date: jasmine.any(Number),
vital: {
id: jasmine.any(String),
type: VitalType.DURATION,
custom: {
foo: 0,
},
},
type: RumEventType.VITAL,
})
expect(rawRumEvents[0].domainContext).toEqual({})
})
})
52 changes: 48 additions & 4 deletions packages/rum-core/src/domain/vital/vitalCollection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { ClocksState } from '@datadog/browser-core'
import type { LifeCycle } from '../lifeCycle'
import { elapsed, generateUUID } from '@datadog/browser-core'
import { LifeCycleEventType } from '../lifeCycle'
import type { LifeCycle, RawRumEventCollectedData } from '../lifeCycle'
import type { RawRumVitalEvent } from '../../rawRumEvent.types'
import { RumEventType, VitalType } from '../../rawRumEvent.types'

export interface DurationVitalStart {
name: string
Expand All @@ -11,9 +15,49 @@ export interface DurationVitalStop {
stopClocks: ClocksState
}

export function startVitalCollection(lifecycle: LifeCycle) {
interface Vital {
name: string
type: VitalType
startClocks: ClocksState
value: number
}
bcaudan marked this conversation as resolved.
Show resolved Hide resolved

export function startVitalCollection(lifeCycle: LifeCycle) {
const vitalStartsByName: { [name: string]: DurationVitalStart } = {}
bcaudan marked this conversation as resolved.
Show resolved Hide resolved
return {
startDurationVital: (vitalStart: DurationVitalStart) => {
vitalStartsByName[vitalStart.name] = vitalStart
},
stopDurationVital: (vitalStop: DurationVitalStop) => {
const vitalStart = vitalStartsByName[vitalStop.name]
if (!vitalStart) {
return
}
const vital = {
name: vitalStart.name,
type: VitalType.DURATION,
startClocks: vitalStart.startClocks,
value: elapsed(vitalStart.startClocks.timeStamp, vitalStop.stopClocks.timeStamp),
}
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processVital(vital))
},
}
}

function processVital(vital: Vital): RawRumEventCollectedData<RawRumVitalEvent> {
return {
startDurationVital: (vitalStart: DurationVitalStart) => {},
stopDurationVital: (vitalStop: DurationVitalStop) => {},
rawRumEvent: {
date: vital.startClocks.timeStamp,
vital: {
id: generateUUID(),
type: vital.type,
custom: {
[vital.name]: vital.value,
},
},
type: RumEventType.VITAL,
},
startTime: vital.startClocks.relative,
domainContext: {},
}
}