Skip to content

Commit 88cc758

Browse files
committed
feat(core): Make custom tracing methods return spans & set default op (#10633)
This should make using these much easier: * add a default `op` to the spans, so users don't need to specify them. * Return the created span (or undefined), ensuring users don't need to do all the checking for op etc. themselves. Also make some small adjustments to ember & angular instrumentation to leverage some of these changes.
1 parent 8ba1d77 commit 88cc758

File tree

5 files changed

+422
-17
lines changed

5 files changed

+422
-17
lines changed

packages/angular/src/tracing.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export class TraceService implements OnDestroy {
129129
if (!getActiveSpan()) {
130130
startBrowserTracingNavigationSpan(client, {
131131
name: strippedUrl,
132-
op: 'navigation',
133132
origin: 'auto.navigation.angular',
134133
attributes: {
135134
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { ExtendedBackburner } from '@sentry/ember/runloop';
1111
import type { Span } from '@sentry/types';
1212
import { GLOBAL_OBJ, browserPerformanceTimeOrigin, timestampInSeconds } from '@sentry/utils';
1313

14-
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
14+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
1515
import type { BrowserClient } from '..';
1616
import { getActiveSpan, startInactiveSpan } from '..';
1717
import type { EmberRouterMain, EmberSentryConfig, GlobalConfig, OwnConfig } from '../types';
@@ -115,17 +115,18 @@ export function _instrumentEmberRouter(
115115
browserTracingOptions.instrumentPageLoad !== false
116116
) {
117117
const routeInfo = routerService.recognize(url);
118-
Sentry.startBrowserTracingPageLoadSpan(client, {
118+
activeRootSpan = Sentry.startBrowserTracingPageLoadSpan(client, {
119119
name: `route:${routeInfo.name}`,
120-
op: 'pageload',
121120
origin: 'auto.pageload.ember',
121+
attributes: {
122+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
123+
},
122124
tags: {
123125
url,
124126
toRoute: routeInfo.name,
125127
'routing.instrumentation': '@sentry/ember',
126128
},
127129
});
128-
activeRootSpan = getActiveSpan();
129130
}
130131

131132
const finishActiveTransaction = (_: unknown, nextInstance: unknown): void => {
@@ -147,19 +148,19 @@ export function _instrumentEmberRouter(
147148
const { fromRoute, toRoute } = getTransitionInformation(transition, routerService);
148149
activeRootSpan?.end();
149150

150-
Sentry.startBrowserTracingNavigationSpan(client, {
151+
activeRootSpan = Sentry.startBrowserTracingNavigationSpan(client, {
151152
name: `route:${toRoute}`,
152-
op: 'navigation',
153153
origin: 'auto.navigation.ember',
154+
attributes: {
155+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
156+
},
154157
tags: {
155158
fromRoute,
156159
toRoute,
157160
'routing.instrumentation': '@sentry/ember',
158161
},
159162
});
160163

161-
activeRootSpan = getActiveSpan();
162-
163164
transitionSpan = startInactiveSpan({
164165
attributes: {
165166
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.ember',

packages/tracing-internal/src/browser/browserTracingIntegration.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* eslint-disable max-lines, complexity */
1+
/* eslint-disable max-lines */
22
import type { IdleTransaction } from '@sentry/core';
3+
import { getActiveSpan } from '@sentry/core';
34
import { getCurrentHub } from '@sentry/core';
45
import {
56
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
@@ -237,7 +238,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
237238
latestRouteName = finalContext.name;
238239
latestRouteSource = getSource(finalContext);
239240

240-
// eslint-disable-next-line deprecation/deprecation
241241
if (finalContext.sampled === false) {
242242
DEBUG_BUILD && logger.log(`[Tracing] Will not send ${finalContext.op} transaction because of beforeNavigate.`);
243243
}
@@ -316,7 +316,10 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
316316
// If there's an open transaction on the scope, we need to finish it before creating an new one.
317317
activeSpan.end();
318318
}
319-
activeSpan = _createRouteTransaction(context);
319+
activeSpan = _createRouteTransaction({
320+
op: 'navigation',
321+
...context,
322+
});
320323
});
321324

322325
client.on('startPageLoadSpan', (context: StartSpanOptions) => {
@@ -325,7 +328,10 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
325328
// If there's an open transaction on the scope, we need to finish it before creating an new one.
326329
activeSpan.end();
327330
}
328-
activeSpan = _createRouteTransaction(context);
331+
activeSpan = _createRouteTransaction({
332+
op: 'pageload',
333+
...context,
334+
});
329335
});
330336
}
331337

@@ -334,7 +340,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
334340
name: WINDOW.location.pathname,
335341
// pageload should always start at timeOrigin (and needs to be in s, not ms)
336342
startTimestamp: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined,
337-
op: 'pageload',
338343
origin: 'auto.pageload.browser',
339344
attributes: {
340345
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
@@ -363,7 +368,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
363368
startingUrl = undefined;
364369
const context: StartSpanOptions = {
365370
name: WINDOW.location.pathname,
366-
op: 'navigation',
367371
origin: 'auto.navigation.browser',
368372
attributes: {
369373
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
@@ -401,24 +405,32 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
401405
* Manually start a page load span.
402406
* This will only do something if the BrowserTracing integration has been setup.
403407
*/
404-
export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: StartSpanOptions): void {
408+
export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
405409
if (!client.emit) {
406410
return;
407411
}
408412

409413
client.emit('startPageLoadSpan', spanOptions);
414+
415+
const span = getActiveSpan();
416+
const op = span && spanToJSON(span).op;
417+
return op === 'pageload' ? span : undefined;
410418
}
411419

412420
/**
413421
* Manually start a navigation span.
414422
* This will only do something if the BrowserTracing integration has been setup.
415423
*/
416-
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): void {
424+
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
417425
if (!client.emit) {
418426
return;
419427
}
420428

421429
client.emit('startNavigationSpan', spanOptions);
430+
431+
const span = getActiveSpan();
432+
const op = span && spanToJSON(span).op;
433+
return op === 'navigation' ? span : undefined;
422434
}
423435

424436
/** Returns the value of a meta tag */

0 commit comments

Comments
 (0)