Skip to content

Commit 083d170

Browse files
committed
improvements for refactors
1 parent 2ca125b commit 083d170

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export function startTrackingElementTiming(): () => void {
4343
* exported only for testing
4444
*/
4545
export const _onElementTiming = ({ entries }: { entries: PerformanceEntry[] }): void => {
46+
const activeSpan = getActiveSpan();
47+
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
48+
const transactionName = rootSpan
49+
? spanToJSON(rootSpan).description
50+
: getCurrentScope().getScopeData().transactionName;
51+
4652
entries.forEach(entry => {
4753
const elementEntry = entry as PerformanceElementTiming;
4854

@@ -63,11 +69,11 @@ export const _onElementTiming = ({ entries }: { entries: PerformanceEntry[] }):
6369
// - `renderTime` if available (available for all entries, except 3rd party images, but these should be covered by `loadTime`, 0 otherwise)
6470
// - `timestampInSeconds()` as a safeguard
6571
// see https://developer.mozilla.org/en-US/docs/Web/API/PerformanceElementTiming/renderTime#cross-origin_image_render_time
66-
const { spanStartTime, spanStartTimeSource } = loadTime
67-
? { spanStartTime: msToSec(loadTime), spanStartTimeSource: 'load-time' }
72+
const [spanStartTime, spanStartTimeSource] = loadTime
73+
? [msToSec(loadTime), 'load-time']
6874
: renderTime
69-
? { spanStartTime: msToSec(renderTime), spanStartTimeSource: 'render-time' }
70-
: { spanStartTime: timestampInSeconds(), spanStartTimeSource: 'entry-emission' };
75+
? [msToSec(renderTime), 'render-time']
76+
: [timestampInSeconds(), 'entry-emission'];
7177

7278
const duration =
7379
paintType === 'image-paint'
@@ -78,30 +84,26 @@ export const _onElementTiming = ({ entries }: { entries: PerformanceEntry[] }):
7884
: // for `'text-paint'` entries, we can't get a duration because the `loadTime` is always zero.
7985
0;
8086

81-
const activeSpan = getActiveSpan();
82-
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
83-
const route = rootSpan ? spanToJSON(rootSpan).description : getCurrentScope().getScopeData().transactionName;
84-
8587
const attributes: SpanAttributes = {
8688
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.browser.elementtiming',
8789
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'ui.elementtiming',
8890
// name must be user-entered, so we can assume low cardinality
8991
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
9092
// recording the source of the span start time, as it varies depending on available data
91-
'sentry.span-start-time-source': spanStartTimeSource,
92-
route,
93-
93+
'sentry.span_start_time_source': spanStartTimeSource,
94+
'sentry.transaction_name': transactionName,
95+
'element.id': elementEntry.id,
9496
'element.type': elementEntry.element?.tagName?.toLowerCase() || 'unknown',
9597
'element.size':
9698
elementEntry.naturalWidth && elementEntry.naturalHeight
9799
? `${elementEntry.naturalWidth}x${elementEntry.naturalHeight}`
98100
: undefined,
99-
'element.render-time': renderTime,
100-
'element.load-time': loadTime,
101+
'element.render_time': renderTime,
102+
'element.load_time': loadTime,
101103
// `url` is `0`(number) for text paints (hence we fall back to undefined)
102104
'element.url': elementEntry.url || undefined,
103105
'element.identifier': elementEntry.identifier,
104-
'element.paint-type': paintType,
106+
'element.paint_type': paintType,
105107
};
106108

107109
startSpan(

0 commit comments

Comments
 (0)