Skip to content

Commit 411cd68

Browse files
committed
feat(core): Remove deprecated props from Span interface
Instead, in places we need it we cast to a `SentrySpan` which still has the things in place, for now.
1 parent e731837 commit 411cd68

File tree

33 files changed

+335
-356
lines changed

33 files changed

+335
-356
lines changed

packages/astro/test/server/meta.test.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import * as SentryCore from '@sentry/core';
2+
import { SentrySpan } from '@sentry/core';
3+
import type { Transaction } from '@sentry/types';
24
import { vi } from 'vitest';
35

46
import { getTracingMetaTags, isValidBaggageString } from '../../src/server/meta';
57

68
const TRACE_FLAG_SAMPLED = 0x1;
79

8-
const mockedSpan = {
9-
isRecording: () => true,
10-
spanContext: () => {
11-
return {
12-
traceId: '12345678901234567890123456789012',
13-
spanId: '1234567890123456',
14-
traceFlags: TRACE_FLAG_SAMPLED,
15-
};
16-
},
17-
transaction: {
18-
getDynamicSamplingContext: () => ({
19-
environment: 'production',
20-
}),
21-
},
22-
} as any;
10+
const mockedSpan = new SentrySpan({
11+
traceId: '12345678901234567890123456789012',
12+
spanId: '1234567890123456',
13+
sampled: true,
14+
});
15+
// eslint-disable-next-line deprecation/deprecation
16+
mockedSpan.transaction = {
17+
getDynamicSamplingContext: () => ({
18+
environment: 'production',
19+
}),
20+
} as Transaction;
2321

2422
const mockedClient = {} as any;
2523

packages/core/src/scope.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
import { dateTimestampInSeconds, isPlainObject, logger, uuid4 } from '@sentry/utils';
2727

2828
import { updateSession } from './session';
29+
import { SentrySpan, Transaction as TransactionClass } from './tracing';
2930

3031
/**
3132
* Default value for maximum number of breadcrumbs added to an event.
@@ -329,10 +330,14 @@ export class Scope implements ScopeInterface {
329330
// Often, this span (if it exists at all) will be a transaction, but it's not guaranteed to be. Regardless, it will
330331
// have a pointer to the currently-active transaction.
331332
const span = this._span;
333+
332334
// Cannot replace with getRootSpan because getRootSpan returns a span, not a transaction
333335
// Also, this method will be removed anyway.
334-
// eslint-disable-next-line deprecation/deprecation
335-
return span && span.transaction;
336+
if (span instanceof SentrySpan || span instanceof TransactionClass) {
337+
// eslint-disable-next-line deprecation/deprecation
338+
return span.transaction;
339+
}
340+
return undefined;
336341
}
337342

338343
/**

packages/core/src/tracing/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import { getActiveTransaction } from './utils';
1010

1111
let errorsInstrumented = false;
1212

13+
/** Only exposed for testing */
14+
export function _resetErrorsInstrumented(): void {
15+
errorsInstrumented = false;
16+
}
17+
1318
/**
1419
* Configures global error listeners
1520
*/

