-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathlcp.ts
More file actions
122 lines (103 loc) · 4.12 KB
/
lcp.ts
File metadata and controls
122 lines (103 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { Client, SpanAttributes } from '@sentry/core';
import {
browserPerformanceTimeOrigin,
debug,
getCurrentScope,
htmlTreeAsString,
SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME,
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT,
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
} from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build';
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;
}
/**
* Starts tracking the Largest Contentful Paint on the current page and collects the value once
*
* - the page visibility is hidden
* - a navigation span is started (to stop LCP measurement for SPA soft navigations)
*
* Once either of these events triggers, the LCP value is sent as a standalone span and we stop
* measuring LCP for subsequent routes.
*/
export function trackLcpAsStandaloneSpan(client: Client): void {
let standaloneLcpValue = 0;
let standaloneLcpEntry: LargestContentfulPaint | undefined;
if (!supportsWebVital('largest-contentful-paint')) {
return;
}
const cleanupLcpHandler = addLcpInstrumentationHandler(({ metric }) => {
const entry = metric.entries[metric.entries.length - 1] as LargestContentfulPaint | undefined;
if (!entry || !isValidLcpMetric(metric.value)) {
return;
}
standaloneLcpValue = metric.value;
standaloneLcpEntry = entry;
}, true);
listenForWebVitalReportEvents(client, (reportEvent, pageloadSpanId) => {
_sendStandaloneLcpSpan(standaloneLcpValue, standaloneLcpEntry, pageloadSpanId, reportEvent);
cleanupLcpHandler();
});
}
/**
* Exported only for testing!
*/
export function _sendStandaloneLcpSpan(
lcpValue: number,
entry: LargestContentfulPaint | undefined,
pageloadSpanId: string,
reportEvent: WebVitalReportEvent,
) {
if (!isValidLcpMetric(lcpValue)) {
return;
}
DEBUG_BUILD && debug.log(`Sending LCP span (${lcpValue})`);
const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + (entry?.startTime || 0));
const routeName = getCurrentScope().getScopeData().transactionName;
const name = entry ? htmlTreeAsString(entry.element) : 'Largest contentful paint';
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser.lcp',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'ui.webvital.lcp',
[SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME]: 0, // LCP is a point-in-time metric
// attach the pageload span id to the LCP span so that we can link them in the UI
'sentry.pageload.span_id': pageloadSpanId,
// describes what triggered the web vital to be reported
'sentry.report_event': reportEvent,
};
if (entry) {
entry.element && (attributes['lcp.element'] = htmlTreeAsString(entry.element));
entry.id && (attributes['lcp.id'] = entry.id);
entry.url && (attributes['lcp.url'] = entry.url);
// 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({
name,
transaction: routeName,
attributes,
startTime,
});
if (span) {
span.addEvent('lcp', {
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: 'millisecond',
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: lcpValue,
});
// LCP is a point-in-time metric, so we end the span immediately
span.end(startTime);
}
}