Skip to content

Commit a1f3cbf

Browse files
committed
feat(core): Deprecate getActiveTransaction() & scope.getTransaction()
1 parent 0cbdf67 commit a1f3cbf

File tree

52 files changed

+124
-63
lines changed

Some content is hidden

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

52 files changed

+124
-63
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.getTransaction()` and `getActiveTransaction()`
12+
13+
Instead, you should not rely on the active transaction, but just use `startSpan()` APIs, which handle this for you.
14+
1115
## Deprecate arguments for `startSpan()` APIs
1216

1317
In v8, the API to start a new span will be reduced from the currently available options.

dev-packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ app.use(Sentry.Handlers.tracingHandler());
2626
app.use(cors());
2727

2828
app.get('/test/express', (_req, res) => {
29+
// eslint-disable-next-line deprecation/deprecation
2930
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
3031
if (transaction) {
3132
// eslint-disable-next-line deprecation/deprecation

dev-packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ app.use(Sentry.Handlers.tracingHandler());
2929
app.use(cors());
3030

3131
app.get('/test/express', (_req, res) => {
32+
// eslint-disable-next-line deprecation/deprecation
3233
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
3334
if (transaction) {
3435
// eslint-disable-next-line deprecation/deprecation

packages/angular-ivy/README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,33 +215,22 @@ export class FooterComponent implements OnInit {
215215
}
216216
```
217217

218-
You can also add your own custom spans by attaching them to the current active transaction using `getActiveTransaction`
219-
helper. For example, if you'd like to track the duration of Angular boostraping process, you can do it as follows:
218+
You can also add your own custom spans via `startSpan()`. For example, if you'd like to track the duration of Angular boostraping process, you can do it as follows:
220219

221220
```javascript
222221
import { enableProdMode } from '@angular/core';
223222
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
224-
import { init, getActiveTransaction } from '@sentry/angular-ivy';
223+
import { init, startSpan } from '@sentry/angular';
225224

226225
import { AppModule } from './app/app.module';
227226

228227
// ...
229-
230-
const activeTransaction = getActiveTransaction();
231-
const boostrapSpan =
232-
activeTransaction &&
233-
activeTransaction.startChild({
234-
description: 'platform-browser-dynamic',
235-
op: 'ui.angular.bootstrap',
236-
});
237-
238-
platformBrowserDynamic()
239-
.bootstrapModule(AppModule)
240-
.then(() => console.log(`Bootstrap success`))
241-
.catch(err => console.error(err));
242-
.finally(() => {
243-
if (bootstrapSpan) {
244-
boostrapSpan.finish();
245-
}
246-
})
228+
startSpan({
229+
name: 'platform-browser-dynamic',
230+
op: 'ui.angular.bootstrap'
231+
},
232+
async () => {
233+
await platformBrowserDynamic().bootstrapModule(AppModule);
234+
}
235+
);
247236
```

packages/angular/README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,33 +215,22 @@ export class FooterComponent implements OnInit {
215215
}
216216
```
217217

218-
You can also add your own custom spans by attaching them to the current active transaction using `getActiveTransaction`
219-
helper. For example, if you'd like to track the duration of Angular boostraping process, you can do it as follows:
218+
You can also add your own custom spans via `startSpan()`. For example, if you'd like to track the duration of Angular boostraping process, you can do it as follows:
220219

221220
```javascript
222221
import { enableProdMode } from '@angular/core';
223222
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
224-
import { init, getActiveTransaction } from '@sentry/angular';
223+
import { init, startSpan } from '@sentry/angular';
225224

226225
import { AppModule } from './app/app.module';
227226

