Skip to content

Commit

Permalink
Polish code and add usage telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
cy-moi committed Feb 3, 2025
1 parent c883c7e commit 2a4ad53
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
32 changes: 32 additions & 0 deletions packages/core/src/domain/telemetry/telemetryEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ export type TelemetryCommonFeaturesUsage =
| SetTrackingConsent
| StopSession
| StartView
| SetViewName
| SetViewContext
| SetViewContextProperty
| GetViewContext
| AddAction
| AddError
| SetGlobalContext
Expand Down Expand Up @@ -596,6 +600,34 @@ export interface StartView {
feature: 'start-view'
[k: string]: unknown
}
export interface SetViewContext {
/**
* setViewContext API
*/
feature: 'set-view-context'
[k: string]: unknown
}
export interface SetViewContextProperty {
/**
* setViewContextProperty API
*/
feature: 'set-view-context-property'
[k: string]: unknown
}
export interface SetViewName {
/**
* setViewName API
*/
feature: 'set-view-name'
[k: string]: unknown
}
export interface GetViewContext {
/**
* getViewContext API
*/
feature: 'get-view-context'
[k: string]: unknown
}
export interface AddAction {
/**
* addAction API
Expand Down
8 changes: 7 additions & 1 deletion packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,23 @@ export function makeRumPublicApi(

setViewName: monitor((name: string) => {
strategy.setViewName(name)
addTelemetryUsage({ feature: 'set-view-name' })
}),

setViewContext: monitor((context: Context) => {
strategy.setViewContext(context)
addTelemetryUsage({ feature: 'set-view-context' })
}),

setViewContextProperty: monitor((key: string, value: any) => {
strategy.setViewContextProperty(key, value)
addTelemetryUsage({ feature: 'set-view-context-property' })
}),

getViewContext: monitor(() => strategy.getViewContext()),
getViewContext: monitor(() => {
addTelemetryUsage({ feature: 'set-view-context-property' })
return strategy.getViewContext()
}),

setGlobalContext: monitor((context) => {
globalContextManager.setContext(context)
Expand Down
6 changes: 3 additions & 3 deletions packages/rum-core/src/domain/contexts/viewHistory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { relativeToClocks, CLEAR_OLD_VALUES_INTERVAL } from '@datadog/browser-co
import type { Clock } from '@datadog/browser-core/test'
import { mockClock, registerCleanupTask } from '@datadog/browser-core/test'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import type { ViewContextEvent, ViewCreatedEvent } from '../view/trackViews'
import type { BeforeViewUpdateEvent, ViewCreatedEvent } from '../view/trackViews'
import type { ViewHistory } from './viewHistory'
import { startViewHistory, VIEW_CONTEXT_TIME_OUT_DELAY } from './viewHistory'

Expand Down Expand Up @@ -101,7 +101,7 @@ describe('ViewHistory', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_UPDATED, {
startClocks,
name: 'Fake Name',
} as ViewContextEvent)
} as BeforeViewUpdateEvent)
expect(viewHistory.findView()!.name).toBe('Fake Name')
})

Expand All @@ -110,7 +110,7 @@ describe('ViewHistory', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_UPDATED, {
startClocks,
context: { bar: 'foo' } as Context,
} as ViewContextEvent)
} as BeforeViewUpdateEvent)
expect(viewHistory.findView()!.context).toEqual({ bar: 'foo' })
})
})
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/domain/contexts/viewHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RelativeTime, ClocksState, Context } from '@datadog/browser-core'
import { SESSION_TIME_OUT_DELAY, createValueHistory } from '@datadog/browser-core'
import type { LifeCycle } from '../lifeCycle'
import { LifeCycleEventType } from '../lifeCycle'
import type { ViewContextEvent, ViewCreatedEvent } from '../view/trackViews'
import type { BeforeViewUpdateEvent, ViewCreatedEvent } from '../view/trackViews'

export const VIEW_CONTEXT_TIME_OUT_DELAY = SESSION_TIME_OUT_DELAY

Expand Down Expand Up @@ -32,7 +32,7 @@ export function startViewHistory(lifeCycle: LifeCycle): ViewHistory {
viewValueHistory.closeActive(endClocks.relative)
})

lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_UPDATED, (viewUpdate: ViewContextEvent) => {
lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_UPDATED, (viewUpdate: BeforeViewUpdateEvent) => {
const currentView = viewValueHistory.find(viewUpdate.startClocks.relative)
if (currentView && viewUpdate.name) {
currentView.name = viewUpdate.name
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/domain/lifeCycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { RumEvent } from '../rumEvent.types'
import type { CommonContext } from './contexts/commonContext'
import type { RequestCompleteEvent, RequestStartEvent } from './requestCollection'
import type { AutoAction } from './action/actionCollection'
import type { ViewEvent, ViewCreatedEvent, ViewEndedEvent, ViewContextEvent } from './view/trackViews'
import type { ViewEvent, ViewCreatedEvent, ViewEndedEvent, BeforeViewUpdateEvent } from './view/trackViews'

export const enum LifeCycleEventType {
// Contexts (like viewHistory) should be opened using prefixed BEFORE_XXX events and closed using prefixed AFTER_XXX events
Expand Down Expand Up @@ -76,7 +76,7 @@ export interface LifeCycleEventMap {
[LifeCycleEventTypeAsConst.AUTO_ACTION_COMPLETED]: AutoAction
[LifeCycleEventTypeAsConst.BEFORE_VIEW_CREATED]: ViewCreatedEvent
[LifeCycleEventTypeAsConst.VIEW_CREATED]: ViewCreatedEvent
[LifeCycleEventTypeAsConst.BEFORE_VIEW_UPDATED]: ViewContextEvent
[LifeCycleEventTypeAsConst.BEFORE_VIEW_UPDATED]: BeforeViewUpdateEvent
[LifeCycleEventTypeAsConst.VIEW_UPDATED]: ViewEvent
[LifeCycleEventTypeAsConst.VIEW_ENDED]: ViewEndedEvent
[LifeCycleEventTypeAsConst.AFTER_VIEW_ENDED]: ViewEndedEvent
Expand Down
28 changes: 15 additions & 13 deletions packages/rum-core/src/domain/view/trackViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface ViewCreatedEvent {
startClocks: ClocksState
}

export interface ViewContextEvent {
export interface BeforeViewUpdateEvent {
id: string
name?: string
context?: Context
Expand Down Expand Up @@ -236,13 +236,9 @@ function newView(
lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, viewCreatedEvent)

// Update the view every time the measures are changing
const { throttled: scheduleViewUpdate, cancel: cancelScheduleViewUpdate } = throttle(
triggerViewUpdate,
THROTTLE_VIEW_UPDATE_PERIOD,
{
leading: false,
}
)
const { throttled, cancel: cancelScheduleViewUpdate } = throttle(triggerViewUpdate, THROTTLE_VIEW_UPDATE_PERIOD, {
leading: false,
})

const {
setLoadEvent,
Expand Down Expand Up @@ -274,28 +270,34 @@ function newView(
triggerViewUpdate()

// View context update should always be throttled
contextManager.changeObservable.subscribe(() => {
contextManager.changeObservable.subscribe(scheduleViewUpdate)

function triggerBeforeViewUpdate() {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_UPDATED, {
id,
name,
context: contextManager.getContext(),
startClocks,
})
scheduleViewUpdate()
})
}

function scheduleViewUpdate() {
triggerBeforeViewUpdate()
throttled()
}

function triggerViewUpdate() {
cancelScheduleViewUpdate()
triggerBeforeViewUpdate()

documentVersion += 1
const currentEnd = endClocks === undefined ? timeStampNow() : endClocks.timeStamp
lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, {
customTimings,
documentVersion,
id,
name,
service,
version,
context: contextManager.getContext(),
loadingType,
location,
startClocks,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/scenario/rum/init.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('API calls and events around init', () => {
})

createTest('should be able to set view context')
.withRum({ enableExperimentalFeatures: ['view_specific_context'] })
.withRum()
.withRumSlim()
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
Expand Down

0 comments on commit 2a4ad53

Please sign in to comment.