Skip to content

ref(browser-utils): Align standalone LCP span attribute setting with pageload span LCP attributes #16844

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

Merged
merged 3 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ sentryTest('captures LCP vital as a standalone span', async ({ getLocalTestUrl,
'user_agent.original': expect.stringContaining('Chrome'),
'sentry.pageload.span_id': expect.stringMatching(/[a-f0-9]{16}/),
'lcp.element': 'body > img',
'lcp.id': '',
'lcp.loadTime': expect.any(Number),
'lcp.renderTime': expect.any(Number),
'lcp.size': expect.any(Number),
Expand Down
32 changes: 24 additions & 8 deletions packages/browser-utils/src/metrics/lcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function trackLcpAsStandaloneSpan(): void {
}
sentSpan = true;
if (pageloadSpanId) {
sendStandaloneLcpSpan(standaloneLcpValue, standaloneLcpEntry, pageloadSpanId);
_sendStandaloneLcpSpan(standaloneLcpValue, standaloneLcpEntry, pageloadSpanId);
}
cleanupLcpHandler();
}
Expand Down Expand Up @@ -88,7 +88,14 @@ export function trackLcpAsStandaloneSpan(): void {
}, 0);
}

function sendStandaloneLcpSpan(lcpValue: number, entry: LargestContentfulPaint | undefined, pageloadSpanId: string) {
/**
* Exported only for testing!
*/
export function _sendStandaloneLcpSpan(
lcpValue: number,
entry: LargestContentfulPaint | undefined,
pageloadSpanId: string,
) {
DEBUG_BUILD && logger.log(`Sending LCP span (${lcpValue})`);

const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + (entry?.startTime || 0));
Expand All @@ -105,12 +112,21 @@ function sendStandaloneLcpSpan(lcpValue: number, entry: LargestContentfulPaint |
};

if (entry) {
attributes['lcp.element'] = htmlTreeAsString(entry.element);
attributes['lcp.id'] = entry.id;
attributes['lcp.url'] = entry.url;
attributes['lcp.loadTime'] = entry.loadTime;
attributes['lcp.renderTime'] = entry.renderTime;
attributes['lcp.size'] = entry.size;
entry.element && (attributes['lcp.element'] = htmlTreeAsString(entry.element));
entry.id && (attributes['lcp.id'] = entry.id);

// Trim URL to the first 200 characters.
entry.url && (attributes['lcp.url'] = entry.url.trim().slice(0, 200));

// loadTime is the time of LCP that's related to receiving the LCP element response..
entry.loadTime != null && (attributes['lcp.loadTime'] = entry.loadTime);

// renderTime is loadTime + rendering time
// it's 0 if the LCP element is loaded from a 3rd party origin that doesn't send the
// `Timing-Allow-Origin` header.
entry.renderTime != null && (attributes['lcp.renderTime'] = entry.renderTime);

entry.size != null && (attributes['lcp.size'] = entry.size);
}

const span = startStandaloneWebVitalSpan({
Expand Down
Loading