Skip to content

Commit

Permalink
Sample attribution telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Jul 19, 2023
1 parent c027125 commit 2d9d55c
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 76 deletions.
4 changes: 2 additions & 2 deletions packages/rum-core/src/boot/startRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
deleteEventBridgeStub,
} from '@datadog/browser-core/test'
import type { RumSessionManagerMock, TestSetupBuilder } from '../../test'
import { createRumSessionManagerMock, noopRecorderApi, setup } from '../../test'
import { createRumSessionManagerMock, noopRecorderApi, noopWebVitalTelemetryDebug, setup } from '../../test'
import type { RumPerformanceNavigationTiming, RumPerformanceEntry } from '../browser/performanceCollection'
import type { LifeCycle } from '../domain/lifeCycle'
import { LifeCycleEventType } from '../domain/lifeCycle'
Expand Down Expand Up @@ -74,7 +74,7 @@ function startRumStub(
startFeatureFlagContexts(lifeCycle),
pageStateHistory,
noopRecorderApi,
sessionManager
noopWebVitalTelemetryDebug
)

startLongTaskCollection(lifeCycle, sessionManager)
Expand Down
5 changes: 4 additions & 1 deletion packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { startCustomerDataTelemetry } from '../domain/startCustomerDataTelemetry
import { startPageStateHistory } from '../domain/contexts/pageStateHistory'
import type { CommonContext } from '../domain/contexts/commonContext'
import { buildCommonContext } from '../domain/contexts/commonContext'
import { startWebVitalTelemetryDebug } from '../domain/rumEventsCollection/view/startWebVitalTelemetryDebug'
import type { RecorderApi } from './rumPublicApi'