packages/core/src/tracing/sentrySpan.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {
22
Primitive,
3-
Span as SpanInterface,
3+
Span,
44
SpanAttributeValue,
55
SpanAttributes,
66
SpanContext,
@@ -62,7 +62,7 @@ export class SpanRecorder {
6262
/**
6363
* Span contains all data about a span
6464
*/
65-
export class SentrySpan implements SpanInterface {
65+
export class SentrySpan implements Span {
6666
/**
6767
* Tags for the span.
6868
* @deprecated Use `spanToJSON(span).atttributes` instead.
@@ -298,7 +298,7 @@ export class SentrySpan implements SpanInterface {
298298
*/
299299
public startChild(
300300
spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'sampled' | 'traceId' | 'parentSpanId'>>,
301-
): SpanInterface {
301+
): Span {
302302
const childSpan = new SentrySpan({
303303
...spanContext,
304304
parentSpanId: this._spanId,

packages/core/src/tracing/trace.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Hub, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';
22

33
import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
4-
54
import { getCurrentScope, getIsolationScope, withScope } from '../currentScopes';
65

76
import { DEBUG_BUILD } from '../debug-build';
@@ -10,6 +9,7 @@ import { handleCallbackErrors } from '../utils/handleCallbackErrors';
109
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
1110
import { spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
1211
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
12+
import type { SentrySpan } from './sentrySpan';
1313
import { addChildSpanToSpan, getActiveSpan, setCapturedScopesOnSpan } from './utils';
1414

1515
/**
@@ -30,7 +30,7 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
3030
// eslint-disable-next-line deprecation/deprecation
3131
const hub = getCurrentHub();
3232
// eslint-disable-next-line deprecation/deprecation
33-
const parentSpan = scope.getSpan();
33+
const parentSpan = scope.getSpan() as SentrySpan | undefined;
3434

3535
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
3636
const activeSpan = shouldSkipSpan
@@ -79,7 +79,7 @@ export function startSpanManual<T>(
7979
// eslint-disable-next-line deprecation/deprecation
8080
const hub = getCurrentHub();
8181
// eslint-disable-next-line deprecation/deprecation
82-
const parentSpan = scope.getSpan();
82+
const parentSpan = scope.getSpan() as SentrySpan | undefined;
8383

8484
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
8585
const activeSpan = shouldSkipSpan
@@ -130,8 +130,8 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
130130
const hub = getCurrentHub();
131131
const parentSpan = context.scope
132132
? // eslint-disable-next-line deprecation/deprecation
133-
context.scope.getSpan()
134-
: getActiveSpan();
133+
(context.scope.getSpan() as SentrySpan | undefined)
134+
: (getActiveSpan() as SentrySpan | undefined);
135135

136136
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
137137

@@ -264,7 +264,7 @@ function createChildSpanOrTransaction(
264264
forceTransaction,
265265
scope,
266266
}: {
267-
parentSpan: Span | undefined;
267+
parentSpan: SentrySpan | undefined;
268268
spanContext: TransactionContext;
269269
forceTransaction?: boolean;
270270
scope: Scope;

packages/core/src/utils/getRootSpan.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Span } from '@sentry/types';
2+
import type { SentrySpan } from './../tracing/sentrySpan';
23

34
/**
45
* Returns the root span of a given span.
@@ -11,5 +12,5 @@ import type { Span } from '@sentry/types';
1112
export function getRootSpan(span: Span): Span | undefined {
1213
// TODO (v8): Remove this check and just return span
1314
// eslint-disable-next-line deprecation/deprecation
14-
return span.transaction;
15+
return (span as SentrySpan).transaction ? (span as SentrySpan).transaction : undefined;
1516
}

packages/core/src/utils/prepareEvent.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,7 @@ function normalizeEvent(event: Event | null, depth: number, maxBreadth: number):
332332

333333
if (data) {
334334
// This is a bit weird, as we generally have `Span` instances here, but to be safe we do not assume so
335-
// eslint-disable-next-line deprecation/deprecation
336-
span.data = normalize(data, depth, maxBreadth);
335+
span.setAttributes(normalize(data, depth, maxBreadth));
337336
}
338337

339338
return span;

packages/core/src/utils/spanUtils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,28 @@ function ensureTimestampInSeconds(timestamp: number): number {
6262
return isMs ? timestamp / 1000 : timestamp;
6363
}
6464

65+
type SpanWithToJSON = Span & { toJSON: () => SpanJSON };
66+
6567
/**
6668
* Convert a span to a JSON representation.
6769
* Note that all fields returned here are optional and need to be guarded against.
6870
*
6971
* Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json).
7072
* This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility.
7173
* And `spanToJSON` needs the Span class from `span.ts` to check here.
72-
* TODO v8: When we remove the deprecated stuff from `span.ts`, we can remove the circular dependency again.
7374
*/
7475
export function spanToJSON(span: Span): Partial<SpanJSON> {
7576
if (spanIsSentrySpan(span)) {
7677
return span.getSpanJSON();
7778
}
7879

7980
// Fallback: We also check for `.toJSON()` here...
80-
// eslint-disable-next-line deprecation/deprecation
81-
if (typeof span.toJSON === 'function') {
82-
// eslint-disable-next-line deprecation/deprecation
83-
return span.toJSON();
81+
if (typeof (span as SpanWithToJSON).toJSON === 'function') {
82+
return (span as SpanWithToJSON).toJSON();
8483
}
8584

85+
// TODO: Also handle OTEL spans here!
86+
8687
return {};
8788
}
8889

packages/core/test/lib/hint.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { captureEvent, getCurrentScope } from '@sentry/core';
21
import { GLOBAL_OBJ } from '@sentry/utils';
32

3+
import { captureEvent, getCurrentScope } from '../../src';
44
import { initAndBind } from '../../src/sdk';
55
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
66
import { AddAttachmentTestIntegration } from '../mocks/integration';

packages/core/test/lib/integrations/requestdata.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { IncomingMessage } from 'http';
2-
import type { RequestDataIntegrationOptions } from '@sentry/core';
3-
import { setCurrentClient } from '@sentry/core';
4-
import { RequestData } from '@sentry/core';
52
import type { Event, EventProcessor } from '@sentry/types';
63
import * as sentryUtils from '@sentry/utils';
4+
import type { RequestDataIntegrationOptions } from '../../../src';
5+
import { RequestData, setCurrentClient } from '../../../src';
76

87
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
98

0 commit comments

Comments
 (0)