Skip to content

Commit 45b7d13

Browse files
committed
feat(core): Decouple scope.transactionName from root spans
1 parent e0882f1 commit 45b7d13

File tree

4 files changed

+93
-18
lines changed

4 files changed

+93
-18
lines changed

packages/core/src/scope.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export class Scope implements ScopeInterface {
8181

8282
/**
8383
* Transaction Name
84+
*
85+
* IMPORTANT: The transaction name on the scope has nothing to do with root spans/transaction objects.
86+
* It's purpose is to assign a transaction to the scope that's added to non-transaction events.
8487
*/
8588
protected _transactionName?: string;
8689

@@ -278,7 +281,7 @@ export class Scope implements ScopeInterface {
278281
}
279282

280283
/**
281-
* Sets the transaction name on the scope for future events.
284+
* @inheritDoc
282285
*/
283286
public setTransactionName(name?: string): this {
284287
this._transactionName = name;

packages/core/src/utils/applyScopeDataToEvent.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export function mergeScopeData(data: ScopeData, mergeData: ScopeData): void {
3939
eventProcessors,
4040
attachments,
4141
propagationContext,
42-
// eslint-disable-next-line deprecation/deprecation
4342
transactionName,
4443
span,
4544
} = mergeData;
@@ -55,7 +54,6 @@ export function mergeScopeData(data: ScopeData, mergeData: ScopeData): void {
5554
}
5655

5756
if (transactionName) {
58-
// eslint-disable-next-line deprecation/deprecation
5957
data.transactionName = transactionName;
6058
}
6159

@@ -119,15 +117,7 @@ export function mergeArray<Prop extends 'breadcrumbs' | 'fingerprint'>(
119117
}
120118

121119
function applyDataToEvent(event: Event, data: ScopeData): void {
122-
const {
123-
extra,
124-
tags,
125-
user,
126-
contexts,
127-
level,
128-
// eslint-disable-next-line deprecation/deprecation
129-
transactionName,
130-
} = data;
120+
const { extra, tags, user, contexts, level, transactionName } = data;
131121

132122
const cleanedExtra = dropUndefinedKeys(extra);
133123
if (cleanedExtra && Object.keys(cleanedExtra).length) {
@@ -153,7 +143,7 @@ function applyDataToEvent(event: Event, data: ScopeData): void {
153143
event.level = level;
154144
}
155145

156-
if (transactionName) {
146+
if (transactionName && event.type !== 'transaction') {
157147
event.transaction = transactionName;
158148
}
159149
}
@@ -180,7 +170,7 @@ function applySpanToEvent(event: Event, span: Span): void {
180170
};
181171

182172
const transactionName = spanToJSON(rootSpan).description;
183-
if (transactionName && !event.transaction) {
173+
if (transactionName && !event.transaction && event.type === 'transaction') {
184174
event.transaction = transactionName;
185175
}
186176
}

packages/core/test/lib/utils/applyScopeDataToEvent.test.ts

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import type { Attachment, Breadcrumb, EventProcessor, ScopeData } from '@sentry/types';
2-
import { mergeAndOverwriteScopeData, mergeArray, mergeScopeData } from '../../../src/utils/applyScopeDataToEvent';
1+
import type { Attachment, Breadcrumb, Event, EventProcessor, EventType, ScopeData } from '@sentry/types';
2+
import { startInactiveSpan } from '../../../src';
3+
import {
4+
applyScopeDataToEvent,
5+
mergeAndOverwriteScopeData,
6+
mergeArray,
7+
mergeScopeData,
8+
} from '../../../src/utils/applyScopeDataToEvent';
39

410
describe('mergeArray', () => {
511
it.each([
@@ -158,3 +164,72 @@ describe('mergeScopeData', () => {
158164
});
159165
});
160166
});
167+
168+
describe('applyScopeDataToEvent', () => {
169+
it("doesn't apply scope.transactionName to transaction events", () => {
170+
const data: ScopeData = {
171+
eventProcessors: [],
172+
breadcrumbs: [],
173+
user: {},
174+
tags: {},
175+
extra: {},
176+
contexts: {},
177+
attachments: [],
178+
propagationContext: { spanId: '1', traceId: '1' },
179+
sdkProcessingMetadata: {},
180+
fingerprint: [],
181+
transactionName: 'foo',
182+
};
183+
const event: Event = { type: 'transaction', transaction: '/users/:id' };
184+
185+
applyScopeDataToEvent(event, data);
186+
187+
expect(event.transaction).toBe('/users/:id');
188+
});
189+
190+
it("doesn't the root span name to non-transaction events", () => {
191+
const data: ScopeData = {
192+
eventProcessors: [],
193+
breadcrumbs: [],
194+
user: {},
195+
tags: {},
196+
extra: {},
197+
contexts: {},
198+
attachments: [],
199+
propagationContext: { spanId: '1', traceId: '1' },
200+
sdkProcessingMetadata: {},
201+
fingerprint: [],
202+
transactionName: '/users/:id',
203+
span: startInactiveSpan({ name: 'foo' }),
204+
};
205+
const event: Event = { type: undefined };
206+
207+
applyScopeDataToEvent(event, data);
208+
209+
expect(event.transaction).toBe('/users/:id');
210+
});
211+
212+
it.each([undefined, 'profile', 'replay_event', 'feedback'])(
213+
'applies scope.transactionName to event with type %s',
214+
type => {
215+
const data: ScopeData = {
216+
eventProcessors: [],
217+
breadcrumbs: [],
218+
user: {},
219+
tags: {},
220+
extra: {},
221+
contexts: {},
222+
attachments: [],
223+
propagationContext: { spanId: '1', traceId: '1' },
224+
sdkProcessingMetadata: {},
225+
fingerprint: [],
226+
transactionName: 'foo',
227+
};
228+
const event: Event = { type: type as EventType, transaction: '/users/:id' };
229+
230+
applyScopeDataToEvent(event, data);
231+
232+
expect(event.transaction).toBe('foo');
233+
},
234+
);
235+
});

packages/types/src/scope.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export interface ScopeData {
4040
sdkProcessingMetadata: { [key: string]: unknown };
4141
fingerprint: string[];
4242
level?: SeverityLevel;
43-
/** @deprecated This will be removed in v8. */
4443
transactionName?: string;
4544
span?: Span;
4645
}
@@ -127,7 +126,15 @@ export interface Scope {
127126
setLevel(level: SeverityLevel): this;
128127

129128
/**
130-
* Sets the transaction name on the scope for future events.
129+
* Sets the transaction name on the scope so that the name of the transaction
130+
* (e.g. taken server route or page location) is attached to future events.
131+
*
132+
* IMPORTANT: Calling this function does NOT change the name of the currently active
133+
* span. If you want to change the name of the active span, use `span.updateName()`
134+
* instead.
135+
*
136+
* By default, the SDK updates the scope's transaction name automatically on sensible
137+
* occasions, such as a page navigation or when handling a new request on the server.
131138
*/
132139
setTransactionName(name?: string): this;
133140

0 commit comments

Comments
 (0)