export function startRum(
Expand Down Expand Up @@ -118,6 +119,8 @@ export function startRum(

startLongTaskCollection(lifeCycle, session)
startResourceCollection(lifeCycle, configuration, session, pageStateHistory)

const webVitalTelemetryDebug = startWebVitalTelemetryDebug(configuration, telemetry, recorderApi, session)
const { addTiming, startView } = startViewCollection(
lifeCycle,
configuration,
Expand All @@ -127,7 +130,7 @@ export function startRum(
featureFlagContexts,
pageStateHistory,
recorderApi,
session,
webVitalTelemetryDebug,
initialViewOptions
)
const { addError } = startErrorCollection(lifeCycle, pageStateHistory, featureFlagContexts)
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function validateAndBuildRumConfiguration(
defaultPrivacyLevel: objectHasValue(DefaultPrivacyLevel, initConfiguration.defaultPrivacyLevel)
? initConfiguration.defaultPrivacyLevel
: DefaultPrivacyLevel.MASK_USER_INPUT,
customerDataTelemetrySampleRate: 1,
customerDataTelemetrySampleRate: 100,
},
baseConfiguration
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { noopRecorderApi, type BuildContext } from '../../../../test'
import { noopWebVitalTelemetryDebug } from '../../../../test'
import { type BuildContext } from '../../../../test'
import { LifeCycleEventType } from '../../lifeCycle'
import type { ViewEvent, ViewOptions } from './trackViews'
import { trackViews } from './trackViews'

export type ViewTest = ReturnType<typeof setupViewTest>

export function setupViewTest(
{ lifeCycle, location, domMutationObservable, configuration, locationChangeObservable, sessionManager }: BuildContext,
{ lifeCycle, location, domMutationObservable, configuration, locationChangeObservable }: BuildContext,
initialViewOptions?: ViewOptions
) {
const {
Expand All @@ -33,8 +34,7 @@ export function setupViewTest(
configuration,
locationChangeObservable,
!configuration.trackViewsManually,
noopRecorderApi,
sessionManager,
noopWebVitalTelemetryDebug,
initialViewOptions
)
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { addTelemetryDebug, elapsed, noop, performDraw, relativeNow, toServerDuration } from '@datadog/browser-core'
import type { Telemetry, RelativeTime } from '@datadog/browser-core'
import type { RecorderApi } from '../../../boot/rumPublicApi'
import type { RumSessionManager } from '../../rumSessionManager'
import type { RumConfiguration } from '../../configuration'

export type WebVitalTelemetryDebug = ReturnType<typeof startWebVitalTelemetryDebug>

export function startWebVitalTelemetryDebug(
configuration: RumConfiguration,
telemetry: Telemetry,
recorderApi: RecorderApi,
session: RumSessionManager
) {
const webVitalTelemetryEnabled = telemetry.enabled && performDraw(configuration.customerDataTelemetrySampleRate)

if (!webVitalTelemetryEnabled) {
return {
addWebVitalTelemetryDebug: noop,
}
}
return {
addWebVitalTelemetryDebug(webVitalName: string, webVitalNode: Node | undefined, webVitalTime: RelativeTime) {
const computationTime = relativeNow()
if (!recorderApi.isRecording()) {
recorderApi.recorderStartObservable.subscribe((recordingStartTime) => {
addTelemetryDebug(`${webVitalName} attribution recording delay`, {
computationDelay: toServerDuration(elapsed(webVitalTime, computationTime)),
recordingDelay: toServerDuration(elapsed(webVitalTime, recordingStartTime)),
hasNode: !!webVitalNode,
serializedDomNode: webVitalNode ? recorderApi.getSerializedNodeId(webVitalNode) : undefined,
})
})
}

addTelemetryDebug(`${webVitalName} attribution`, {
computationDelay: toServerDuration(elapsed(webVitalTime, computationTime)),
hasNode: !!webVitalNode,
replayRecording: recorderApi.isRecording(),
replaySampled: session.findTrackedSession()?.sessionReplayAllowed,
serializedDomNode: webVitalNode ? recorderApi.getSerializedNodeId(webVitalNode) : undefined,
})
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import type { Duration, RelativeTime } from '@datadog/browser-core'
import { DOM_EVENT } from '@datadog/browser-core'
import { restorePageVisibility, setPageVisibility, createNewEvent } from '@datadog/browser-core/test'
import type { TestSetupBuilder } from '../../../../test'
import { createRumSessionManagerMock, noopRecorderApi, setup } from '../../../../test'
import { noopWebVitalTelemetryDebug, setup } from '../../../../test'
import type {
RumFirstInputTiming,
RumLargestContentfulPaintTiming,
RumPerformanceNavigationTiming,
RumPerformancePaintTiming,
} from '../../../browser/performanceCollection'
import { LifeCycleEventType } from '../../lifeCycle'
import type { RumSessionManager } from '../../rumSessionManager'
import { resetFirstHidden } from './trackFirstHidden'
import type { Timings } from './trackInitialViewTimings'
import {
Expand Down Expand Up @@ -57,18 +56,15 @@ describe('trackInitialViewTimings', () => {
let scheduleViewUpdateSpy: jasmine.Spy<() => void>
let trackInitialViewTimingsResult: ReturnType<typeof trackInitialViewTimings>
let setLoadEventSpy: jasmine.Spy<(loadEvent: Duration) => void>
let sessionManager: RumSessionManager

beforeEach(() => {
scheduleViewUpdateSpy = jasmine.createSpy()
setLoadEventSpy = jasmine.createSpy()
sessionManager = createRumSessionManagerMock()

setupBuilder = setup().beforeBuild(({ lifeCycle }) => {
trackInitialViewTimingsResult = trackInitialViewTimings(
lifeCycle,
noopRecorderApi,
sessionManager,
noopWebVitalTelemetryDebug,
setLoadEventSpy,
scheduleViewUpdateSpy
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ import {
relativeNow,
} from '@datadog/browser-core'

import type { RecorderApi } from '../../../boot/rumPublicApi'
import type { LifeCycle } from '../../lifeCycle'
import { LifeCycleEventType } from '../../lifeCycle'
import type {
RumFirstInputTiming,
RumLargestContentfulPaintTiming,
RumPerformancePaintTiming,
} from '../../../browser/performanceCollection'
import type { RumSessionManager } from '../../rumSessionManager'
import { trackFirstHidden } from './trackFirstHidden'
import { addWebVitalTelemetryDebug } from './addWebVitalTelemetryDebug'
import type { WebVitalTelemetryDebug } from './startWebVitalTelemetryDebug'

// Discard LCP and FCP timings above a certain delay to avoid incorrect data
// It happens in some cases like sleep mode or some browser implementations
Expand Down Expand Up @@ -49,8 +47,7 @@ export interface Timings {

export function trackInitialViewTimings(
lifeCycle: LifeCycle,
recorderApi: RecorderApi,
session: RumSessionManager,
webVitalTelemetryDebug: WebVitalTelemetryDebug,
setLoadEvent: (loadEnd: Duration) => void,
scheduleViewUpdate: () => void
) {
Expand All @@ -72,7 +69,7 @@ export function trackInitialViewTimings(
lifeCycle,
window,
(largestContentfulPaint, lcpElement) => {
addWebVitalTelemetryDebug(recorderApi, session, 'LCP', lcpElement, largestContentfulPaint)
webVitalTelemetryDebug.addWebVitalTelemetryDebug('LCP', lcpElement, largestContentfulPaint)

setTimings({
largestContentfulPaint,
Expand All @@ -83,7 +80,7 @@ export function trackInitialViewTimings(
const { stop: stopFIDTracking } = trackFirstInputTimings(
lifeCycle,
({ firstInputDelay, firstInputTime, firstInputTarget }) => {
addWebVitalTelemetryDebug(recorderApi, session, 'FID', firstInputTarget, firstInputTime)
webVitalTelemetryDebug.addWebVitalTelemetryDebug('FID', firstInputTarget, firstInputTime)

setTimings({
firstInputDelay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
throttle,
find,
} from '@datadog/browser-core'
import type { RecorderApi } from '../../../boot/rumPublicApi'
import type { RumLayoutShiftTiming } from '../../../browser/performanceCollection'
import { supportPerformanceTimingEvent } from '../../../browser/performanceCollection'
import { ViewLoadingType } from '../../../rawRumEvent.types'
Expand All @@ -23,8 +22,7 @@ import { waitPageActivityEnd } from '../../waitPageActivityEnd'

import { getScrollY } from '../../../browser/scroll'
import { getViewportDimension } from '../../../browser/viewportObservable'
import type { RumSessionManager } from '../../rumSessionManager'
import { addWebVitalTelemetryDebug } from './addWebVitalTelemetryDebug'
import type { WebVitalTelemetryDebug } from './startWebVitalTelemetryDebug'

export interface ScrollMetrics {
maxDepth: number
Expand All @@ -48,8 +46,7 @@ export function trackViewMetrics(
scheduleViewUpdate: () => void,
loadingType: ViewLoadingType,
viewStart: ClocksState,
recorderApi: RecorderApi,
session: RumSessionManager
webVitalTelemetryDebug: WebVitalTelemetryDebug
) {
const viewMetrics: ViewMetrics = {}

Expand Down Expand Up @@ -99,7 +96,7 @@ export function trackViewMetrics(

if (!clsAttributionCollected) {
clsAttributionCollected = true
addWebVitalTelemetryDebug(recorderApi, session, 'CLS', largestLayoutShiftNode, largestLayoutShiftTime)
webVitalTelemetryDebug.addWebVitalTelemetryDebug('CLS', largestLayoutShiftNode, largestLayoutShiftTime)
}
scheduleViewUpdate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
clearInterval,
} from '@datadog/browser-core'

import type { RecorderApi } from '../../../boot/rumPublicApi'
import type { ViewCustomTimings } from '../../../rawRumEvent.types'
import { ViewLoadingType } from '../../../rawRumEvent.types'

Expand All @@ -26,12 +25,12 @@ import { LifeCycleEventType } from '../../lifeCycle'
import type { EventCounts } from '../../trackEventCounts'
import type { LocationChange } from '../../../browser/locationChangeObservable'
import type { RumConfiguration } from '../../configuration'
import type { RumSessionManager } from '../../rumSessionManager'
import type { Timings } from './trackInitialViewTimings'
import { trackInitialViewTimings } from './trackInitialViewTimings'
import type { ScrollMetrics } from './trackViewMetrics'
import { trackViewMetrics } from './trackViewMetrics'
import { trackViewEventCounts } from './trackViewEventCounts'
import type { WebVitalTelemetryDebug } from './startWebVitalTelemetryDebug'

export interface ViewEvent {
id: string
Expand Down Expand Up @@ -81,8 +80,7 @@ export function trackViews(
configuration: RumConfiguration,
locationChangeObservable: Observable<LocationChange>,
areViewsTrackedAutomatically: boolean,
recorderApi: RecorderApi,
session: RumSessionManager,
webVitalTelemetryDebug: WebVitalTelemetryDebug,
initialViewOptions?: ViewOptions
) {
let currentView = startNewView(ViewLoadingType.INITIAL_LOAD, clocksOrigin(), initialViewOptions)
Expand All @@ -101,8 +99,7 @@ export function trackViews(
configuration,
location,
loadingType,
recorderApi,
session,
webVitalTelemetryDebug,
startClocks,
viewOptions
)
Expand Down Expand Up @@ -160,8 +157,7 @@ function newView(
configuration: RumConfiguration,
initialLocation: Location,
loadingType: ViewLoadingType,
recorderApi: RecorderApi,
session: RumSessionManager,
webVitalTelemetryDebug: WebVitalTelemetryDebug,
startClocks: ClocksState = clocksNow(),
viewOptions?: ViewOptions
) {
Expand Down Expand Up @@ -211,13 +207,12 @@ function newView(
scheduleViewUpdate,
loadingType,
startClocks,
recorderApi,
session
webVitalTelemetryDebug
)

const { scheduleStop: scheduleStopInitialViewTimingsTracking, timings } =
loadingType === ViewLoadingType.INITIAL_LOAD
? trackInitialViewTimings(lifeCycle, recorderApi, session, setLoadEvent, scheduleViewUpdate)
? trackInitialViewTimings(lifeCycle, webVitalTelemetryDebug, setLoadEvent, scheduleViewUpdate)
: { scheduleStop: noop, timings: {} as Timings }

const { scheduleStop: scheduleStopEventCountsTracking, eventCounts } = trackViewEventCounts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Duration, RelativeTime, ServerDuration, TimeStamp } from '@datadog
import { resetExperimentalFeatures, ExperimentalFeature, addExperimentalFeatures } from '@datadog/browser-core'
import type { RecorderApi } from '../../../boot/rumPublicApi'
import type { TestSetupBuilder } from '../../../../test'
import { setup, noopRecorderApi } from '../../../../test'
import { setup, noopRecorderApi, noopWebVitalTelemetryDebug } from '../../../../test'
import type { RawRumViewEvent } from '../../../rawRumEvent.types'
import { RumEventType, ViewLoadingType } from '../../../rawRumEvent.types'
import { LifeCycleEventType } from '../../lifeCycle'
Expand Down Expand Up @@ -72,7 +72,6 @@ describe('viewCollection', () => {
domMutationObservable,
locationChangeObservable,
pageStateHistory,
sessionManager,
}) => {
getReplayStatsSpy = jasmine.createSpy()
startViewCollection(
Expand All @@ -87,7 +86,7 @@ describe('viewCollection', () => {
...noopRecorderApi,
getReplayStats: getReplayStatsSpy,
},
sessionManager
noopWebVitalTelemetryDebug
)
}
)
Expand Down
Loading

0 comments on commit 2d9d55c

Please sign in to comment.