Skip to content

Commit 8cca930

Browse files
committed
ref(core): Remove scope.setSpan() and scope.getSpan() methods
Instead, we have an internal utility for this now.
1 parent c75f93e commit 8cca930

File tree

16 files changed

+126
-163
lines changed

16 files changed

+126
-163
lines changed

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({ name: 'test-span' });
1010
window.span = span;
11-
Sentry.getCurrentScope().setSpan(span);
1211
});
1312

1413
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
}

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/spanUtils';
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
getDynamicSamplingContextFromClient,
2525
getDynamicSamplingContextFromSpan,
2626
} from './tracing';
27-
import { getRootSpan, spanToTraceContext } from './utils/spanUtils';
27+
import { _getSpanForScope, getRootSpan, spanToTraceContext } from './utils/spanUtils';
2828

2929
export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
3030
platform?: string;
@@ -252,8 +252,7 @@ export class ServerRuntimeClient<
252252
return [undefined, undefined];
253253
}
254254

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

packages/core/src/tracing/idleSpan.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DEBUG_BUILD } from '../debug-build';
66
import { SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON } from '../semanticAttributes';
77
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
88
import {
9+
_setSpanForScope,
910
getActiveSpan,
1011
getSpanDescendants,
1112
removeChildSpanFromSpan,
@@ -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

packages/core/src/tracing/trace.ts

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import type {
2-
ClientOptions,
3-
Hub,
4-
Scope,
5-
Span,
6-
SpanTimeInput,
7-
StartSpanOptions,
8-
TransactionContext,
9-
} from '@sentry/types';
1+
import type { ClientOptions, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';
102

113
import { propagationContextFromHeaders } from '@sentry/utils';
124
import type { AsyncContextStrategy } from '../asyncContext';
@@ -17,6 +9,8 @@ import { getAsyncContextStrategy, getCurrentHub } from '../hub';
179
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
1810
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
1911
import {
12+
_getSpanForScope,
13+
_setSpanForScope,
2014
addChildSpanToSpan,
2115
getActiveSpan,
2216
spanIsSampled,
@@ -34,13 +28,12 @@ import { setCapturedScopesOnSpan } from './utils';
3428
/**
3529
* Wraps a function with a transaction/span and finishes the span after the function is done.
3630
* The created span is the active span and will be used as parent by other spans created inside the function
37-
* and can be accessed via `Sentry.getSpan()`, as long as the function is executed while the scope is active.
31+
* and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
3832
*
3933
* If you want to create a span that is not set as active, use {@link startInactiveSpan}.
4034
*
41-
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
42-
* or you didn't set `tracesSampleRate`, this function will not generate spans
43-
* and the `span` returned from the callback will be undefined.
35+
* You'll always get a span passed to the callback,
36+
* it may just be a non-recording span if the span is not sampled or if tracing is disabled.
4437
*/
4538
export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) => T): T {
4639
const acs = getAcs();
@@ -51,23 +44,19 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
5144
const spanContext = normalizeContext(context);
5245

5346
return withScope(context.scope, scope => {
54-
// eslint-disable-next-line deprecation/deprecation
55-
const hub = getCurrentHub();
56-
// eslint-disable-next-line deprecation/deprecation
57-
const parentSpan = scope.getSpan() as SentrySpan | undefined;
47+
const parentSpan = _getSpanForScope(scope) as SentrySpan | undefined;
5848

5949
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
6050
const activeSpan = shouldSkipSpan
6151
? new SentryNonRecordingSpan()
62-
: createChildSpanOrTransaction(hub, {
52+
: createChildSpanOrTransaction({
6353
parentSpan,
6454
spanContext,
6555
forceTransaction: context.forceTransaction,
6656
scope,
6757
});
6858

69-
// eslint-disable-next-line deprecation/deprecation
70-
scope.setSpan(activeSpan);
59+
_setSpanForScope(scope, activeSpan);
7160

7261
return handleCallbackErrors(
7362
() => callback(activeSpan),
@@ -90,9 +79,8 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
9079
* The created span is the active span and will be used as parent by other spans created inside the function
9180
* and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
9281
*
93-
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
94-
* or you didn't set `tracesSampleRate`, this function will not generate spans
95-
* and the `span` returned from the callback will be undefined.
82+
* You'll always get a span passed to the callback,
83+
* it may just be a non-recording span if the span is not sampled or if tracing is disabled.
9684
*/
9785
export function startSpanManual<T>(context: StartSpanOptions, callback: (span: Span, finish: () => void) => T): T {
9886
const acs = getAcs();
@@ -103,23 +91,19 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
10391
const spanContext = normalizeContext(context);
10492

10593
return withScope(context.scope, scope => {
106-
// eslint-disable-next-line deprecation/deprecation
107-
const hub = getCurrentHub();
108-
// eslint-disable-next-line deprecation/deprecation
109-
const parentSpan = scope.getSpan() as SentrySpan | undefined;
94+
const parentSpan = _getSpanForScope(scope) as SentrySpan | undefined;
11095

11196
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
11297
const activeSpan = shouldSkipSpan
11398
? new SentryNonRecordingSpan()
114-
: createChildSpanOrTransaction(hub, {
99+
: createChildSpanOrTransaction({
115100
parentSpan,
116101
spanContext,
117102
forceTransaction: context.forceTransaction,
118103
scope,
119104
});
120105

121-
// eslint-disable-next-line deprecation/deprecation
122-
scope.setSpan(activeSpan);
106+
_setSpanForScope(scope, activeSpan);
123107

124108
function finishAndSetSpan(): void {
125109
activeSpan.end();
@@ -140,13 +124,12 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
140124

141125
/**
142126
* Creates a span. This span is not set as active, so will not get automatic instrumentation spans
143-
* as children or be able to be accessed via `Sentry.getSpan()`.
127+
* as children or be able to be accessed via `Sentry.getActiveSpan()`.
144128
*
145129
* If you want to create a span that is set as active, use {@link startSpan}.
146130
*
147-
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
148-
* or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
149-
* and the `span` returned from the callback will be undefined.
131+
* This function will always return a span,
132+
* it may just be a non-recording span if the span is not sampled or if tracing is disabled.
150133
*/
151134
export function startInactiveSpan(context: StartSpanOptions): Span {
152135
const acs = getAcs();
@@ -155,11 +138,8 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
155138
}
156139

157140
const spanContext = normalizeContext(context);
158-
// eslint-disable-next-line deprecation/deprecation
159-
const hub = getCurrentHub();
160141
const parentSpan = context.scope
161-
? // eslint-disable-next-line deprecation/deprecation
162-
(context.scope.getSpan() as SentrySpan | undefined)
142+
? (_getSpanForScope(context.scope) as SentrySpan | undefined)
163143
: (getActiveSpan() as SentrySpan | undefined);
164144

165145
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
@@ -170,7 +150,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
170150

171151
const scope = context.scope || getCurrentScope();
172152

173-
return createChildSpanOrTransaction(hub, {
153+
return createChildSpanOrTransaction({
174154
parentSpan,
175155
spanContext,
176156
forceTransaction: context.forceTransaction,
@@ -219,26 +199,22 @@ export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) =>
219199
}
220200

221201
return withScope(scope => {
222-
// eslint-disable-next-line deprecation/deprecation
223-
scope.setSpan(span || undefined);
202+
_setSpanForScope(scope, span || undefined);
224203
return callback(scope);
225204
});
226205
}
227206

228-
function createChildSpanOrTransaction(
229-
hub: Hub,
230-
{
231-
parentSpan,
232-
spanContext,
233-
forceTransaction,
234-
scope,
235-
}: {
236-
parentSpan: SentrySpan | undefined;
237-
spanContext: TransactionContext;
238-
forceTransaction?: boolean;
239-
scope: Scope;
240-
},
241-
): Span {
207+
function createChildSpanOrTransaction({
208+
parentSpan,
209+
spanContext,
210+
forceTransaction,
211+
scope,
212+
}: {
213+
parentSpan: SentrySpan | undefined;
214+
spanContext: TransactionContext;
215+
forceTransaction?: boolean;
216+
scope: Scope;
217+
}): Span {
242218
if (!hasTracingEnabled()) {
243219
return new SentryNonRecordingSpan();
244220
}

0 commit comments

Comments
 (0)