Skip to content

Commit 0727988

Browse files
authored
ref(browser-utils): Align standalone LCP span attribute setting with pageload span LCP attributes (#16844)
Small follow-up from #16591: As identified by cursor (lol), our way of setting the `lcp.*` attributes wasn't 100% aligned with how and when we set these attributes on the pageload span. This PR changes two things: - only set attributes if the respective LCP entry values are defined - truncate `lcp.url` if it is longer than 200 characters Also: - re-added comments, since we'll likely remove the pageload-based logic at some point - also add `lcp.size` if `entry.size === 0` (which previously was falsy and hence not added) - added unit tests
1 parent a215bf9 commit 0727988

File tree

2 files changed

+24
-9
lines changed
  • dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-standalone-spans
  • packages/browser-utils/src/metrics

2 files changed

+24
-9
lines changed

dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-standalone-spans/test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ sentryTest('captures LCP vital as a standalone span', async ({ getLocalTestUrl,
6060
'user_agent.original': expect.stringContaining('Chrome'),
6161
'sentry.pageload.span_id': expect.stringMatching(/[a-f0-9]{16}/),
6262
'lcp.element': 'body > img',
63-
'lcp.id': '',
6463
'lcp.loadTime': expect.any(Number),
6564
'lcp.renderTime': expect.any(Number),
6665
'lcp.size': expect.any(Number),

packages/browser-utils/src/metrics/lcp.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function trackLcpAsStandaloneSpan(): void {
4444
}
4545
sentSpan = true;
4646
if (pageloadSpanId) {
47-
sendStandaloneLcpSpan(standaloneLcpValue, standaloneLcpEntry, pageloadSpanId);
47+
_sendStandaloneLcpSpan(standaloneLcpValue, standaloneLcpEntry, pageloadSpanId);
4848
}
4949
cleanupLcpHandler();
5050
}
@@ -88,7 +88,14 @@ export function trackLcpAsStandaloneSpan(): void {
8888
}, 0);
8989
}
9090

91-
function sendStandaloneLcpSpan(lcpValue: number, entry: LargestContentfulPaint | undefined, pageloadSpanId: string) {
91+
/**
92+
* Exported only for testing!
93+
*/
94+
export function _sendStandaloneLcpSpan(
95+
lcpValue: number,
96+
entry: LargestContentfulPaint | undefined,
97+
pageloadSpanId: string,
98+
) {
9299
DEBUG_BUILD && logger.log(`Sending LCP span (${lcpValue})`);
93100

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

107114
if (entry) {
108-
attributes['lcp.element'] = htmlTreeAsString(entry.element);
109-
attributes['lcp.id'] = entry.id;
110-
attributes['lcp.url'] = entry.url;
111-
attributes['lcp.loadTime'] = entry.loadTime;
112-
attributes['lcp.renderTime'] = entry.renderTime;
113-
attributes['lcp.size'] = entry.size;
115+
entry.element && (attributes['lcp.element'] = htmlTreeAsString(entry.element));
116+
entry.id && (attributes['lcp.id'] = entry.id);
117+
118+
// Trim URL to the first 200 characters.
119+
entry.url && (attributes['lcp.url'] = entry.url.trim().slice(0, 200));
120+
121+
// loadTime is the time of LCP that's related to receiving the LCP element response..
122+
entry.loadTime != null && (attributes['lcp.loadTime'] = entry.loadTime);
123+
124+
// renderTime is loadTime + rendering time
125+
// it's 0 if the LCP element is loaded from a 3rd party origin that doesn't send the
126+
// `Timing-Allow-Origin` header.
127+
entry.renderTime != null && (attributes['lcp.renderTime'] = entry.renderTime);
128+
129+
entry.size != null && (attributes['lcp.size'] = entry.size);
114130
}
115131

116132
const span = startStandaloneWebVitalSpan({

0 commit comments

Comments
 (0)