Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(browser): filter implausible LCP values#20338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,6 +15,15 @@ import { addLcpInstrumentationHandler } from './instrument'; | ||
| import type { WebVitalReportEvent } from './utils'; | ||
| import { listenForWebVitalReportEvents, msToSec, startStandaloneWebVitalSpan, supportsWebVital } from './utils'; | ||
| /** | ||
| * 60 seconds is the maximum for a plausible LCP value. | ||
| */ | ||
| export const MAX_PLAUSIBLE_LCP_DURATION = 60_000; | ||
| export function isValidLcpMetric(lcpValue: number | undefined): lcpValue is number { | ||
| return lcpValue != null && lcpValue > 0 && lcpValue <= MAX_PLAUSIBLE_LCP_DURATION; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * Starts tracking the Largest Contentful Paint on the current page and collects the value once | ||
| * | ||
| @@ -34,7 +43,7 @@ export function trackLcpAsStandaloneSpan(client: Client): void { | ||
| const cleanupLcpHandler = addLcpInstrumentationHandler(({ metric }) => { | ||
| const entry = metric.entries[metric.entries.length - 1] as LargestContentfulPaint | undefined; | ||
| if (!entry) { | ||
| if (!entry || !isValidLcpMetric(metric.value)) { | ||
| return; | ||
| } | ||
| standaloneLcpValue = metric.value; | ||
logaretm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @@ -56,6 +65,10 @@ export function _sendStandaloneLcpSpan( | ||
| pageloadSpanId: string, | ||
| reportEvent: WebVitalReportEvent, | ||
| ) { | ||
| if (!isValidLcpMetric(lcpValue)) { | ||
| return; | ||
| } | ||
| DEBUG_BUILD && debug.log(`Sending LCP span (${lcpValue})`); | ||
| const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + (entry?.startTime || 0)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import * as SentryCore from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { _sendStandaloneLcpSpan, isValidLcpMetric, MAX_PLAUSIBLE_LCP_DURATION } from '../../src/metrics/lcp'; | ||
| import * as WebVitalUtils from '../../src/metrics/utils'; | ||
| vi.mock('@sentry/core', async () => { | ||
| const actual = await vi.importActual('@sentry/core'); | ||
| return { | ||
| ...actual, | ||
| browserPerformanceTimeOrigin: vi.fn(), | ||
| getCurrentScope: vi.fn(), | ||
| htmlTreeAsString: vi.fn(), | ||
| }; | ||
| }); | ||
| describe('isValidLcpMetric', () => { | ||
| it('returns true for plausible lcp values', () => { | ||
| expect(isValidLcpMetric(1)).toBe(true); | ||
| expect(isValidLcpMetric(2_500)).toBe(true); | ||
| expect(isValidLcpMetric(MAX_PLAUSIBLE_LCP_DURATION)).toBe(true); | ||
| }); | ||
| it('returns false for implausible lcp values', () => { | ||
| expect(isValidLcpMetric(undefined)).toBe(false); | ||
| expect(isValidLcpMetric(0)).toBe(false); | ||
| expect(isValidLcpMetric(-1)).toBe(false); | ||
| expect(isValidLcpMetric(MAX_PLAUSIBLE_LCP_DURATION + 1)).toBe(false); | ||
| }); | ||
| }); | ||
| describe('_sendStandaloneLcpSpan', () => { | ||
| const mockSpan = { | ||
| addEvent: vi.fn(), | ||
| end: vi.fn(), | ||
| }; | ||
| const mockScope = { | ||
| getScopeData: vi.fn().mockReturnValue({ | ||
| transactionName: 'test-transaction', | ||
| }), | ||
| }; | ||
| beforeEach(() => { | ||
| vi.mocked(SentryCore.getCurrentScope).mockReturnValue(mockScope as any); | ||
| vi.mocked(SentryCore.browserPerformanceTimeOrigin).mockReturnValue(1000); | ||
| vi.mocked(SentryCore.htmlTreeAsString).mockImplementation((node: any) => `<${node?.tagName || 'div'}>`); | ||
| vi.spyOn(WebVitalUtils, 'startStandaloneWebVitalSpan').mockReturnValue(mockSpan as any); | ||
| }); | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
| it('sends a standalone lcp span with entry data', () => { | ||
| const lcpValue = 1_234; | ||
| const mockEntry: LargestContentfulPaint = { | ||
| name: 'largest-contentful-paint', | ||
| entryType: 'largest-contentful-paint', | ||
| startTime: 100, | ||
| duration: 0, | ||
| id: 'image', | ||
| url: 'https://example.com/image.png', | ||
| size: 1234, | ||
| loadTime: 95, | ||
| renderTime: 100, | ||
| element: { tagName: 'img' } as Element, | ||
| toJSON: vi.fn(), | ||
| }; | ||
| _sendStandaloneLcpSpan(lcpValue, mockEntry, '123', 'navigation'); | ||
| expect(WebVitalUtils.startStandaloneWebVitalSpan).toHaveBeenCalledWith({ | ||
| name: '<img>', | ||
| transaction: 'test-transaction', | ||
| attributes: { | ||
| 'sentry.origin': 'auto.http.browser.lcp', | ||
| 'sentry.op': 'ui.webvital.lcp', | ||
| 'sentry.exclusive_time': 0, | ||
| 'sentry.pageload.span_id': '123', | ||
| 'sentry.report_event': 'navigation', | ||
| 'lcp.element': '<img>', | ||
| 'lcp.id': 'image', | ||
| 'lcp.url': 'https://example.com/image.png', | ||
| 'lcp.loadTime': 95, | ||
| 'lcp.renderTime': 100, | ||
| 'lcp.size': 1234, | ||
| }, | ||
| startTime: 1.1, | ||
| }); | ||
| expect(mockSpan.addEvent).toHaveBeenCalledWith('lcp', { | ||
| 'sentry.measurement_unit': 'millisecond', | ||
| 'sentry.measurement_value': lcpValue, | ||
| }); | ||
| expect(mockSpan.end).toHaveBeenCalledWith(1.1); | ||
| }); | ||
| it('does not send a standalone lcp span for implausibly large values', () => { | ||
| _sendStandaloneLcpSpan(MAX_PLAUSIBLE_LCP_DURATION + 1, undefined, '123', 'pagehide'); | ||
| expect(WebVitalUtils.startStandaloneWebVitalSpan).not.toHaveBeenCalled(); | ||
| expect(mockSpan.addEvent).not.toHaveBeenCalled(); | ||
| expect(mockSpan.end).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.