Skip to content

Commit d265348

Browse files
committed
improve tracing check, optimize svelteKitSpansIntegration
1 parent cd9c441 commit d265348

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

packages/sveltekit/src/server-common/handle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ async function instrumentHandle(
162162
}),
163163
});
164164

165-
const kitRootSpan = event.tracing?.root;
165+
const kitRootSpan = event.tracing?.enabled ? event.tracing?.root : undefined;
166166

167167
if (sentrySpan) {
168168
setHttpStatus(sentrySpan, res.status);

packages/sveltekit/src/server-common/integrations/svelteKitSpans.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export function svelteKitSpansIntegration(): Integration {
1212
// Using preprocessEvent to ensure the processing happens before user-configured
1313
// event processors are executed
1414
preprocessEvent(event) {
15-
if (event.type === 'transaction') {
15+
// only iterate over the spans if the root span was emitted by SvelteKit
16+
if (event.type === 'transaction' && event.contexts?.trace?.data?.['sveltekit.tracing.original_name']) {
1617
event.spans?.forEach(_enhanceKitSpan);
1718
}
1819
},

packages/sveltekit/test/server-common/integrations/svelteKitSpans.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ describe('svelteKitSpansIntegration', () => {
1111
expect(typeof integration.preprocessEvent).toBe('function');
1212
});
1313

14-
it('enhances spans from SvelteKit', () => {
14+
it('enhances spans from SvelteKit, if root span was emitted by SvelteKit', () => {
1515
const event: TransactionEvent = {
1616
type: 'transaction',
17+
contexts: {
18+
trace: {
19+
span_id: '123',
20+
trace_id: 'abc',
21+
data: {
22+
'sveltekit.tracing.original_name': 'sveltekit.handle.root',
23+
},
24+
},
25+
},
1726
spans: [
1827
{
1928
description: 'sveltekit.resolve',
@@ -37,6 +46,39 @@ describe('svelteKitSpansIntegration', () => {
3746
expect(event.spans?.[0]?.data[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBe('auto.http.sveltekit');
3847
});
3948

49+
it('does not enhance spans from SvelteKit, if root span was not emitted by SvelteKit', () => {
50+
const event: TransactionEvent = {
51+
type: 'transaction',
52+
contexts: {
53+
trace: {
54+
span_id: '123',
55+
trace_id: 'abc',
56+
data: {},
57+
},
58+
},
59+
spans: [
60+
{
61+
description: 'sveltekit.resolve',
62+
data: {
63+
someAttribute: 'someValue',
64+
},
65+
span_id: '123',
66+
trace_id: 'abc',
67+
start_timestamp: 0,
68+
},
69+
],
70+
};
71+
72+
// @ts-expect-error -- passing in an empty option for client but it is unused in the integration
73+
svelteKitSpansIntegration().preprocessEvent?.(event, {}, {});
74+
75+
expect(event.spans).toHaveLength(1);
76+
expect(event.spans?.[0]?.op).toBeUndefined();
77+
expect(event.spans?.[0]?.origin).toBeUndefined();
78+
expect(event.spans?.[0]?.data[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBeUndefined();
79+
expect(event.spans?.[0]?.data[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBeUndefined();
80+
});
81+
4082
describe('_enhanceKitSpan', () => {
4183
it.each([
4284
['sveltekit.resolve', 'function.sveltekit.resolve', 'auto.http.sveltekit'],

0 commit comments

Comments
 (0)