Skip to content

Commit

Permalink
🔊 [RUMF-1041] add lcp info on view events
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Oct 12, 2021
1 parent 63dc200 commit 5c3d77e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 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 @@ -6,12 +6,14 @@ import {
toServerDuration,
Configuration,
Observable,
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 @@ -99,5 +101,14 @@ 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,
}
}

0 comments on commit 5c3d77e

Please sign in to comment.