Skip to content

Commit 88d41ea

Browse files
authored
ref(angular): Refactor usage of startChild (#11056)
Also remove the deprecated `getActiveTransaction` export.
1 parent af90bfa commit 88d41ea

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

packages/angular/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export * from '@sentry/browser';
55
export { init } from './sdk';
66
export { createErrorHandler, SentryErrorHandler } from './errorhandler';
77
export {
8-
// eslint-disable-next-line deprecation/deprecation
9-
getActiveTransaction,
108
browserTracingIntegration,
119
TraceClassDecorator,
1210
TraceMethodDecorator,

packages/angular/src/tracing.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ import {
1010
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1111
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1212
browserTracingIntegration as originalBrowserTracingIntegration,
13+
getActiveSpan,
14+
getClient,
1315
getCurrentScope,
16+
getRootSpan,
17+
spanToJSON,
1418
startBrowserTracingNavigationSpan,
19+
startInactiveSpan,
1520
} from '@sentry/browser';
16-
import { getActiveSpan, getClient, getRootSpan, spanToJSON, startInactiveSpan } from '@sentry/core';
17-
import type { Integration, Span, Transaction } from '@sentry/types';
21+
import type { Integration, Span } from '@sentry/types';
1822
import { logger, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/utils';
1923
import type { Observable } from 'rxjs';
2024
import { Subscription } from 'rxjs';
@@ -59,16 +63,6 @@ export function _updateSpanAttributesForParametrizedUrl(route: string, span?: Sp
5963
}
6064
}
6165

62-
/**
63-
* Grabs active transaction off scope.
64-
*
65-
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
66-
*/
67-
export function getActiveTransaction(): Transaction | undefined {
68-
// eslint-disable-next-line deprecation/deprecation
69-
return getCurrentScope().getTransaction();
70-
}
71-
7266
/**
7367
* Angular's Service responsible for hooking into Angular Router and tracking current navigation process.
7468
* Creates a new transaction for every route change and measures a duration of routing process.
@@ -293,21 +287,19 @@ export function TraceClassDecorator(): ClassDecorator {
293287
let tracingSpan: Span;
294288

295289
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
296-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
297290
return target => {
298291
const originalOnInit = target.prototype.ngOnInit;
299292
// eslint-disable-next-line @typescript-eslint/no-explicit-any
300293
target.prototype.ngOnInit = function (...args: any[]): ReturnType<typeof originalOnInit> {
301-
// eslint-disable-next-line deprecation/deprecation
302-
const activeTransaction = getActiveTransaction();
303-
if (activeTransaction) {
304-
// eslint-disable-next-line deprecation/deprecation
305-
tracingSpan = activeTransaction.startChild({
306-
name: `<${target.name}>`,
307-
op: ANGULAR_INIT_OP,
308-
origin: 'auto.ui.angular.trace_class_decorator',
309-
});
310-
}
294+
tracingSpan = startInactiveSpan({
295+
onlyIfParent: true,
296+
name: `<${target.name}>`,
297+
op: ANGULAR_INIT_OP,
298+
attributes: {
299+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_class_decorator',
300+
},
301+
});
302+
311303
if (originalOnInit) {
312304
return originalOnInit.apply(this, args);
313305
}
@@ -331,24 +323,23 @@ export function TraceClassDecorator(): ClassDecorator {
331323
* Decorator function that can be used to capture a single lifecycle methods of the component.
332324
*/
333325
export function TraceMethodDecorator(): MethodDecorator {
334-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/ban-types
326+
// eslint-disable-next-line @typescript-eslint/ban-types
335327
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
336328
const originalMethod = descriptor.value;
337329
// eslint-disable-next-line @typescript-eslint/no-explicit-any
338330
descriptor.value = function (...args: any[]): ReturnType<typeof originalMethod> {
339331
const now = timestampInSeconds();
340-
// eslint-disable-next-line deprecation/deprecation
341-
const activeTransaction = getActiveTransaction();
342-
if (activeTransaction) {
343-
// eslint-disable-next-line deprecation/deprecation
344-
activeTransaction.startChild({
345-
name: `<${target.constructor.name}>`,
346-
endTimestamp: now,
347-
op: `${ANGULAR_OP}.${String(propertyKey)}`,
348-
origin: 'auto.ui.angular.trace_method_decorator',
349-
startTimestamp: now,
350-
});
351-
}
332+
333+
startInactiveSpan({
334+
onlyIfParent: true,
335+
name: `<${target.constructor.name}>`,
336+
op: `${ANGULAR_OP}.${String(propertyKey)}`,
337+
startTime: now,
338+
attributes: {
339+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_method_decorator',
340+
},
341+
}).end(now);
342+
352343
if (originalMethod) {
353344
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
354345
return originalMethod.apply(this, args);

0 commit comments

Comments
 (0)