Skip to content

Commit 2db7416

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

File tree

19 files changed

+170
-203
lines changed

19 files changed

+170
-203
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/dynamicSamplingContext.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,29 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
4949
const dsc = getDynamicSamplingContextFromClient(spanToJSON(span).trace_id || '', client);
5050

5151
// TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
52-
const txn = getRootSpan(span) as TransactionWithV7FrozenDsc | undefined;
53-
if (!txn) {
52+
const rootSpan = getRootSpan(span);
53+
if (!rootSpan) {
5454
return dsc;
5555
}
5656

5757
// TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
5858
// For now we need to avoid breaking users who directly created a txn with a DSC, where this field is still set.
5959
// @see Transaction class constructor
60-
const v7FrozenDsc = txn && txn._frozenDynamicSamplingContext;
60+
const v7FrozenDsc = rootSpan && (rootSpan as TransactionWithV7FrozenDsc)._frozenDynamicSamplingContext;
6161
if (v7FrozenDsc) {
6262
return v7FrozenDsc;
6363
}
6464

6565
// TODO (v8): Replace txn.metadata with txn.attributes[]
6666
// We can't do this yet because attributes aren't always set yet.
6767
// eslint-disable-next-line deprecation/deprecation
68-
const { sampleRate: maybeSampleRate } = txn.metadata;
68+
const { sampleRate: maybeSampleRate } = (rootSpan as TransactionWithV7FrozenDsc).metadata || {};
6969
if (maybeSampleRate != null) {
7070
dsc.sample_rate = `${maybeSampleRate}`;
7171
}
7272

7373
// We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII
74-
const jsonSpan = spanToJSON(txn);
74+
const jsonSpan = spanToJSON(rootSpan);
7575

7676
const source = (jsonSpan.data || {})[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
7777

@@ -80,7 +80,7 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
8080
dsc.transaction = jsonSpan.description;
8181
}
8282

83-
dsc.sampled = String(spanIsSampled(txn));
83+
dsc.sampled = String(spanIsSampled(rootSpan));
8484

8585
client.emit('createDsc', dsc);
8686

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

0 commit comments

Comments
 (0)