Skip to content

Commit d305c8d

Browse files
authored
feat(core): Deprecate getActiveTransaction() & scope.getTransaction() (#10098)
A lot to refactor for us... but for now, let's deprecate this.
1 parent a1b1929 commit d305c8d

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)) {

0 commit comments

Comments
 (0)