From 2a4ad533ddee18faf4e1f823b743a63bc665123b Mon Sep 17 00:00:00 2001 From: zcy Date: Mon, 3 Feb 2025 19:00:21 +0100 Subject: [PATCH] Polish code and add usage telemetry --- .../domain/telemetry/telemetryEvent.types.ts | 32 +++++++++++++++++++ packages/rum-core/src/boot/rumPublicApi.ts | 8 ++++- .../src/domain/contexts/viewHistory.spec.ts | 6 ++-- .../src/domain/contexts/viewHistory.ts | 4 +-- packages/rum-core/src/domain/lifeCycle.ts | 4 +-- .../rum-core/src/domain/view/trackViews.ts | 28 ++++++++-------- test/e2e/scenario/rum/init.scenario.ts | 2 +- 7 files changed, 62 insertions(+), 22 deletions(-) diff --git a/packages/core/src/domain/telemetry/telemetryEvent.types.ts b/packages/core/src/domain/telemetry/telemetryEvent.types.ts index 2ba79a50ea..b25a291f7d 100644 --- a/packages/core/src/domain/telemetry/telemetryEvent.types.ts +++ b/packages/core/src/domain/telemetry/telemetryEvent.types.ts @@ -429,6 +429,10 @@ export type TelemetryCommonFeaturesUsage = | SetTrackingConsent | StopSession | StartView + | SetViewName + | SetViewContext + | SetViewContextProperty + | GetViewContext | AddAction | AddError | SetGlobalContext @@ -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 diff --git a/packages/rum-core/src/boot/rumPublicApi.ts b/packages/rum-core/src/boot/rumPublicApi.ts index 35b7562e53..f5bb747bee 100644 --- a/packages/rum-core/src/boot/rumPublicApi.ts +++ b/packages/rum-core/src/boot/rumPublicApi.ts @@ -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) diff --git a/packages/rum-core/src/domain/contexts/viewHistory.spec.ts b/packages/rum-core/src/domain/contexts/viewHistory.spec.ts index 718e13ea9a..60a24bae35 100644 --- a/packages/rum-core/src/domain/contexts/viewHistory.spec.ts +++ b/packages/rum-core/src/domain/contexts/viewHistory.spec.ts @@ -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' @@ -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') }) @@ -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' }) }) }) diff --git a/packages/rum-core/src/domain/contexts/viewHistory.ts b/packages/rum-core/src/domain/contexts/viewHistory.ts index 16c5d86067..6939ff3cc2 100644 --- a/packages/rum-core/src/domain/contexts/viewHistory.ts +++ b/packages/rum-core/src/domain/contexts/viewHistory.ts @@ -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 @@ -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 diff --git a/packages/rum-core/src/domain/lifeCycle.ts b/packages/rum-core/src/domain/lifeCycle.ts index 30051962e5..772eb93f12 100644 --- a/packages/rum-core/src/domain/lifeCycle.ts +++ b/packages/rum-core/src/domain/lifeCycle.ts @@ -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 @@ -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 diff --git a/packages/rum-core/src/domain/view/trackViews.ts b/packages/rum-core/src/domain/view/trackViews.ts index e89794cd3c..b0d6c1a6d7 100644 --- a/packages/rum-core/src/domain/view/trackViews.ts +++ b/packages/rum-core/src/domain/view/trackViews.ts @@ -67,7 +67,7 @@ export interface ViewCreatedEvent { startClocks: ClocksState } -export interface ViewContextEvent { +export interface BeforeViewUpdateEvent { id: string name?: string context?: Context @@ -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, @@ -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, diff --git a/test/e2e/scenario/rum/init.scenario.ts b/test/e2e/scenario/rum/init.scenario.ts index 2a3d58a29a..003bf68bdf 100644 --- a/test/e2e/scenario/rum/init.scenario.ts +++ b/test/e2e/scenario/rum/init.scenario.ts @@ -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)