Skip to content

Commit 695b2b0

Browse files
authored
feat(core): Remove deprecated scope.applyToEvent() method (#10842)
This also resolves the circular dependencies we have in core right now by moving utils around a bit.
1 parent 10da5ce commit 695b2b0

File tree

10 files changed

+93
-118
lines changed

10 files changed

+93
-118
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ env:
4848
${{ github.workspace }}/packages/utils/cjs
4949
${{ github.workspace }}/packages/utils/esm
5050
51-
BUILD_CACHE_KEY: ${{ github.event.inputs.commit || github.sha }}
51+
BUILD_CACHE_KEY: build-cache-${{ github.event.inputs.commit || github.sha }}
5252
BUILD_PROFILING_NODE_CACHE_TARBALL_KEY: profiling-node-tarball-${{ github.event.inputs.commit || github.sha }}
5353

5454
# GH will use the first restore-key it finds that matches

nx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"build:types": {
3333
"inputs": ["production", "^production"],
3434
"dependsOn": ["^build:types"],
35-
"outputs": ["{projectRoot}/build/**/*.d.ts", "{projectRoot}/build/**/*.d.ts.map"]
35+
"outputs": ["{projectRoot}/build/{types,types-ts3.8}", "{projectRoot}/build/npm/{types,types-ts3.8}"]
3636
},
3737
"lint": {
3838
"inputs": ["default"],

packages/core/src/metrics/metric-summary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { MeasurementUnit, Span } from '@sentry/types';
22
import type { MetricSummary } from '@sentry/types';
33
import type { Primitive } from '@sentry/types';
44
import { dropUndefinedKeys } from '@sentry/utils';
5-
import { getActiveSpan } from '../tracing';
5+
import { getActiveSpan } from '../tracing/utils';
66
import type { MetricType } from './types';
77

88
/**

packages/core/src/scope.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ import type {
2525
} from '@sentry/types';
2626
import { dateTimestampInSeconds, isPlainObject, logger, uuid4 } from '@sentry/utils';
2727

28-
import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors';
2928
import { updateSession } from './session';
30-
import { applyScopeDataToEvent } from './utils/applyScopeDataToEvent';
3129

3230
/**
3331
* Default value for maximum number of breadcrumbs added to an event.
3432
*/
3533
const DEFAULT_MAX_BREADCRUMBS = 100;
3634

3735
/**
38-
* Holds additional event information. {@link Scope.applyToEvent} will be
39-
* called by the client before an event will be sent.
36+
* Holds additional event information.
4037
*/
4138
export class Scope implements ScopeInterface {
4239
/** Flag if notifying is happening. */
@@ -45,7 +42,7 @@ export class Scope implements ScopeInterface {
4542
/** Callback for client to receive scope changes. */
4643
protected _scopeListeners: Array<(scope: Scope) => void>;
4744

48-
/** Callback list that will be called after {@link applyToEvent}. */
45+
/** Callback list that will be called during event processing. */
4946
protected _eventProcessors: EventProcessor[];
5047

5148
/** Array of breadcrumbs. */
@@ -538,32 +535,6 @@ export class Scope implements ScopeInterface {
538535
};
539536
}
540537

541-
/**
542-
* Applies data from the scope to the event and runs all event processors on it.
543-
*
544-
* @param event Event
545-
* @param hint Object containing additional information about the original exception, for use by the event processors.
546-
* @hidden
547-
* @deprecated Use `applyScopeDataToEvent()` directly
548-
*/
549-
public applyToEvent(
550-
event: Event,
551-
hint: EventHint = {},
552-
additionalEventProcessors: EventProcessor[] = [],
553-
): PromiseLike<Event | null> {
554-
applyScopeDataToEvent(event, this.getScopeData());
555-
556-
// TODO (v8): Update this order to be: Global > Client > Scope
557-
const eventProcessors: EventProcessor[] = [
558-
...additionalEventProcessors,
559-
// eslint-disable-next-line deprecation/deprecation
560-
...getGlobalEventProcessors(),
561-
...this._eventProcessors,
562-
];
563-
564-
return notifyEventProcessors(eventProcessors, event, hint);
565-
}
566-
567538
/**
568539
* Add data which will be accessible during event processing but won't get sent to Sentry
569540
*/

packages/core/src/tracing/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type { BeforeFinishCallback } from './idletransaction';
44
export { SentrySpan } from './sentrySpan';
55
export { Transaction } from './transaction';
66
// eslint-disable-next-line deprecation/deprecation
7-
export { getActiveTransaction } from './utils';
7+
export { getActiveTransaction, getActiveSpan } from './utils';
88
// eslint-disable-next-line deprecation/deprecation
99
export { SpanStatus } from './spanstatus';
1010
export {
@@ -13,7 +13,6 @@ export {
1313
} from './spanstatus';
1414
export type { SpanStatusType } from './spanstatus';
1515
export {
16-
getActiveSpan,
1716
startSpan,
1817
startInactiveSpan,
1918
startSpanManual,

packages/core/src/tracing/sentrySpan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
spanToTraceContext,
2626
} from '../utils/spanUtils';
2727
import type { SpanStatusType } from './spanstatus';
28-
import { addChildSpanToSpan } from './trace';
28+
import { addChildSpanToSpan } from './utils';
2929

3030
/**
3131
* Keeps track of finished spans for a given transaction

packages/core/src/tracing/trace.ts

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

3-
import { addNonEnumerableProperty, dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
4-
import { getDynamicSamplingContextFromSpan } from '.';
3+
import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
4+
55
import { getCurrentScope, getIsolationScope, withScope } from '../currentScopes';
66

77
import { DEBUG_BUILD } from '../debug-build';
88
import { getCurrentHub } from '../hub';
99
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
1010
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
1111
import { spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
12+
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
13+
import { addChildSpanToSpan, getActiveSpan, setCapturedScopesOnSpan } from './utils';
1214

1315
/**
1416
* Wraps a function with a transaction/span and finishes the span after the function is done.
@@ -152,14 +154,6 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
152154
});
153155
}
154156

155-
/**
156-
* Returns the currently active span.
157-
*/
158-
export function getActiveSpan(): Span | undefined {
159-
// eslint-disable-next-line deprecation/deprecation
160-
return getCurrentScope().getSpan();
161-
}
162-
163157
interface ContinueTrace {
164158
/**
165159
* Continue a trace from `sentry-trace` and `baggage` values.
@@ -353,70 +347,3 @@ function normalizeContext(context: StartSpanOptions): TransactionContext {
353347

354348
return context;
355349
}
356-
357-
const CHILD_SPANS_FIELD = '_sentryChildSpans';
358-
359-
type SpanWithPotentialChildren = Span & {
360-
[CHILD_SPANS_FIELD]?: Set<Span>;
361-
};
362-
363-
/**
364-
* Adds an opaque child span reference to a span.
365-
*/
366-
export function addChildSpanToSpan(span: SpanWithPotentialChildren, childSpan: Span): void {
367-
if (span[CHILD_SPANS_FIELD] && span[CHILD_SPANS_FIELD].size < 1000) {
368-
span[CHILD_SPANS_FIELD].add(childSpan);
369-
} else {
370-
span[CHILD_SPANS_FIELD] = new Set([childSpan]);
371-
}
372-
}
373-
374-
/**
375-
* Obtains the entire span tree, meaning a span + all of its descendants for a particular span.
376-
*/
377-
export function getSpanTree(span: SpanWithPotentialChildren): Span[] {
378-
const resultSet = new Set<Span>();
379-
380-
function addSpanChildren(span: SpanWithPotentialChildren): void {
381-
// This exit condition is required to not infinitely loop in case of a circular dependency.
382-
if (resultSet.has(span)) {
383-
return;
384-
} else {
385-
resultSet.add(span);
386-
const childSpans = span[CHILD_SPANS_FIELD] ? Array.from(span[CHILD_SPANS_FIELD]) : [];
387-
for (const childSpan of childSpans) {
388-
addSpanChildren(childSpan);
389-
}
390-
}
391-
}
392-
393-
addSpanChildren(span);
394-
395-
return Array.from(resultSet);
396-
}
397-
398-
const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';
399-
const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope';
400-
401-
type SpanWithScopes = Span & {
402-
[SCOPE_ON_START_SPAN_FIELD]?: Scope;
403-
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope;
404-
};
405-
406-
/** Store the scope & isolation scope for a span, which can the be used when it is finished. */
407-
function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void {
408-
if (span) {
409-
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope);
410-
addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope);
411-
}
412-
}
413-
414-
/**
415-
* Grabs the scope and isolation scope off a span that were active when the span was started.
416-
*/
417-
export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } {
418-
return {
419-
scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD],
420-
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD],
421-
};
422-
}

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE
2121
import { spanTimeInputToSeconds, spanToJSON, spanToTraceContext } from '../utils/spanUtils';
2222
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
2323
import { SentrySpan, SpanRecorder } from './sentrySpan';
24-
import { getCapturedScopesOnSpan, getSpanTree } from './trace';
24+
import { getCapturedScopesOnSpan, getSpanTree } from './utils';
2525

