Skip to content

Commit 89ca41d

Browse files
mydeaLms24
andauthored
feat(core): Deprecate startTransaction() (#10073)
This finally deprecates `startTransaction()`. There are only few remaining usages in our own code base, which we can refactor as we go. I chose to leave usages in E2E/integration tests for now, we can then refactor them when we get rid of these to ensure the behavior remains the same. --------- Co-authored-by: Lukas Stracke <[email protected]>
1 parent 5def983 commit 89ca41d

File tree

48 files changed

+244
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+244
-148
lines changed

MIGRATION.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ 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 `startTransaction()`
12+
13+
In v8, the old performance API `startTransaction()` (as well as `hub.startTransaction()`) will be removed.
14+
Instead, use the new performance APIs:
15+
16+
* `startSpan()`
17+
* `startSpanManual()`
18+
* `startInactiveSpan()`
19+
20+
You can [read more about the new performance APIs here](./docs/v8-new-performance-apis.md).
21+
1122
## Deprecate `Sentry.lastEventId()` and `hub.lastEventId()`
1223

1324
`Sentry.lastEventId()` sometimes causes race conditons, so we are deprecating it in favour of the `beforeSend` callback.

dev-packages/e2e-tests/test-applications/create-next-app/pages/api/success.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Sentry from '@sentry/nextjs';
33
import type { NextApiRequest, NextApiResponse } from 'next';
44

55
export default function handler(req: NextApiRequest, res: NextApiResponse) {
6+
// eslint-disable-next-line deprecation/deprecation
67
const transaction = Sentry.startTransaction({ name: 'test-transaction', op: 'e2e-test' });
78
Sentry.getCurrentHub().getScope().setSpan(transaction);
89

dev-packages/e2e-tests/test-applications/node-express-app/src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ app.get('/test-param/:param', function (req, res) {
3434
});
3535

3636
app.get('/test-transaction', async function (req, res) {
37+
// eslint-disable-next-line deprecation/deprecation
3738
const transaction = Sentry.startTransaction({ name: 'test-transaction', op: 'e2e-test' });
3839
Sentry.getCurrentScope().setSpan(transaction);
3940

dev-packages/node-integration-tests/suites/public-api/startTransaction/basic-usage/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Sentry.init({
99
tracesSampleRate: 1.0,
1010
});
1111

12+
// eslint-disable-next-line deprecation/deprecation
1213
const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });
1314

1415
transaction.end();

dev-packages/node-integration-tests/suites/public-api/startTransaction/with-nested-spans/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Sentry.init({
88
tracesSampleRate: 1.0,
99
});
1010

11+
// eslint-disable-next-line deprecation/deprecation
1112
const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });
1213
const span_1 = transaction.startChild({
1314
op: 'span_1',

dev-packages/node-integration-tests/suites/tracing-new/apollo-graphql/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const server = new ApolloServer({
2727
resolvers,
2828
});
2929

30+
// eslint-disable-next-line deprecation/deprecation
3031
const transaction = Sentry.startTransaction({ name: 'test_transaction', op: 'transaction' });
3132

3233
Sentry.getCurrentScope().setSpan(transaction);

dev-packages/node-integration-tests/suites/tracing-new/auto-instrument/mongodb/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const client = new MongoClient(process.env.MONGO_URL || '', {
1616
});
1717

1818
async function run(): Promise<void> {
19+
// eslint-disable-next-line deprecation/deprecation
1920
const transaction = Sentry.startTransaction({
2021
name: 'Test Transaction',
2122
op: 'transaction',

dev-packages/node-integration-tests/suites/tracing-new/auto-instrument/mysql/withConnect/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ connection.connect(function (err: unknown) {
1919
}
2020
});
2121

22+
// eslint-disable-next-line deprecation/deprecation
2223
const transaction = Sentry.startTransaction({
2324
op: 'transaction',
2425
name: 'Test Transaction',

dev-packages/node-integration-tests/suites/tracing-new/auto-instrument/mysql/withoutCallback/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ connection.connect(function (err: unknown) {
1919
}
2020
});
2121

22+
// eslint-disable-next-line deprecation/deprecation
2223
const transaction = Sentry.startTransaction({
2324
op: 'transaction',
2425
name: 'Test Transaction',

dev-packages/node-integration-tests/suites/tracing-new/auto-instrument/mysql/withoutConnect/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const connection = mysql.createConnection({
1313
password: 'docker',
1414
});
1515

16+
// eslint-disable-next-line deprecation/deprecation
1617
const transaction = Sentry.startTransaction({
1718
op: 'transaction',
1819
name: 'Test Transaction',

dev-packages/node-integration-tests/suites/tracing-new/auto-instrument/pg/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Sentry.init({
88
integrations: [...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations()],
99
});
1010

11+
// eslint-disable-next-line deprecation/deprecation
1112
const transaction = Sentry.startTransaction({
1213
op: 'transaction',
1314
name: 'Test Transaction',

dev-packages/node-integration-tests/suites/tracing-new/prisma-orm/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Sentry.init({
1313
});
1414

1515
async function run(): Promise<void> {
16+
// eslint-disable-next-line deprecation/deprecation
1617
const transaction = Sentry.startTransaction({
1718
name: 'Test Transaction',
1819
op: 'transaction',

dev-packages/node-integration-tests/suites/tracing-new/tracePropagationTargets/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Sentry.init({
1010
integrations: [new Sentry.Integrations.Http({ tracing: true })],
1111
});
1212

13+
// eslint-disable-next-line deprecation/deprecation
1314
const transaction = Sentry.startTransaction({ name: 'test_transaction' });
1415

1516
Sentry.getCurrentScope().setSpan(transaction);

dev-packages/node-integration-tests/suites/tracing/apollo-graphql/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const server = new ApolloServer({
2929
resolvers,
3030
});
3131

32+
// eslint-disable-next-line deprecation/deprecation
3233
const transaction = Sentry.startTransaction({ name: 'test_transaction', op: 'transaction' });
3334

3435
Sentry.getCurrentScope().setSpan(transaction);

dev-packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const client = new MongoClient(process.env.MONGO_URL || '', {
1717
});
1818

1919
async function run(): Promise<void> {
20+
// eslint-disable-next-line deprecation/deprecation
2021
const transaction = Sentry.startTransaction({
2122
name: 'Test Transaction',
2223
op: 'transaction',

dev-packages/node-integration-tests/suites/tracing/auto-instrument/mysql/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ connection.connect(function (err: unknown) {
2020
}
2121
});
2222

23+
// eslint-disable-next-line deprecation/deprecation
2324
const transaction = Sentry.startTransaction({
2425
op: 'transaction',
2526
name: 'Test Transaction',

dev-packages/node-integration-tests/suites/tracing/auto-instrument/pg/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Sentry.init({
99
tracesSampleRate: 1.0,
1010
});
1111

12+
// eslint-disable-next-line deprecation/deprecation
1213
const transaction = Sentry.startTransaction({
1314
op: 'transaction',
1415
name: 'Test Transaction',

dev-packages/node-integration-tests/suites/tracing/prisma-orm/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Sentry.init({
1515
});
1616

1717
async function run(): Promise<void> {
18+
// eslint-disable-next-line deprecation/deprecation
1819
const transaction = Sentry.startTransaction({
1920
name: 'Test Transaction',
2021
op: 'transaction',

dev-packages/node-integration-tests/suites/tracing/tracePropagationTargets/scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Sentry.init({
1212
integrations: [new Sentry.Integrations.Http({ tracing: true })],
1313
});
1414

15+
// eslint-disable-next-line deprecation/deprecation
1516
const transaction = Sentry.startTransaction({ name: 'test_transaction' });
1617

1718
Sentry.getCurrentScope().setSpan(transaction);

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export {
3232
Hub,
3333
makeMain,
3434
Scope,
35+
// eslint-disable-next-line deprecation/deprecation
3536
startTransaction,
3637
SDK_VERSION,
3738
setContext,

packages/browser/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export {
4545
lastEventId,
4646
makeMain,
4747
Scope,
48+
// eslint-disable-next-line deprecation/deprecation
4849
startTransaction,
4950
getActiveSpan,
5051
startSpan,

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export {
5252
makeMain,
5353
runWithAsyncContext,
5454
Scope,
55+
// eslint-disable-next-line deprecation/deprecation
5556
startTransaction,
5657
SDK_VERSION,
5758
setContext,

packages/core/src/exports.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,14 @@ export function withScope<T>(callback: (scope: Scope) => T): T {
190190
* default values). See {@link Options.tracesSampler}.
191191
*
192192
* @returns The transaction which was just started
193+
*
194+
* @deprecated Use `startSpan()`, `startSpanManual()` or `startInactiveSpan()` instead.
193195
*/
194196
export function startTransaction(
195197
context: TransactionContext,
196198
customSamplingContext?: CustomSamplingContext,
197199
): ReturnType<Hub['startTransaction']> {
200+
// eslint-disable-next-line deprecation/deprecation
198201
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);
199202
}
200203

packages/core/src/hub.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,23 @@ export class Hub implements HubInterface {
440440
}
441441

442442
/**
443-
* @inheritDoc
443+
* Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation.
444+
*
445+
* A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a
446+
* new child span within the transaction or any span, call the respective `.startChild()` method.
447+
*
448+
* Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded.
449+
*
450+
* The transaction must be finished with a call to its `.end()` method, at which point the transaction with all its
451+
* finished child spans will be sent to Sentry.
452+
*
453+
* @param context Properties of the new `Transaction`.
454+
* @param customSamplingContext Information given to the transaction sampling function (along with context-dependent
455+
* default values). See {@link Options.tracesSampler}.
456+
*
457+
* @returns The transaction which was just started
458+
*
459+
* @deprecated Use `startSpan()`, `startSpanManual()` or `startInactiveSpan()` instead.
444460
*/
445461
public startTransaction(context: TransactionContext, customSamplingContext?: CustomSamplingContext): Transaction {
446462
const result = this._callExtensionMethod<Transaction>('startTransaction', context, customSamplingContext);

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export {
1919
flush,
2020
// eslint-disable-next-line deprecation/deprecation
2121
lastEventId,
22+
// eslint-disable-next-line deprecation/deprecation
2223
startTransaction,
2324
setContext,
2425
setExtra,

packages/core/src/tracing/trace.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
157157
const ctx = normalizeContext(context);
158158
const hub = getCurrentHub();
159159
const parentSpan = getActiveSpan();
160-
return parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx);
160+
return parentSpan
161+
? parentSpan.startChild(ctx)
162+
: // eslint-disable-next-line deprecation/deprecation
163+
hub.startTransaction(ctx);
161164
}
162165

163166
/**
@@ -236,7 +239,10 @@ function createChildSpanOrTransaction(
236239
if (!hasTracingEnabled()) {
237240
return undefined;
238241
}
239-
return parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx);
242+
return parentSpan
243+
? parentSpan.startChild(ctx)
244+
: // eslint-disable-next-line deprecation/deprecation
245+
hub.startTransaction(ctx);
240246
}
241247

242248
/**

packages/core/test/lib/tracing/errors.test.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserClient } from '@sentry/browser';
2-
import { Hub, addTracingExtensions, makeMain } from '@sentry/core';
2+
import { Hub, addTracingExtensions, makeMain, startInactiveSpan, startSpan } from '@sentry/core';
33
import type { HandlerDataError, HandlerDataUnhandledRejection } from '@sentry/types';
44

55
import { getDefaultBrowserClientOptions } from '../../../../tracing/test/testutils';
@@ -30,19 +30,14 @@ beforeAll(() => {
3030
});
3131

3232
describe('registerErrorHandlers()', () => {
33-
let hub: Hub;
3433
beforeEach(() => {
3534
mockAddGlobalErrorInstrumentationHandler.mockClear();
3635
mockAddGlobalUnhandledRejectionInstrumentationHandler.mockClear();
37-
const options = getDefaultBrowserClientOptions();
38-
hub = new Hub(new BrowserClient(options));
36+
const options = getDefaultBrowserClientOptions({ enableTracing: true });
37+
const hub = new Hub(new BrowserClient(options));
3938
makeMain(hub);
4039
});
4140

42-
afterEach(() => {
43-
hub.getScope().setSpan(undefined);
44-
});
45-
4641
it('registers error instrumentation', () => {
4742
registerErrorInstrumentation();
4843
expect(mockAddGlobalErrorInstrumentationHandler).toHaveBeenCalledTimes(1);
@@ -53,7 +48,8 @@ describe('registerErrorHandlers()', () => {
5348

5449
it('does not set status if transaction is not on scope', () => {
5550
registerErrorInstrumentation();
56-
const transaction = hub.startTransaction({ name: 'test' });
51+
52+
const transaction = startInactiveSpan({ name: 'test' })!;
5753
expect(transaction.status).toBe(undefined);
5854

5955
mockErrorCallback({} as HandlerDataError);
@@ -66,22 +62,19 @@ describe('registerErrorHandlers()', () => {
6662

6763
it('sets status for transaction on scope on error', () => {
6864
registerErrorInstrumentation();
69-
const transaction = hub.startTransaction({ name: 'test' });
70-
hub.getScope().setSpan(transaction);
71-
72-
mockErrorCallback({} as HandlerDataError);
73-
expect(transaction.status).toBe('internal_error');
7465

75-
transaction.end();
66+
startSpan({ name: 'test' }, span => {
67+
mockErrorCallback({} as HandlerDataError);
68+
expect(span?.status).toBe('internal_error');
69+
});
7670
});
7771

7872
it('sets status for transaction on scope on unhandledrejection', () => {
7973
registerErrorInstrumentation();
80-
const transaction = hub.startTransaction({ name: 'test' });
81-
hub.getScope().setSpan(transaction);
8274

83-
mockUnhandledRejectionCallback({});
84-
expect(transaction.status).toBe('internal_error');
85-
transaction.end();
75+
startSpan({ name: 'test' }, span => {
76+
mockUnhandledRejectionCallback({});
77+
expect(span?.status).toBe('internal_error');
78+
});
8679
});
8780
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
makeMain,
5252
runWithAsyncContext,
5353
Scope,
54+
// eslint-disable-next-line deprecation/deprecation
5455
startTransaction,
5556
SDK_VERSION,
5657
setContext,

packages/hub/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const configureScope = configureScopeCore;
118118
/**
119119
* @deprecated This export has moved to @sentry/core. The @sentry/hub package will be removed in v8.
120120
*/
121+
// eslint-disable-next-line deprecation/deprecation
121122
export const startTransaction = startTransactionCore;
122123

123124
/**

packages/nextjs/src/common/utils/wrapperUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
100100
if (platformSupportsStreaming()) {
101101
let spanToContinue: Span;
102102
if (previousSpan === undefined) {
103+
// TODO: Refactor this to use `startSpan()`
104+
// eslint-disable-next-line deprecation/deprecation
103105
const newTransaction = startTransaction(
104106
{
105107
op: 'http.server',
@@ -136,6 +138,8 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
136138
status: 'ok',
137139
});
138140
} else {
141+
// TODO: Refactor this to use `startSpan()`
142+
// eslint-disable-next-line deprecation/deprecation
139143
dataFetcherSpan = startTransaction({
140144
op: 'function.nextjs',
141145
name: `${options.dataFetchingMethodName} (${options.dataFetcherRouteName})`,

packages/nextjs/test/clientSdk.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseClient, getCurrentHub } from '@sentry/core';
22
import * as SentryReact from '@sentry/react';
3-
import { BrowserTracing, WINDOW } from '@sentry/react';
3+
import { BrowserTracing, WINDOW, getCurrentScope } from '@sentry/react';
44
import type { Integration } from '@sentry/types';
55
import type { UserIntegrationsFunction } from '@sentry/utils';
66
import { logger } from '@sentry/utils';
@@ -89,8 +89,12 @@ describe('Client init()', () => {
8989
const hub = getCurrentHub();
9090
const transportSend = jest.spyOn(hub.getClient()!.getTransport()!, 'send');
9191

92-
const transaction = hub.startTransaction({ name: '/404' });
93-
transaction.end();
92+
// Ensure we have no current span, so our next span is a transaction
93+
getCurrentScope().setSpan(undefined);
94+
95+
SentryReact.startSpan({ name: '/404' }, () => {
96+
// noop
97+
});
9498

9599
expect(transportSend).not.toHaveBeenCalled();
96100
expect(captureEvent.mock.results[0].value).toBeUndefined();

packages/nextjs/test/serverSdk.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ describe('Server init()', () => {
105105
const hub = getCurrentHub();
106106
const transportSend = jest.spyOn(hub.getClient()!.getTransport()!, 'send');
107107

108-
const transaction = hub.startTransaction({ name: '/404' });
109-
transaction.end();
108+
SentryNode.startSpan({ name: '/404' }, () => {
109+
// noop
110+
});
110111

111112
// We need to flush because the event processor pipeline is async whereas transaction.end() is sync.
112113
await SentryNode.flush();

0 commit comments

Comments
 (0)