228227
// ...
229-
230-
const activeTransaction = getActiveTransaction();
231-
const boostrapSpan =
232-
activeTransaction &&
233-
activeTransaction.startChild({
234-
description: 'platform-browser-dynamic',
235-
op: 'ui.angular.bootstrap',
236-
});
237-
238-
platformBrowserDynamic()
239-
.bootstrapModule(AppModule)
240-
.then(() => console.log(`Bootstrap success`))
241-
.catch(err => console.error(err));
242-
.finally(() => {
243-
if (bootstrapSpan) {
244-
boostrapSpan.finish();
245-
}
246-
})
228+
startSpan({
229+
name: 'platform-browser-dynamic',
230+
op: 'ui.angular.bootstrap'
231+
},
232+
async () => {
233+
await platformBrowserDynamic().bootstrapModule(AppModule);
234+
}
235+
);
247236
```

packages/angular/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from '@sentry/browser';
55
export { init } from './sdk';
66
export { createErrorHandler, SentryErrorHandler } from './errorhandler';
77
export {
8+
// eslint-disable-next-line deprecation/deprecation
89
getActiveTransaction,
910
// TODO `instrumentAngularRouting` is just an alias for `routingInstrumentation`; deprecate the latter at some point
1011
instrumentAngularRouting, // new name

packages/angular/src/tracing.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ export function routingInstrumentation(
4747
export const instrumentAngularRouting = routingInstrumentation;
4848

4949
/**
50-
* Grabs active transaction off scope
50+
* Grabs active transaction off scope.
51+
*
52+
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
5153
*/
5254
export function getActiveTransaction(): Transaction | undefined {
55+
// eslint-disable-next-line deprecation/deprecation
5356
return getCurrentScope().getTransaction();
5457
}
5558

@@ -69,6 +72,7 @@ export class TraceService implements OnDestroy {
6972
}
7073

7174
const strippedUrl = stripUrlQueryAndFragment(navigationEvent.url);
75+
// eslint-disable-next-line deprecation/deprecation
7276
let activeTransaction = getActiveTransaction();
7377

7478
if (!activeTransaction && stashedStartTransactionOnLocationChange) {
@@ -116,6 +120,7 @@ export class TraceService implements OnDestroy {
116120
(event.state as unknown as RouterState & { root: ActivatedRouteSnapshot }).root,
117121
);
118122

123+
// eslint-disable-next-line deprecation/deprecation
119124
const transaction = getActiveTransaction();
120125
// TODO (v8 / #5416): revisit the source condition. Do we want to make the parameterized route the default?
121126
if (transaction && transaction.metadata.source === 'url') {
@@ -182,6 +187,7 @@ export class TraceDirective implements OnInit, AfterViewInit {
182187
this.componentName = UNKNOWN_COMPONENT;
183188
}
184189

190+
// eslint-disable-next-line deprecation/deprecation
185191
const activeTransaction = getActiveTransaction();
186192
if (activeTransaction) {
187193
// eslint-disable-next-line deprecation/deprecation
@@ -225,6 +231,7 @@ export function TraceClassDecorator(): ClassDecorator {
225231
const originalOnInit = target.prototype.ngOnInit;
226232
// eslint-disable-next-line @typescript-eslint/no-explicit-any
227233
target.prototype.ngOnInit = function (...args: any[]): ReturnType<typeof originalOnInit> {
234+
// eslint-disable-next-line deprecation/deprecation
228235
const activeTransaction = getActiveTransaction();
229236
if (activeTransaction) {
230237
// eslint-disable-next-line deprecation/deprecation
@@ -263,6 +270,7 @@ export function TraceMethodDecorator(): MethodDecorator {
263270
// eslint-disable-next-line @typescript-eslint/no-explicit-any
264271
descriptor.value = function (...args: any[]): ReturnType<typeof originalMethod> {
265272
const now = timestampInSeconds();
273+
// eslint-disable-next-line deprecation/deprecation
266274
const activeTransaction = getActiveTransaction();
267275
if (activeTransaction) {
268276
// eslint-disable-next-line deprecation/deprecation

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export {
2222
createTransport,
2323
// eslint-disable-next-line deprecation/deprecation
2424
extractTraceparentData,
25+
// eslint-disable-next-line deprecation/deprecation
2526
getActiveTransaction,
2627
getHubFromCarrier,
2728
getCurrentHub,

packages/browser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export {
4646
setMeasurement,
4747
// eslint-disable-next-line deprecation/deprecation
4848
extractTraceparentData,
49+
// eslint-disable-next-line deprecation/deprecation
4950
getActiveTransaction,
5051
spanStatusfromHttpCode,
5152
// eslint-disable-next-line deprecation/deprecation

packages/browser/src/profiling/integration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const browserProfilingIntegration: IntegrationFn = () => {
2424
setup(client) {
2525
const scope = getCurrentScope();
2626

27+
// eslint-disable-next-line deprecation/deprecation
2728
const transaction = scope.getTransaction();
2829

2930
if (transaction && isAutomatedPageLoadTransaction(transaction)) {

packages/browser/test/unit/profiling/integration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('BrowserProfilingIntegration', () => {
5050

5151
const client = Sentry.getClient<BrowserClient>();
5252

53+
// eslint-disable-next-line deprecation/deprecation
5354
const currentTransaction = Sentry.getCurrentHub().getScope().getTransaction();
5455
expect(currentTransaction?.op).toBe('pageload');
5556
currentTransaction?.end();

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export {
3939
// eslint-disable-next-line deprecation/deprecation
4040
extractTraceparentData,
4141
flush,
42+
// eslint-disable-next-line deprecation/deprecation
4243
getActiveTransaction,
4344
getHubFromCarrier,
4445
getCurrentHub,

packages/core/src/metrics/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function addToMetricsAggregator(
3030
}
3131
const { unit, tags, timestamp } = data;
3232
const { release, environment } = client.getOptions();
33+
// eslint-disable-next-line deprecation/deprecation
3334
const transaction = scope.getTransaction();
3435
const metricTags: Record<string, string> = {};
3536
if (release) {

packages/core/src/scope.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ export class Scope implements ScopeInterface {
321321
}
322322

323323
/**
324-
* @inheritDoc
324+
* Returns the `Transaction` attached to the scope (if there is one).
325+
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
325326
*/
326327
public getTransaction(): Transaction | undefined {
327328
// Often, this span (if it exists at all) will be a transaction, but it's not guaranteed to be. Regardless, it will

packages/core/src/tracing/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function registerErrorInstrumentation(): void {
2727
* If an error or unhandled promise occurs, we mark the active transaction as failed
2828
*/
2929
function errorCallback(): void {
30+
// eslint-disable-next-line deprecation/deprecation
3031
const activeTransaction = getActiveTransaction();
3132
if (activeTransaction) {
3233
const status: SpanStatusType = 'internal_error';

packages/core/src/tracing/idletransaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export class IdleTransaction extends Transaction {
196196
// if `this._onScope` is `true`, the transaction put itself on the scope when it started
197197
if (this._onScope) {
198198
const scope = this._idleHub.getScope();
199+
// eslint-disable-next-line deprecation/deprecation
199200
if (scope.getTransaction() === this) {
200201
scope.setSpan(undefined);
201202
}

packages/core/src/tracing/measurement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getActiveTransaction } from './utils';
66
* Adds a measurement to the current active transaction.
77
*/
88
export function setMeasurement(name: string, value: number, unit: MeasurementUnit): void {
9+
// eslint-disable-next-line deprecation/deprecation
910
const transaction = getActiveTransaction();
1011
if (transaction) {
1112
transaction.setMeasurement(name, value, unit);

packages/core/src/tracing/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import { extractTraceparentData as _extractTraceparentData } from '@sentry/utils
44
import type { Hub } from '../hub';
55
import { getCurrentHub } from '../hub';
66

7-
/** Grabs active transaction off scope, if any */
7+
/**
8+
* Grabs active transaction off scope.
9+
*
10+
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
11+
*/
812
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
913
const hub = maybeHub || getCurrentHub();
1014
const scope = hub.getScope();
15+
// eslint-disable-next-line deprecation/deprecation
1116
return scope.getTransaction() as T | undefined;
1217
}
1318

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export {
3838
extractTraceparentData,
3939
continueTrace,
4040
flush,
41+
// eslint-disable-next-line deprecation/deprecation
4142
getActiveTransaction,
4243
getHubFromCarrier,
4344
getCurrentHub,

packages/ember/addon/index.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { assert, warn } from '@ember/debug';
22
import type Route from '@ember/routing/route';
33
import { next } from '@ember/runloop';
44
import { getOwnConfig, isDevelopingApp, macroCondition } from '@embroider/macros';
5+
import { startSpan } from '@sentry/browser';
56
import type { BrowserOptions } from '@sentry/browser';
67
import * as Sentry from '@sentry/browser';
78
import { SDK_VERSION } from '@sentry/browser';
89
import type { Transaction } from '@sentry/types';
9-
import { GLOBAL_OBJ, timestampInSeconds } from '@sentry/utils';
10+
import { GLOBAL_OBJ } from '@sentry/utils';
1011
import Ember from 'ember';
1112

1213
import type { EmberSentryConfig, GlobalConfig, OwnConfig } from './types';
@@ -66,7 +67,13 @@ export function InitSentryForEmber(_runtimeConfig?: BrowserOptions): void {
6667
}
6768
}
6869

70+
/**
71+
* Grabs active transaction off scope.
72+
*
73+
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
74+
*/
6975
export const getActiveTransaction = (): Transaction | undefined => {
76+
// eslint-disable-next-line deprecation/deprecation
7077
return Sentry.getCurrentHub().getScope().getTransaction();
7178
};
7279

@@ -80,23 +87,16 @@ export const instrumentRoutePerformance = <T extends RouteConstructor>(BaseRoute
8087
fn: X,
8188
args: Parameters<X>,
8289
): Promise<ReturnType<X>> => {
83-
const startTimestamp = timestampInSeconds();
84-
const result = await fn(...args);
85-
86-
const currentTransaction = getActiveTransaction();
87-
if (!currentTransaction) {
88-
return result;
89-
}
90-
currentTransaction
91-
// eslint-disable-next-line deprecation/deprecation
92-
.startChild({
90+
return startSpan(
91+
{
9392
op,
94-
description,
93+
name: description,
9594
origin: 'auto.ui.ember',
96-
startTimestamp,
97-
})
98-
.end();
99-
return result;
95+
},
96+
() => {
97+
return fn(...args);
98+
},
99+
);
100100
};
101101

102102
const routeName = BaseRoute.name;

packages/ember/addon/instance-initializers/sentry-performance.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function _instrumentEmberRunloop(config: EmberSentryConfig): void {
196196
if (previousInstance) {
197197
return;
198198
}
199+
// eslint-disable-next-line deprecation/deprecation
199200
const activeTransaction = getActiveTransaction();
200201
if (!activeTransaction) {
201202
return;
@@ -227,6 +228,7 @@ function _instrumentEmberRunloop(config: EmberSentryConfig): void {
227228

228229
// Setup for next queue
229230

231+
// eslint-disable-next-line deprecation/deprecation
230232
const stillActiveTransaction = getActiveTransaction();
231233
if (!stillActiveTransaction) {
232234
return;
@@ -288,6 +290,7 @@ function processComponentRenderAfter(
288290
const componentRenderDuration = now - begin.now;
289291

290292
if (componentRenderDuration * 1000 >= minComponentDuration) {
293+
// eslint-disable-next-line deprecation/deprecation
291294
const activeTransaction = getActiveTransaction();
292295
// eslint-disable-next-line deprecation/deprecation
293296
activeTransaction?.startChild({
@@ -374,6 +377,7 @@ function _instrumentInitialLoad(config: EmberSentryConfig): void {
374377
const startTimestamp = (measure.startTime + browserPerformanceTimeOrigin) / 1000;
375378
const endTimestamp = startTimestamp + measure.duration / 1000;
376379

380+
// eslint-disable-next-line deprecation/deprecation
377381
const transaction = getActiveTransaction();
378382
// eslint-disable-next-line deprecation/deprecation
379383
const span = transaction?.startChild({

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export async function callDataFetcherTraced<F extends (...args: any[]) => Promis
194194
): Promise<ReturnType<F>> {
195195
const { parameterizedRoute, dataFetchingMethodName } = options;
196196

197+
// eslint-disable-next-line deprecation/deprecation
197198
const transaction = getActiveTransaction();
198199

199200
if (!transaction) {

packages/nextjs/src/common/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
5252
};
5353
} = await tracedGetInitialProps.apply(thisArg, args);
5454

55+
// eslint-disable-next-line deprecation/deprecation
5556
const requestTransaction = getTransactionFromRequest(req) ?? getCurrentScope().getTransaction();
5657

5758
// Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call

packages/nextjs/src/common/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function wrapErrorGetInitialPropsWithSentry(
5353
_sentryBaggage?: string;
5454
} = await tracedGetInitialProps.apply(thisArg, args);
5555

56+
// eslint-disable-next-line deprecation/deprecation
5657
const requestTransaction = getTransactionFromRequest(req) ?? getCurrentScope().getTransaction();
5758
if (requestTransaction) {
5859
errorGetInitialProps._sentryTraceData = spanToTraceHeader(requestTransaction);

0 commit comments

Comments
 (0)