Skip to content

feat(core): Remove deprecated scope.applyToEvent() method #10842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ env:
${{ github.workspace }}/packages/utils/cjs
${{ github.workspace }}/packages/utils/esm

BUILD_CACHE_KEY: ${{ github.event.inputs.commit || github.sha }}
BUILD_CACHE_KEY: build-cache-${{ github.event.inputs.commit || github.sha }}
BUILD_PROFILING_NODE_CACHE_TARBALL_KEY: profiling-node-tarball-${{ github.event.inputs.commit || github.sha }}

# GH will use the first restore-key it finds that matches
Expand Down
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build:types": {
"inputs": ["production", "^production"],
"dependsOn": ["^build:types"],
"outputs": ["{projectRoot}/build/**/*.d.ts", "{projectRoot}/build/**/*.d.ts.map"]
"outputs": ["{projectRoot}/build/{types,types-ts3.8}", "{projectRoot}/build/npm/{types,types-ts3.8}"]
},
"lint": {
"inputs": ["default"],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/metrics/metric-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MeasurementUnit, Span } from '@sentry/types';
import type { MetricSummary } from '@sentry/types';
import type { Primitive } from '@sentry/types';
import { dropUndefinedKeys } from '@sentry/utils';
import { getActiveSpan } from '../tracing';
import { getActiveSpan } from '../tracing/utils';
import type { MetricType } from './types';

/**
Expand Down
33 changes: 2 additions & 31 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ import type {
} from '@sentry/types';
import { dateTimestampInSeconds, isPlainObject, logger, uuid4 } from '@sentry/utils';

import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors';
import { updateSession } from './session';
import { applyScopeDataToEvent } from './utils/applyScopeDataToEvent';

/**
* Default value for maximum number of breadcrumbs added to an event.
*/
const DEFAULT_MAX_BREADCRUMBS = 100;

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

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

/** Array of breadcrumbs. */
Expand Down Expand Up @@ -538,32 +535,6 @@ export class Scope implements ScopeInterface {
};
}

/**
* Applies data from the scope to the event and runs all event processors on it.
*
* @param event Event
* @param hint Object containing additional information about the original exception, for use by the event processors.
* @hidden
* @deprecated Use `applyScopeDataToEvent()` directly
*/
public applyToEvent(
event: Event,
hint: EventHint = {},
additionalEventProcessors: EventProcessor[] = [],
): PromiseLike<Event | null> {
applyScopeDataToEvent(event, this.getScopeData());

// TODO (v8): Update this order to be: Global > Client > Scope
const eventProcessors: EventProcessor[] = [
...additionalEventProcessors,
// eslint-disable-next-line deprecation/deprecation
...getGlobalEventProcessors(),
...this._eventProcessors,
];

return notifyEventProcessors(eventProcessors, event, hint);
}

/**
* Add data which will be accessible during event processing but won't get sent to Sentry
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type { BeforeFinishCallback } from './idletransaction';
export { SentrySpan } from './sentrySpan';
export { Transaction } from './transaction';
// eslint-disable-next-line deprecation/deprecation
export { getActiveTransaction } from './utils';
export { getActiveTransaction, getActiveSpan } from './utils';
// eslint-disable-next-line deprecation/deprecation
export { SpanStatus } from './spanstatus';
export {
Expand All @@ -13,7 +13,6 @@ export {
} from './spanstatus';
export type { SpanStatusType } from './spanstatus';
export {
getActiveSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tracing/sentrySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
spanToTraceContext,
} from '../utils/spanUtils';
import type { SpanStatusType } from './spanstatus';
import { addChildSpanToSpan } from './trace';
import { addChildSpanToSpan } from './utils';

/**
* Keeps track of finished spans for a given transaction
Expand Down
81 changes: 4 additions & 77 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { Hub, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';

import { addNonEnumerableProperty, dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
import { getDynamicSamplingContextFromSpan } from '.';
import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';

import { getCurrentScope, getIsolationScope, withScope } from '../currentScopes';

import { DEBUG_BUILD } from '../debug-build';
import { getCurrentHub } from '../hub';
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
import { spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
import { addChildSpanToSpan, getActiveSpan, setCapturedScopesOnSpan } from './utils';

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

/**
* Returns the currently active span.
*/
export function getActiveSpan(): Span | undefined {
// eslint-disable-next-line deprecation/deprecation
return getCurrentScope().getSpan();
}

