Skip to content

Commit

Permalink
πŸ”Š [RUMF-1041] add lcp info on view events (#1129)
Browse files Browse the repository at this point in the history
* πŸ”₯ remove _dd.sleep_duration

* πŸ”Š [RUMF-1041] add lcp info on view events
  • Loading branch information
BenoitZugmeyer authored Oct 14, 2021
1 parent 45f8df5 commit 3cdb491
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@ describe('trackFirstContentfulPaintTiming', () => {
describe('largestContentfulPaintTiming', () => {
let setupBuilder: TestSetupBuilder
let lcpCallback: jasmine.Spy<(value: RelativeTime) => void>
let discardCallback: jasmine.Spy<(discardReason: string) => void>
let emitter: Element

beforeEach(() => {
lcpCallback = jasmine.createSpy()
discardCallback = jasmine.createSpy()
emitter = document.createElement('div')
setupBuilder = setup().beforeBuild(({ lifeCycle }) =>
trackLargestContentfulPaintTiming(lifeCycle, emitter, lcpCallback)
trackLargestContentfulPaintTiming(lifeCycle, emitter, lcpCallback, discardCallback)
)
resetFirstHidden()
})
Expand All @@ -184,6 +186,7 @@ describe('largestContentfulPaintTiming', () => {

lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, FAKE_LARGEST_CONTENTFUL_PAINT_ENTRY)
expect(lcpCallback).not.toHaveBeenCalled()
expect(discardCallback).toHaveBeenCalledOnceWith('interaction')
})

it('should be discarded if the page is hidden', () => {
Expand All @@ -193,6 +196,7 @@ describe('largestContentfulPaintTiming', () => {
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, FAKE_LARGEST_CONTENTFUL_PAINT_ENTRY)

expect(lcpCallback).not.toHaveBeenCalled()
expect(discardCallback).toHaveBeenCalledOnceWith('hidden')
})

it('should be discarded if it is reported after a long time', () => {
Expand All @@ -203,6 +207,7 @@ describe('largestContentfulPaintTiming', () => {
startTime: TIMING_MAXIMUM_DELAY as RelativeTime,
})
expect(lcpCallback).not.toHaveBeenCalled()
expect(discardCallback).toHaveBeenCalledOnceWith('maximum delay')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
EventEmitter,
RelativeTime,
ONE_MINUTE,
addMonitoringMessage,
isExperimentalFeatureEnabled,
} from '@datadog/browser-core'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
import { trackFirstHidden } from './trackFirstHidden'
Expand All @@ -25,6 +23,7 @@ export interface Timings {
largestContentfulPaint?: Duration
firstInputDelay?: Duration
firstInputTime?: Duration
lcpDiscardReason?: string
}

export function trackInitialViewTimings(lifeCycle: LifeCycle, callback: (timings: Timings) => void) {
Expand All @@ -38,11 +37,20 @@ export function trackInitialViewTimings(lifeCycle: LifeCycle, callback: (timings
const { stop: stopFCPTracking } = trackFirstContentfulPaintTiming(lifeCycle, (firstContentfulPaint) =>
setTimings({ firstContentfulPaint })
)
const { stop: stopLCPTracking } = trackLargestContentfulPaintTiming(lifeCycle, window, (largestContentfulPaint) => {
setTimings({
largestContentfulPaint,
})
})
const { stop: stopLCPTracking } = trackLargestContentfulPaintTiming(
lifeCycle,
window,
(largestContentfulPaint) => {
setTimings({
largestContentfulPaint,
})
},
(lcpDiscardReason) => {
setTimings({
lcpDiscardReason,
})
}
)
const { stop: stopFIDTracking } = trackFirstInputTimings(lifeCycle, ({ firstInputDelay, firstInputTime }) => {
setTimings({
firstInputDelay,
Expand Down Expand Up @@ -99,7 +107,8 @@ export function trackFirstContentfulPaintTiming(lifeCycle: LifeCycle, callback:
export function trackLargestContentfulPaintTiming(
lifeCycle: LifeCycle,
emitter: EventEmitter,
callback: (lcpTiming: RelativeTime) => void
callback: (lcpTiming: RelativeTime) => void,
discardCallback: (discardReason: string) => void
) {
const firstHidden = trackFirstHidden()

Expand Down Expand Up @@ -130,23 +139,17 @@ export function trackLargestContentfulPaintTiming(
entry.startTime < TIMING_MAXIMUM_DELAY
) {
callback(entry.startTime)
} else if (isFirstLCP && isExperimentalFeatureEnabled('monitor-dropped-lcp')) {
} else if (isFirstLCP) {
const reason =
entry.startTime >= firstInteractionTimestamp
? 'after interaction'
? 'interaction'
: entry.startTime >= firstHidden.timeStamp
? 'after hidden'
? 'hidden'
: entry.startTime >= TIMING_MAXIMUM_DELAY
? 'after maximum delay'
? 'maximum delay'
: 'N/A'

addMonitoringMessage(`LCP dropped ${reason}`, {
debug: {
startTime: entry.startTime,
firstInteractionTimestamp,
firstHiddenTimestamp: firstHidden.timeStamp,
},
})
discardCallback(reason)
}
isFirstLCP = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
Configuration,
Observable,
isNumber,
isExperimentalFeatureEnabled,
} from '@datadog/browser-core'
import { RecorderApi } from '../../../boot/rumPublicApi'
import { RawRumViewEvent, RumEventType } from '../../../rawRumEvent.types'
import { LifeCycle, LifeCycleEventType, RawRumEventCollectedData } from '../../lifeCycle'
import { ForegroundContexts } from '../../foregroundContexts'
import { LocationChange } from '../../../browser/locationChangeObservable'
import { supportPerformanceTimingEvent } from '../../../browser/performanceCollection'
import { trackViews, ViewEvent } from './trackViews'

export function startViewCollection(
Expand Down Expand Up @@ -100,6 +102,15 @@ function processViewUpdate(
domainContext: {
location: view.location,
},
customerContext:
isExperimentalFeatureEnabled('monitor-dropped-lcp') && view.loadingType === 'initial_load'
? {
lcp: {
support: supportPerformanceTimingEvent('largest-contentful-paint'),
discard_reason: view.timings.lcpDiscardReason,
},
}
: undefined,
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface RawRumResourceEvent {
_dd?: {
trace_id?: string
span_id?: string // not available for initial document tracing
sleep_duration?: number
}
}

Expand Down

0 comments on commit 3cdb491

Please sign in to comment.