Skip to content

feat(core): Deprecate scope.setTransactionName() #10113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ npx @sentry/migr8@latest

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

## Deprecate `scope.setTransactionName()`

Instead, either set this as attributes or tags, or use an event processor to set `event.transaction`.

## Deprecate `scope.getTransaction()` and `getActiveTransaction()`

Instead, you should not rely on the active transaction, but just use `startSpan()` APIs, which handle this for you.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ sentryTest(
await page.evaluate(() => {
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
scope.setUser({ id: 'user123', segment: 'segmentB' });
scope.setTransactionName('testTransactionDSC');
scope.addEventProcessor(event => {
event.transaction = 'testTransactionDSC';
return event;
});
});

const req0 = await transactionReq;
Expand Down Expand Up @@ -78,7 +81,10 @@ sentryTest(
await page.evaluate(() => {
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
scope.setUser({ id: 'user123', segment: 'segmentB' });
scope.setTransactionName('testTransactionDSC');
scope.addEventProcessor(event => {
event.transaction = 'testTransactionDSC';
return event;
});
});

const req0 = await transactionReq;
Expand Down Expand Up @@ -135,7 +141,10 @@ sentryTest(
await page.evaluate(() => {
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
scope.setUser({ id: 'user123', segment: 'segmentB' });
scope.setTransactionName('testTransactionDSC');
scope.addEventProcessor(event => {
event.transaction = 'testTransactionDSC';
return event;
});
});

const req0 = await transactionReq;
Expand Down Expand Up @@ -183,7 +192,10 @@ sentryTest(
await page.evaluate(async () => {
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
scope.setUser({ id: 'user123', segment: 'segmentB' });
scope.setTransactionName('testTransactionDSC');
scope.addEventProcessor(event => {
event.transaction = 'testTransactionDSC';
return event;
});
});

const req0 = await transactionReq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ Sentry.init({

const scope = Sentry.getCurrentScope();
scope.setUser({ id: 'user123', segment: 'segmentB' });
scope.setTransactionName('testTransactionDSC');
scope.addEventProcessor(event => {
event.transaction = 'testTransactionDSC';
return event;
});
scope.getTransaction().setMetadata({ source: 'custom' });
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ Sentry.init({

const scope = Sentry.getCurrentScope();
scope.setUser({ id: 'user123', segment: 'segmentB' });
scope.setTransactionName('testTransactionDSC');
scope.addEventProcessor(event => {
event.transaction = 'testTransactionDSC';
return event;
});
7 changes: 5 additions & 2 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export class Scope implements ScopeInterface {
// eslint-disable-next-line deprecation/deprecation
protected _level?: Severity | SeverityLevel;

/** Transaction Name */
/**
* Transaction Name
*/
protected _transactionName?: string;

/** Span */
Expand Down Expand Up @@ -281,7 +283,8 @@ export class Scope implements ScopeInterface {
}

/**
* @inheritDoc
* Sets the transaction name on the scope for future events.
* @deprecated Use extra or tags instead.
*/
public setTransactionName(name?: string): this {
this._transactionName = name;
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/utils/applyScopeDataToEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function mergeScopeData(data: ScopeData, mergeData: ScopeData): void {
eventProcessors,
attachments,
propagationContext,
// eslint-disable-next-line deprecation/deprecation
transactionName,
span,
} = mergeData;
Expand All @@ -52,6 +53,7 @@ export function mergeScopeData(data: ScopeData, mergeData: ScopeData): void {
}

if (transactionName) {
// eslint-disable-next-line deprecation/deprecation
data.transactionName = transactionName;
}

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

function applyDataToEvent(event: Event, data: ScopeData): void {
const { extra, tags, user, contexts, level, transactionName } = data;
const {
extra,
tags,
user,
contexts,
level,
// eslint-disable-next-line deprecation/deprecation
transactionName,
} = data;

if (extra && Object.keys(extra).length) {
event.extra = { ...extra, ...event.extra };
Expand Down
5 changes: 4 additions & 1 deletion packages/serverless/src/awslambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
* @param context AWS Lambda context that will be used to extract some part of the data
*/
function enhanceScopeWithTransactionData(scope: Scope, context: Context): void {
scope.setTransactionName(context.functionName);
scope.addEventProcessor(event => {
event.transaction = context.functionName;
return event;
});
scope.setTag('server_name', process.env._AWS_XRAY_DAEMON_ADDRESS || process.env.SENTRY_NAME || hostname());
scope.setTag('url', `awslambda:///${context.functionName}`);
}
Expand Down
2 changes: 0 additions & 2 deletions packages/serverless/test/__mocks__/@sentry/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const continueTrace = origSentry.continueTrace;

export const fakeScope = {
addEventProcessor: jest.fn(),
setTransactionName: jest.fn(),
setTag: jest.fn(),
setContext: jest.fn(),
setSpan: jest.fn(),
Expand Down Expand Up @@ -46,7 +45,6 @@ export const resetMocks = (): void => {
fakeSpan.setHttpStatus.mockClear();

fakeScope.addEventProcessor.mockClear();
fakeScope.setTransactionName.mockClear();
fakeScope.setTag.mockClear();
fakeScope.setContext.mockClear();
fakeScope.setSpan.mockClear();
Expand Down
25 changes: 16 additions & 9 deletions packages/serverless/test/awslambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ const fakeCallback: Callback = (err, result) => {

function expectScopeSettings() {
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeScope.setTransactionName).toBeCalledWith('functionName');
expect(SentryNode.fakeScope.addEventProcessor).toBeCalledTimes(1);
// Test than an event processor to add `transaction` is registered for the scope
// @ts-expect-error see "Why @ts-expect-error" note
const eventProcessor = SentryNode.fakeScope.addEventProcessor.mock.calls[0][0];
const event: Event = {};
eventProcessor(event);
expect(event).toEqual({ transaction: 'functionName' });

// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
// @ts-expect-error see "Why @ts-expect-error" note
Expand Down Expand Up @@ -186,7 +193,7 @@ describe('AWSLambda', () => {
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);

// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeScope.setTransactionName).toBeCalledTimes(0);
expect(SentryNode.fakeScope.addEventProcessor).toBeCalledTimes(0);
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeScope.setTag).toBeCalledTimes(0);
expect(SentryNode.startSpanManual).toBeCalledTimes(0);
Expand All @@ -195,7 +202,7 @@ describe('AWSLambda', () => {

describe('wrapHandler() on sync handler', () => {
test('successful execution', async () => {
expect.assertions(9);
expect.assertions(10);

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

test('unsuccessful execution', async () => {
expect.assertions(9);
expect.assertions(10);

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

test('capture error', async () => {
expect.assertions(9);
expect.assertions(10);

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

describe('wrapHandler() on async handler', () => {
test('successful execution', async () => {
expect.assertions(9);
expect.assertions(10);

const handler: Handler = async (_event, _context) => {
return 42;
Expand Down Expand Up @@ -376,7 +383,7 @@ describe('AWSLambda', () => {
});

test('capture error', async () => {
expect.assertions(9);
expect.assertions(10);

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

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

const handler: Handler = async (_event, _context, _callback) => {
return 42;
Expand Down Expand Up @@ -462,7 +469,7 @@ describe('AWSLambda', () => {
});

test('capture error', async () => {
expect.assertions(9);
expect.assertions(10);

const error = new Error('wat');
const handler: Handler = async (_event, _context, _callback) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface ScopeData {
sdkProcessingMetadata: { [key: string]: unknown };
fingerprint: string[];
level?: SeverityLevel;
/** @deprecated This will be removed in v8. */
transactionName?: string;
span?: Span;
}
Expand Down Expand Up @@ -125,6 +126,7 @@ export interface Scope {

/**
* Sets the transaction name on the scope for future events.
* @deprecated Use extra or tags instead.
*/
setTransactionName(name?: string): this;

Expand Down