interface ContinueTrace {
/**
* Continue a trace from `sentry-trace` and `baggage` values.
Expand Down Expand Up @@ -353,70 +347,3 @@ function normalizeContext(context: StartSpanOptions): TransactionContext {

return context;
}

const CHILD_SPANS_FIELD = '_sentryChildSpans';

type SpanWithPotentialChildren = Span & {
[CHILD_SPANS_FIELD]?: Set<Span>;
};

/**
* Adds an opaque child span reference to a span.
*/
export function addChildSpanToSpan(span: SpanWithPotentialChildren, childSpan: Span): void {
if (span[CHILD_SPANS_FIELD] && span[CHILD_SPANS_FIELD].size < 1000) {
span[CHILD_SPANS_FIELD].add(childSpan);
} else {
span[CHILD_SPANS_FIELD] = new Set([childSpan]);
}
}

/**
* Obtains the entire span tree, meaning a span + all of its descendants for a particular span.
*/
export function getSpanTree(span: SpanWithPotentialChildren): Span[] {
const resultSet = new Set<Span>();

function addSpanChildren(span: SpanWithPotentialChildren): void {
// This exit condition is required to not infinitely loop in case of a circular dependency.
if (resultSet.has(span)) {
return;
} else {
resultSet.add(span);
const childSpans = span[CHILD_SPANS_FIELD] ? Array.from(span[CHILD_SPANS_FIELD]) : [];
for (const childSpan of childSpans) {
addSpanChildren(childSpan);
}
}
}

addSpanChildren(span);

return Array.from(resultSet);
}

const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';
const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope';

type SpanWithScopes = Span & {
[SCOPE_ON_START_SPAN_FIELD]?: Scope;
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope;
};

/** Store the scope & isolation scope for a span, which can the be used when it is finished. */
function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void {
if (span) {
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope);
addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope);
}
}

/**
* Grabs the scope and isolation scope off a span that were active when the span was started.
*/
export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } {
return {
scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD],
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD],
};
}
2 changes: 1 addition & 1 deletion packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE
import { spanTimeInputToSeconds, spanToJSON, spanToTraceContext } from '../utils/spanUtils';
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
import { SentrySpan, SpanRecorder } from './sentrySpan';
import { getCapturedScopesOnSpan, getSpanTree } from './trace';
import { getCapturedScopesOnSpan, getSpanTree } from './utils';

/** JSDoc */
export class Transaction extends SentrySpan implements TransactionInterface {
Expand Down
80 changes: 79 additions & 1 deletion packages/core/src/tracing/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Transaction } from '@sentry/types';
import type { Span, Transaction } from '@sentry/types';
import type { Scope } from '@sentry/types';
import { addNonEnumerableProperty } from '@sentry/utils';
import { getCurrentScope } from '../currentScopes';

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

// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils
export { stripUrlQueryAndFragment } from '@sentry/utils';

/**
* Returns the currently active span.
*/
export function getActiveSpan(): Span | undefined {
// eslint-disable-next-line deprecation/deprecation
return getCurrentScope().getSpan();
}

const CHILD_SPANS_FIELD = '_sentryChildSpans';

type SpanWithPotentialChildren = Span & {
[CHILD_SPANS_FIELD]?: Set<Span>;
};

/**
* Adds an opaque child span reference to a span.
*/
export function addChildSpanToSpan(span: SpanWithPotentialChildren, childSpan: Span): void {
if (span[CHILD_SPANS_FIELD] && span[CHILD_SPANS_FIELD].size < 1000) {
span[CHILD_SPANS_FIELD].add(childSpan);
} else {
span[CHILD_SPANS_FIELD] = new Set([childSpan]);
}
}

/**
* Obtains the entire span tree, meaning a span + all of its descendants for a particular span.
*/
export function getSpanTree(span: SpanWithPotentialChildren): Span[] {
const resultSet = new Set<Span>();

function addSpanChildren(span: SpanWithPotentialChildren): void {
// This exit condition is required to not infinitely loop in case of a circular dependency.
if (resultSet.has(span)) {
return;
} else {
resultSet.add(span);
const childSpans = span[CHILD_SPANS_FIELD] ? Array.from(span[CHILD_SPANS_FIELD]) : [];
for (const childSpan of childSpans) {
addSpanChildren(childSpan);
}
}
}

addSpanChildren(span);

return Array.from(resultSet);
}

const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';
const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope';

type SpanWithScopes = Span & {
[SCOPE_ON_START_SPAN_FIELD]?: Scope;
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope;
};

/** Store the scope & isolation scope for a span, which can the be used when it is finished. */
export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void {
if (span) {
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope);
addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope);
}
}

/**
* Grabs the scope and isolation scope off a span that were active when the span was started.
*/
export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } {
return {
scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD],
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD],
};
}
4 changes: 2 additions & 2 deletions packages/types/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface ScopeData {
}

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

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

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