2626
/** JSDoc */
2727
export class Transaction extends SentrySpan implements TransactionInterface {

packages/core/src/tracing/utils.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { Transaction } from '@sentry/types';
1+
import type { Span, Transaction } from '@sentry/types';
2+
import type { Scope } from '@sentry/types';
3+
import { addNonEnumerableProperty } from '@sentry/utils';
4+
import { getCurrentScope } from '../currentScopes';
25

36
import type { Hub } from '../hub';
47
import { getCurrentHub } from '../hub';
@@ -19,3 +22,78 @@ export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T |
1922

2023
// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils
2124
export { stripUrlQueryAndFragment } from '@sentry/utils';
25+
26+
/**
27+
* Returns the currently active span.
28+
*/
29+
export function getActiveSpan(): Span | undefined {
30+
// eslint-disable-next-line deprecation/deprecation
31+
return getCurrentScope().getSpan();
32+
}
33+
34+
const CHILD_SPANS_FIELD = '_sentryChildSpans';
35+
36+
type SpanWithPotentialChildren = Span & {
37+
[CHILD_SPANS_FIELD]?: Set<Span>;
38+
};
39+
40+
/**
41+
* Adds an opaque child span reference to a span.
42+
*/
43+
export function addChildSpanToSpan(span: SpanWithPotentialChildren, childSpan: Span): void {
44+
if (span[CHILD_SPANS_FIELD] && span[CHILD_SPANS_FIELD].size < 1000) {
45+
span[CHILD_SPANS_FIELD].add(childSpan);
46+
} else {
47+
span[CHILD_SPANS_FIELD] = new Set([childSpan]);
48+
}
49+
}
50+
51+
/**
52+
* Obtains the entire span tree, meaning a span + all of its descendants for a particular span.
53+
*/
54+
export function getSpanTree(span: SpanWithPotentialChildren): Span[] {
55+
const resultSet = new Set<Span>();
56+
57+
function addSpanChildren(span: SpanWithPotentialChildren): void {
58+
// This exit condition is required to not infinitely loop in case of a circular dependency.
59+
if (resultSet.has(span)) {
60+
return;
61+
} else {
62+
resultSet.add(span);
63+
const childSpans = span[CHILD_SPANS_FIELD] ? Array.from(span[CHILD_SPANS_FIELD]) : [];
64+
for (const childSpan of childSpans) {
65+
addSpanChildren(childSpan);
66+
}
67+
}
68+
}
69+
70+
addSpanChildren(span);
71+
72+
return Array.from(resultSet);
73+
}
74+
75+
const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';
76+
const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope';
77+
78+
type SpanWithScopes = Span & {
79+
[SCOPE_ON_START_SPAN_FIELD]?: Scope;
80+
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope;
81+
};
82+
83+
/** Store the scope & isolation scope for a span, which can the be used when it is finished. */
84+
export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void {
85+
if (span) {
86+
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope);
87+
addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope);
88+
}
89+
}
90+
91+
/**
92+
* Grabs the scope and isolation scope off a span that were active when the span was started.
93+
*/
94+
export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } {
95+
return {
96+
scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD],
97+
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD],
98+
};
99+
}

packages/types/src/scope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface ScopeData {
4646
}
4747

4848
/**
49-
* Holds additional event information. {@link Scope.applyToEvent} will be called by the client before an event is sent.
49+
* Holds additional event information.
5050
*/
5151
export interface Scope {
5252
/**
@@ -61,7 +61,7 @@ export interface Scope {
6161
*/
6262
getClient<C extends Client>(): C | undefined;
6363

64-
/** Add new event processor that will be called after {@link applyToEvent}. */
64+
/** Add new event processor that will be called during event processing. */
6565
addEventProcessor(callback: EventProcessor): this;
6666

6767
/** Get the data of this scope, which is applied to an event during processing. */

0 commit comments

Comments
 (0)