Skip to content

Commit b5829e1

Browse files
authored
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 d3c5942 commit b5829e1

File tree

4 files changed

+399
-17
lines changed

4 files changed

+399
-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';
@@ -111,17 +111,18 @@ export function _instrumentEmberRouter(
111111

112112
if (url && browserTracingOptions.instrumentPageLoad !== false) {
113113
const routeInfo = routerService.recognize(url);
114-
Sentry.startBrowserTracingPageLoadSpan(client, {
114+
activeRootSpan = Sentry.startBrowserTracingPageLoadSpan(client, {
115115
name: `route:${routeInfo.name}`,
116-
op: 'pageload',
117116
origin: 'auto.pageload.ember',
117+
attributes: {
118+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
119+
},
118120
tags: {
119121
url,
120122
toRoute: routeInfo.name,
121123
'routing.instrumentation': '@sentry/ember',
122124
},
123125
});
124-
activeRootSpan = getActiveSpan();
125126
}
126127

127128
const finishActiveTransaction = (_: unknown, nextInstance: unknown): void => {
@@ -140,19 +141,19 @@ export function _instrumentEmberRouter(
140141
const { fromRoute, toRoute } = getTransitionInformation(transition, routerService);
141142
activeRootSpan?.end();
142143

143-
Sentry.startBrowserTracingNavigationSpan(client, {
144+
activeRootSpan = Sentry.startBrowserTracingNavigationSpan(client, {
144145
name: `route:${toRoute}`,
145-
op: 'navigation',
146146
origin: 'auto.navigation.ember',
147+
attributes: {
148+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
149+
},
147150
tags: {
148151
fromRoute,
149152
toRoute,
150153
'routing.instrumentation': '@sentry/ember',
151154
},
152155
});
153156

154-
activeRootSpan = getActiveSpan();
155-
156157
transitionSpan = startInactiveSpan({
157158
attributes: {
158159
[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
}
@@ -315,7 +315,10 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
315315
// If there's an open transaction on the scope, we need to finish it before creating an new one.
316316
activeSpan.end();
317317
}
318-
activeSpan = _createRouteTransaction(context);
318+
activeSpan = _createRouteTransaction({
319+
op: 'navigation',
320+
...context,
321+
});
319322
});
320323

321324
client.on('startPageLoadSpan', (context: StartSpanOptions) => {
@@ -324,15 +327,17 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
324327
// If there's an open transaction on the scope, we need to finish it before creating an new one.
325328
activeSpan.end();
326329
}
327-
activeSpan = _createRouteTransaction(context);
330+
activeSpan = _createRouteTransaction({
331+
op: 'pageload',
332+
...context,
333+
});
328334
});
329335

330336
if (options.instrumentPageLoad) {
331337
const context: StartSpanOptions = {
332338
name: WINDOW.location.pathname,
333339
// pageload should always start at timeOrigin (and needs to be in s, not ms)
334340
startTimestamp: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined,
335-
op: 'pageload',
336341
origin: 'auto.pageload.browser',
337342
attributes: {
338343
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
@@ -361,7 +366,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
361366
startingUrl = undefined;
362367
const context: StartSpanOptions = {
363368
name: WINDOW.location.pathname,
364-
op: 'navigation',
365369
origin: 'auto.navigation.browser',
366370
attributes: {
367371
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
@@ -399,16 +403,24 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
399403
* Manually start a page load span.
400404
* This will only do something if the BrowserTracing integration has been setup.
401405
*/
402-
export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: StartSpanOptions): void {
406+
export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
403407
client.emit('startPageLoadSpan', spanOptions);
408+
409+
const span = getActiveSpan();
410+
const op = span && spanToJSON(span).op;
411+
return op === 'pageload' ? span : undefined;
404412
}
405413

406414
/**
407415
* Manually start a navigation span.
408416
* This will only do something if the BrowserTracing integration has been setup.
409417
*/
410-
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): void {
418+
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
411419
client.emit('startNavigationSpan', spanOptions);
420+
421+
const span = getActiveSpan();
422+
const op = span && spanToJSON(span).op;
423+
return op === 'navigation' ? span : undefined;
412424
}
413425

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

0 commit comments

Comments
 (0)