Skip to content

Commit b21aa19

Browse files
authored
ref(core): Remove scope.setSpan() and scope.getSpan() methods (#11051)
Instead, we have an internal utility for this now.
1 parent 0e9cdee commit b21aa19

File tree

23 files changed

+151
-136
lines changed

23 files changed

+151
-136
lines changed

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/backgroundtab-custom/init.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ window.Sentry = Sentry;
44

55
Sentry.init({
66
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7-
integrations: [Sentry.browserTracingIntegration({ idleTimeout: 9000, instrumentPageLoad: false })],
7+
integrations: [
8+
Sentry.browserTracingIntegration({
9+
idleTimeout: 9000,
10+
instrumentPageLoad: false,
11+
instrumentNavigation: false,
12+
}),
13+
],
814
tracesSampleRate: 1,
915
});

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/backgroundtab-custom/subject.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ document.getElementById('go-background').addEventListener('click', () => {
66
});
77

88
document.getElementById('start-span').addEventListener('click', () => {
9-
const span = Sentry.startInactiveSpan({ name: 'test-span' });
9+
const span = Sentry.startBrowserTracingNavigationSpan(Sentry.getClient(), { name: 'test-span' });
1010
window.span = span;
11-
Sentry.getCurrentScope().setSpan(span);
1211
});
1312

1413
window.getSpanJson = () => {

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/backgroundtab-custom/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ sentryTest('should finish a custom transaction when the page goes background', a
1010
}
1111

1212
const url = await getLocalTestPath({ testDir: __dirname });
13-
page.goto(url);
13+
await page.goto(url);
1414

1515
await page.locator('#start-span').click();
1616
const spanJsonBefore: SpanJSON = await page.evaluate('window.getSpanJson()');

dev-packages/e2e-tests/test-applications/nextjs-app-dir/components/span-context.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { getCurrentScope, startInactiveSpan } from '@sentry/nextjs';
3+
import { startInactiveSpan } from '@sentry/nextjs';
44
import { Span } from '@sentry/types';
55
import { PropsWithChildren, createContext, useState } from 'react';
66

@@ -29,7 +29,6 @@ export function SpanContextProvider({ children }: PropsWithChildren) {
2929
spanActive: false,
3030
start: (spanName: string) => {
3131
const span = startInactiveSpan({ name: spanName });
32-
getCurrentScope().setSpan(span);
3332
setSpan(span);
3433
},
3534
}

dev-packages/rollup-utils/plugins/bundlePlugins.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ export function makeTerserPlugin() {
124124
// For v7 backwards-compatibility we need to access txn._frozenDynamicSamplingContext
125125
// TODO (v8): Remove this reserved word
126126
'_frozenDynamicSamplingContext',
127-
// These are used to keep span relationships
127+
// These are used to keep span & scope relationships
128128
'_sentryRootSpan',
129129
'_sentryChildSpans',
130+
'_sentrySpan',
131+
'_sentryScope',
132+
'_sentryIsolationScope',
130133
],
131134
},
132135
},

packages/browser/src/index.bundle.tracing.replay.feedback.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { feedbackIntegration } from '@sentry-internal/feedback';
22
import { replayIntegration } from '@sentry-internal/replay';
3-
import { browserTracingIntegration } from '@sentry-internal/tracing';
3+
import {
4+
browserTracingIntegration,
5+
startBrowserTracingNavigationSpan,
6+
startBrowserTracingPageLoadSpan,
7+
} from '@sentry-internal/tracing';
48
import { addTracingExtensions } from '@sentry/core';
59

610
// We are patching the global object with our hub extension methods
@@ -16,6 +20,13 @@ export {
1620
getSpanDescendants,
1721
} from '@sentry/core';
1822

19-
export { feedbackIntegration, replayIntegration, browserTracingIntegration, addTracingExtensions };
23+
export {
24+
feedbackIntegration,
25+
replayIntegration,
26+
browserTracingIntegration,
27+
addTracingExtensions,
28+
startBrowserTracingNavigationSpan,
29+
startBrowserTracingPageLoadSpan,
30+
};
2031

