Skip to content

Commit 9c599bd

Browse files
committed
update nested span handling in svelte
1 parent 0e913c7 commit 9c599bd

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

packages/svelte/src/performance.ts

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Span } from '@sentry/types';
33
import { afterUpdate, beforeUpdate, onMount } from 'svelte';
44
import { current_component } from 'svelte/internal';
55

6-
import { getRootSpan, startInactiveSpan, withActiveSpan } from '@sentry/core';
6+
import { startInactiveSpan } from '@sentry/core';
77
import { DEFAULT_COMPONENT_NAME, UI_SVELTE_INIT, UI_SVELTE_UPDATE } from './constants';
88
import type { TrackComponentOptions } from './types';
99

@@ -32,17 +32,16 @@ export function trackComponent(options?: TrackComponentOptions): void {
3232
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
3333
const componentName = `<${customComponentName || current_component.constructor.name || DEFAULT_COMPONENT_NAME}>`;
3434

35-
let initSpan: Span | undefined = undefined;
3635
if (mergedOptions.trackInit) {
37-
initSpan = recordInitSpan(componentName);
36+
recordInitSpan(componentName);
3837
}
3938

4039
if (mergedOptions.trackUpdates) {
41-
recordUpdateSpans(componentName, initSpan);
40+
recordUpdateSpans(componentName);
4241
}
4342
}
4443

45-
function recordInitSpan(componentName: string): Span | undefined {
44+
function recordInitSpan(componentName: string): void {
4645
const initSpan = startInactiveSpan({
4746
onlyIfParent: true,
4847
op: UI_SVELTE_INIT,
@@ -53,35 +52,21 @@ function recordInitSpan(componentName: string): Span | undefined {
5352
onMount(() => {
5453
initSpan.end();
5554
});
56-
57-
return initSpan;
5855
}
5956

60-
function recordUpdateSpans(componentName: string, initSpan?: Span): void {
57+
function recordUpdateSpans(componentName: string): void {
6158
let updateSpan: Span | undefined;
6259
beforeUpdate(() => {
63-
// We need to get the active transaction again because the initial one could
64-
// already be finished or there is currently no transaction going on.
60+
// If there is no active span, we skip
6561
const activeSpan = getActiveSpan();
6662
if (!activeSpan) {
6763
return;
6864
}
6965

70-
// If we are initializing the component when the update span is started, we start it as child
71-
// of the init span. Else, we start it as a child of the transaction.
72-
const parentSpan =
73-
initSpan && initSpan.isRecording() && getRootSpan(initSpan) === getRootSpan(activeSpan)
74-
? initSpan
75-
: getRootSpan(activeSpan);
76-
77-
if (!parentSpan) return;
78-
79-
updateSpan = withActiveSpan(parentSpan, () => {
80-
return startInactiveSpan({
81-
op: UI_SVELTE_UPDATE,
82-
name: componentName,
83-
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.svelte' },
84-
});
66+
updateSpan = startInactiveSpan({
67+
op: UI_SVELTE_UPDATE,
68+
name: componentName,
69+
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.svelte' },
8570
});
8671
});
8772

packages/svelte/test/performance.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Sentry.trackComponent()', () => {
3333
});
3434
});
3535

36-
it('creates nested init and update spans on component initialization', async () => {
36+
it('creates init and update spans on component initialization', async () => {
3737
startSpan({ name: 'outer' }, span => {
3838
expect(span).toBeDefined();
3939
render(DummyComponent, { props: { options: {} } });
@@ -73,7 +73,7 @@ describe('Sentry.trackComponent()', () => {
7373
description: '<Dummy$>',
7474
op: 'ui.svelte.update',
7575
origin: 'auto.ui.svelte',
76-
parent_span_id: initSpanId,
76+
parent_span_id: rootSpanId,
7777
span_id: expect.any(String),
7878
start_timestamp: expect.any(Number),
7979
timestamp: expect.any(Number),
@@ -128,7 +128,7 @@ describe('Sentry.trackComponent()', () => {
128128
description: '<Dummy$>',
129129
op: 'ui.svelte.update',
130130
origin: 'auto.ui.svelte',
131-
parent_span_id: initSpanId,
131+
parent_span_id: rootSpanId,
132132
span_id: expect.any(String),
133133
start_timestamp: expect.any(Number),
134134
timestamp: expect.any(Number),

0 commit comments

Comments
 (0)