Skip to content

Commit d41e39e

Browse files
authored
feat(core): Deprecate scope.setTransactionName() (#10113)
This can be replaced by an event processor. We only use this in a single place right now. Closes #5660
1 parent 89cef6b commit d41e39e

File tree

10 files changed

+66
-21
lines changed

10 files changed

+66
-21
lines changed

MIGRATION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ npx @sentry/migr8@latest
88

99
This will let you select which updates to run, and automatically update your code. Make sure to still review all code changes!
1010

11+
## Deprecate `scope.setTransactionName()`
12+
13+
Instead, either set this as attributes or tags, or use an event processor to set `event.transaction`.
14+
1115
## Deprecate `scope.getTransaction()` and `getActiveTransaction()`
1216

1317
Instead, you should not rely on the active transaction, but just use `startSpan()` APIs, which handle this for you.

dev-packages/browser-integration-tests/suites/replay/dsc/test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ sentryTest(
3333
await page.evaluate(() => {
3434
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
3535
scope.setUser({ id: 'user123', segment: 'segmentB' });
36-
scope.setTransactionName('testTransactionDSC');
36+
scope.addEventProcessor(event => {
37+
event.transaction = 'testTransactionDSC';
38+
return event;
39+
});
3740
});
3841

3942
const req0 = await transactionReq;
@@ -78,7 +81,10 @@ sentryTest(
7881
await page.evaluate(() => {
7982
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
8083
scope.setUser({ id: 'user123', segment: 'segmentB' });
81-
scope.setTransactionName('testTransactionDSC');
84+
scope.addEventProcessor(event => {
85+
event.transaction = 'testTransactionDSC';
86+
return event;
87+
});
8288
});
8389

8490
const req0 = await transactionReq;
@@ -135,7 +141,10 @@ sentryTest(
135141
await page.evaluate(() => {
136142
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
137143
scope.setUser({ id: 'user123', segment: 'segmentB' });
138-
scope.setTransactionName('testTransactionDSC');
144+
scope.addEventProcessor(event => {
145+
event.transaction = 'testTransactionDSC';
146+
return event;
147+
});
139148
});
140149

141150
const req0 = await transactionReq;
@@ -183,7 +192,10 @@ sentryTest(
183192
await page.evaluate(async () => {
184193
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
185194
scope.setUser({ id: 'user123', segment: 'segmentB' });
186-
scope.setTransactionName('testTransactionDSC');
195+
scope.addEventProcessor(event => {
196+
event.transaction = 'testTransactionDSC';
197+
return event;
198+
});
187199
});
188200

189201
const req0 = await transactionReq;

dev-packages/browser-integration-tests/suites/tracing/envelope-header-transaction-name/init.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ Sentry.init({
1313

1414
const scope = Sentry.getCurrentScope();
1515
scope.setUser({ id: 'user123', segment: 'segmentB' });
16-
scope.setTransactionName('testTransactionDSC');
16+
scope.addEventProcessor(event => {
17+
event.transaction = 'testTransactionDSC';
18+
return event;
19+
});
1720
scope.getTransaction().setMetadata({ source: 'custom' });

dev-packages/browser-integration-tests/suites/tracing/envelope-header/init.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ Sentry.init({
1313

1414
const scope = Sentry.getCurrentScope();
1515
scope.setUser({ id: 'user123', segment: 'segmentB' });
16-
scope.setTransactionName('testTransactionDSC');
16+
scope.addEventProcessor(event => {
17+
event.transaction = 'testTransactionDSC';
18+
return event;
19+
});

packages/core/src/scope.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ export class Scope implements ScopeInterface {
8989
// eslint-disable-next-line deprecation/deprecation
9090
protected _level?: Severity | SeverityLevel;
9191

92-
/** Transaction Name */
92+
/**
93+
* Transaction Name
94+
*/
9395
protected _transactionName?: string;
9496

9597
/** Span */
@@ -281,7 +283,8 @@ export class Scope implements ScopeInterface {
281283
}
282284

283285
/**
284-
* @inheritDoc
286+
* Sets the transaction name on the scope for future events.
287+
* @deprecated Use extra or tags instead.
285288
*/
286289
public setTransactionName(name?: string): this {
287290
this._transactionName = name;

packages/core/src/utils/applyScopeDataToEvent.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function mergeScopeData(data: ScopeData, mergeData: ScopeData): void {
3737
eventProcessors,
3838
attachments,
3939
propagationContext,
40+
// eslint-disable-next-line deprecation/deprecation
4041
transactionName,
4142
span,
4243
} = mergeData;
@@ -52,6 +53,7 @@ export function mergeScopeData(data: ScopeData, mergeData: ScopeData): void {
5253
}
5354

5455
if (transactionName) {
56+
// eslint-disable-next-line deprecation/deprecation
5557
data.transactionName = transactionName;
5658
}
5759

@@ -122,7 +124,15 @@ export function mergeArray<Prop extends 'breadcrumbs' | 'fingerprint'>(
122124
}
123125

124126
function applyDataToEvent(event: Event, data: ScopeData): void {
125-
const { extra, tags, user, contexts, level, transactionName } = data;
127+
const {
128+
extra,
129+
tags,
130+
user,
131+
contexts,
132+
level,
133+
// eslint-disable-next-line deprecation/deprecation
134+
transactionName,
135+
} = data;
126136

127137
if (extra && Object.keys(extra).length) {
128138
event.extra = { ...extra, ...event.extra };

packages/serverless/src/awslambda.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
224224
* @param context AWS Lambda context that will be used to extract some part of the data
225225
*/
226226
function enhanceScopeWithTransactionData(scope: Scope, context: Context): void {
227-
scope.setTransactionName(context.functionName);
227+
scope.addEventProcessor(event => {
228+
event.transaction = context.functionName;
229+
return event;
230+
});
228231
scope.setTag('server_name', process.env._AWS_XRAY_DAEMON_ADDRESS || process.env.SENTRY_NAME || hostname());
229232
scope.setTag('url', `awslambda:///${context.functionName}`);
230233
}

packages/serverless/test/__mocks__/@sentry/node.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const continueTrace = origSentry.continueTrace;
1111

1212
export const fakeScope = {
1313
addEventProcessor: jest.fn(),
14-
setTransactionName: jest.fn(),
1514
setTag: jest.fn(),
1615
setContext: jest.fn(),
1716
setSpan: jest.fn(),
@@ -46,7 +45,6 @@ export const resetMocks = (): void => {
4645
fakeSpan.setHttpStatus.mockClear();
4746

4847
fakeScope.addEventProcessor.mockClear();
49-
fakeScope.setTransactionName.mockClear();
5048
fakeScope.setTag.mockClear();
5149
fakeScope.setContext.mockClear();
5250
fakeScope.setSpan.mockClear();

packages/serverless/test/awslambda.test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ const fakeCallback: Callback = (err, result) => {
4242

4343
function expectScopeSettings() {
4444
// @ts-expect-error see "Why @ts-expect-error" note
45-
expect(SentryNode.fakeScope.setTransactionName).toBeCalledWith('functionName');
45+
expect(SentryNode.fakeScope.addEventProcessor).toBeCalledTimes(1);
46+
// Test than an event processor to add `transaction` is registered for the scope
47+
// @ts-expect-error see "Why @ts-expect-error" note
48+
const eventProcessor = SentryNode.fakeScope.addEventProcessor.mock.calls[0][0];
49+
const event: Event = {};
50+
eventProcessor(event);
51+
expect(event).toEqual({ transaction: 'functionName' });
52+
4653
// @ts-expect-error see "Why @ts-expect-error" note
4754
expect(SentryNode.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
4855
// @ts-expect-error see "Why @ts-expect-error" note
@@ -186,7 +193,7 @@ describe('AWSLambda', () => {
186193
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
187194

188195
// @ts-expect-error see "Why @ts-expect-error" note
189-
expect(SentryNode.fakeScope.setTransactionName).toBeCalledTimes(0);
196+
expect(SentryNode.fakeScope.addEventProcessor).toBeCalledTimes(0);
190197
// @ts-expect-error see "Why @ts-expect-error" note
191198
expect(SentryNode.fakeScope.setTag).toBeCalledTimes(0);
192199
expect(SentryNode.startSpanManual).toBeCalledTimes(0);
@@ -195,7 +202,7 @@ describe('AWSLambda', () => {
195202

196203
describe('wrapHandler() on sync handler', () => {
197204
test('successful execution', async () => {
198-
expect.assertions(9);
205+
expect.assertions(10);
199206

200207
const handler: Handler = (_event, _context, callback) => {
201208
callback(null, 42);
@@ -222,7 +229,7 @@ describe('AWSLambda', () => {
222229
});
223230

224231
test('unsuccessful execution', async () => {
225-
expect.assertions(9);
232+
expect.assertions(10);
226233

227234
const error = new Error('sorry');
228235
const handler: Handler = (_event, _context, callback) => {
@@ -301,7 +308,7 @@ describe('AWSLambda', () => {
301308
});
302309

303310
test('capture error', async () => {
304-
expect.assertions(9);
311+
expect.assertions(10);
305312

306313
const error = new Error('wat');
307314
const handler: Handler = (_event, _context, _callback) => {
@@ -338,7 +345,7 @@ describe('AWSLambda', () => {
338345

339346
describe('wrapHandler() on async handler', () => {
340347
test('successful execution', async () => {
341-
expect.assertions(9);
348+
expect.assertions(10);
342349

343350
const handler: Handler = async (_event, _context) => {
344351
return 42;
@@ -376,7 +383,7 @@ describe('AWSLambda', () => {
376383
});
377384

378385
test('capture error', async () => {
379-
expect.assertions(9);
386+
expect.assertions(10);
380387

381388
const error = new Error('wat');
382389
const handler: Handler = async (_event, _context) => {
@@ -424,7 +431,7 @@ describe('AWSLambda', () => {
424431

425432
describe('wrapHandler() on async handler with a callback method (aka incorrect usage)', () => {
426433
test('successful execution', async () => {
427-
expect.assertions(9);
434+
expect.assertions(10);
428435

429436
const handler: Handler = async (_event, _context, _callback) => {
430437
return 42;
@@ -462,7 +469,7 @@ describe('AWSLambda', () => {
462469
});
463470

464471
test('capture error', async () => {
465-
expect.assertions(9);
472+
expect.assertions(10);
466473

467474
const error = new Error('wat');
468475
const handler: Handler = async (_event, _context, _callback) => {

packages/types/src/scope.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ScopeData {
4141
sdkProcessingMetadata: { [key: string]: unknown };
4242
fingerprint: string[];
4343
level?: SeverityLevel;
44+
/** @deprecated This will be removed in v8. */
4445
transactionName?: string;
4546
span?: Span;
4647
}
@@ -125,6 +126,7 @@ export interface Scope {
125126

126127
/**
127128
* Sets the transaction name on the scope for future events.
129+
* @deprecated Use extra or tags instead.
128130
*/
129131
setTransactionName(name?: string): this;
130132

0 commit comments

Comments
 (0)