2132
export * from './index.bundle.base';

packages/browser/src/index.bundle.tracing.replay.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { feedbackIntegrationShim } from '@sentry-internal/integration-shims';
22
import { replayIntegration } from '@sentry-internal/replay';
3-
import { browserTracingIntegration } from '@sentry-internal/tracing';
3+
import {
4+
browserTracingIntegration,
5+
startBrowserTracingNavigationSpan,
6+
startBrowserTracingPageLoadSpan,
7+
} from '@sentry-internal/tracing';
48
import { addTracingExtensions } from '@sentry/core';
59

610
// We are patching the global object with our hub extension methods
@@ -21,6 +25,8 @@ export {
2125
feedbackIntegrationShim as feedbackIntegration,
2226
browserTracingIntegration,
2327
addTracingExtensions,
28+
startBrowserTracingNavigationSpan,
29+
startBrowserTracingPageLoadSpan,
2430
};
2531

2632
export * from './index.bundle.base';

packages/browser/src/index.bundle.tracing.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// This is exported so the loader does not fail when switching off Replay
22
import { feedbackIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims';
3-
import { browserTracingIntegration } from '@sentry-internal/tracing';
3+
import {
4+
browserTracingIntegration,
5+
startBrowserTracingNavigationSpan,
6+
startBrowserTracingPageLoadSpan,
7+
} from '@sentry-internal/tracing';
48
import { addTracingExtensions } from '@sentry/core';
59

610
// We are patching the global object with our hub extension methods
@@ -21,6 +25,8 @@ export {
2125
replayIntegrationShim as replayIntegration,
2226
browserTracingIntegration,
2327
addTracingExtensions,
28+
startBrowserTracingPageLoadSpan,
29+
startBrowserTracingNavigationSpan,
2430
};
2531

2632
export * from './index.bundle.base';

packages/core/src/fetch.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,8 @@ export function addTracingHeadersToFetchRequest(
127127
}
128128
| PolymorphicRequestHeaders;
129129
},
130-
requestSpan?: Span,
130+
span?: Span,
131131
): PolymorphicRequestHeaders | undefined {
132-
// eslint-disable-next-line deprecation/deprecation
133-
const span = requestSpan || scope.getSpan();
134-
135132
const isolationScope = getIsolationScope();
136133

137134
const { traceId, spanId, sampled, dsc } = {

packages/core/src/scope.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import type {
1919
ScopeData,
2020
Session,
2121
SeverityLevel,
22-
Span,
2322
Transaction,
2423
User,
2524
} from '@sentry/types';
2625
import { dateTimestampInSeconds, isPlainObject, logger, uuid4 } from '@sentry/utils';
2726

2827
import { updateSession } from './session';
2928
import type { SentrySpan } from './tracing/sentrySpan';
29+
import { _getSpanForScope, _setSpanForScope } from './utils/spanOnScope';
3030

3131
/**
3232
* Default value for maximum number of breadcrumbs added to an event.
@@ -87,9 +87,6 @@ export class Scope implements ScopeInterface {
8787
*/
8888
protected _transactionName?: string;
8989

90-
/** Span */
91-
protected _span?: Span;
92-
9390
/** Session */
9491
protected _session?: Session;
9592

@@ -134,7 +131,6 @@ export class Scope implements ScopeInterface {
134131
newScope._contexts = { ...this._contexts };
135132
newScope._user = this._user;
136133
newScope._level = this._level;
137-
newScope._span = this._span;
138134
newScope._session = this._session;
139135
newScope._transactionName = this._transactionName;
140136
newScope._fingerprint = this._fingerprint;
@@ -145,6 +141,8 @@ export class Scope implements ScopeInterface {
145141
newScope._propagationContext = { ...this._propagationContext };
146142
newScope._client = this._client;
147143

144+
_setSpanForScope(newScope, _getSpanForScope(this));
145+
148146
return newScope;
149147
}
150148

@@ -304,33 +302,14 @@ export class Scope implements ScopeInterface {
304302
return this;
305303
}
306304

307-
/**
308-
* Sets the Span on the scope.
309-
* @param span Span
310-
* @deprecated Instead of setting a span on a scope, use `startSpan()`/`startSpanManual()` instead.
311-
*/
312-
public setSpan(span?: Span): this {
313-
this._span = span;
314-
this._notifyScopeListeners();
315-
return this;
316-
}
317-
318-
/**
319-
* Returns the `Span` if there is one.
320-
* @deprecated Use `getActiveSpan()` instead.
321-
*/
322-
public getSpan(): Span | undefined {
323-
return this._span;
324-
}
325-
326305
/**
327306
* Returns the `Transaction` attached to the scope (if there is one).
328307
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
329308
*/
330309
public getTransaction(): Transaction | undefined {
331310
// Often, this span (if it exists at all) will be a transaction, but it's not guaranteed to be. Regardless, it will
332311
// have a pointer to the currently-active transaction.
333-
const span = this._span;
312+
const span = _getSpanForScope(this);
334313

335314
// Cannot replace with getRootSpan because getRootSpan returns a span, not a transaction
336315
// Also, this method will be removed anyway.
@@ -432,11 +411,12 @@ export class Scope implements ScopeInterface {
432411
this._transactionName = undefined;
433412
this._fingerprint = undefined;
434413
this._requestSession = undefined;
435-
this._span = undefined;
436414
this._session = undefined;
437-
this._notifyScopeListeners();
415+
_setSpanForScope(this, undefined);
438416
this._attachments = [];
439417
this._propagationContext = generatePropagationContext();
418+
419+
this._notifyScopeListeners();
440420
return this;
441421
}
442422

@@ -522,7 +502,6 @@ export class Scope implements ScopeInterface {
522502
_propagationContext,
523503
_sdkProcessingMetadata,
524504
_transactionName,
525-
_span,
526505
} = this;
527506

528507
return {
@@ -538,7 +517,7 @@ export class Scope implements ScopeInterface {
538517
propagationContext: _propagationContext,
539518
sdkProcessingMetadata: _sdkProcessingMetadata,
540519
transactionName: _transactionName,
541-
span: _span,
520+
span: _getSpanForScope(this),
542521
};
543522
}
544523

packages/core/src/server-runtime-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
getDynamicSamplingContextFromClient,
2525
getDynamicSamplingContextFromSpan,
2626
} from './tracing';
27+
import { _getSpanForScope } from './utils/spanOnScope';
2728
import { getRootSpan, spanToTraceContext } from './utils/spanUtils';
2829

2930
export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
@@ -252,8 +253,7 @@ export class ServerRuntimeClient<
252253
return [undefined, undefined];
253254
}
254255

255-
// eslint-disable-next-line deprecation/deprecation
256-
const span = scope.getSpan();
256+
const span = _getSpanForScope(scope);
257257
if (span) {
258258
const rootSpan = getRootSpan(span);
259259
const samplingContext = getDynamicSamplingContextFromSpan(rootSpan);

packages/core/src/tracing/idleSpan.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getClient, getCurrentScope } from '../currentScopes';
55
import { DEBUG_BUILD } from '../debug-build';
66
import { SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON } from '../semanticAttributes';
77
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
8+
import { _setSpanForScope } from '../utils/spanOnScope';
89
import {
910
getActiveSpan,
1011
getSpanDescendants,
@@ -232,8 +233,7 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
232233
beforeSpanEnd(span);
233234
}
234235

235-
// eslint-disable-next-line deprecation/deprecation
236-
scope.setSpan(previousActiveSpan);
236+
_setSpanForScope(scope, previousActiveSpan);
237237

238238
const spanJSON = spanToJSON(span);
239239

@@ -347,8 +347,7 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
347347
function _startIdleSpan(options: StartSpanOptions): Span {
348348
const span = startInactiveSpan(options);
349349

350-
// eslint-disable-next-line deprecation/deprecation
351-
getCurrentScope().setSpan(span);
350+
_setSpanForScope(getCurrentScope(), span);
352351

353352
DEBUG_BUILD && logger.log(`Setting idle span on scope. Span ID: ${span.spanContext().spanId}`);
354353

0 commit comments

Comments
 (0)