Skip to content

[POC][RUM-9952] send event on vital start #3570

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ describe('rum public api', () => {
expect(startDurationVitalSpy).toHaveBeenCalledWith('foo', {
description: 'description-value',
context: { foo: 'bar' },
userStory: undefined,
})
})
})
Expand Down
6 changes: 5 additions & 1 deletion packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ export interface RumPublicApi extends PublicApi {
* @param options.description Description of the vital
* @returns reference to the custom vital
*/
startDurationVital: (name: string, options?: { context?: object; description?: string }) => DurationVitalReference
startDurationVital: (
name: string,
options?: { context?: object; description?: string; userStory?: boolean }
) => DurationVitalReference

/**
* Stop a custom duration vital
Expand Down Expand Up @@ -647,6 +650,7 @@ export function makeRumPublicApi(
return strategy.startDurationVital(sanitize(name)!, {
context: sanitize(options && options.context) as Context,
description: sanitize(options && options.description) as string | undefined,
userStory: options && options.userStory,
})
}),

Expand Down
25 changes: 22 additions & 3 deletions packages/rum-core/src/domain/vital/vitalCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PageState } from '../contexts/pageStateHistory'
export interface DurationVitalOptions {
context?: Context
description?: string
userStory?: boolean
}

export interface DurationVitalReference {
Expand Down Expand Up @@ -58,10 +59,18 @@ export function startVitalCollection(
}
}

function sendDurationVitalStartEvent(vital: DurationVitalStart) {
if (vital) {
// temporary hack to send an event when the vital is started
const durationVitalStart = buildDurationVital(vital, vital.startClocks, {}, clocksNow())
addDurationVital(durationVitalStart)
}
}

return {
addDurationVital,
startDurationVital: (name: string, options: DurationVitalOptions = {}) =>
startDurationVital(customVitalsState, name, options),
startDurationVital(customVitalsState, name, options, sendDurationVitalStartEvent),
stopDurationVital: (nameOrRef: string | DurationVitalReference, options: DurationVitalOptions = {}) => {
stopDurationVital(addDurationVital, customVitalsState, nameOrRef, options)
},
Expand All @@ -71,7 +80,8 @@ export function startVitalCollection(
export function startDurationVital(
{ vitalsByName, vitalsByReference }: CustomVitalsState,
name: string,
options: DurationVitalOptions = {}
options: DurationVitalOptions = {},
sendDurationVitalStart?: (vital: DurationVitalStart) => void
) {
const vital = {
name,
Expand All @@ -80,9 +90,18 @@ export function startDurationVital(
description: options.description,
}

// EXPERIMENTAL: If the user story option is set, we send a start event immediately.
if (sendDurationVitalStart && options.userStory) {
sendDurationVitalStart(vital)
vital.context = combine(vital.context, {
userStory: options.userStory,
id: generateUUID(),
status: 'in-progress',
})
}

// To avoid leaking implementation details of the vital, we return a reference to it.
const reference: DurationVitalReference = { __dd_vital_reference: true }

vitalsByName.set(name, vital)

// To avoid memory leaks caused by the creation of numerous references (e.g., from improper useEffect implementations), we use a WeakMap.
Expand Down