diff --git a/packages/node-experimental/src/sdk/init.ts b/packages/node-experimental/src/sdk/init.ts index a018dbdbb435..e969b2d52966 100644 --- a/packages/node-experimental/src/sdk/init.ts +++ b/packages/node-experimental/src/sdk/init.ts @@ -45,7 +45,6 @@ function getCjsOnlyIntegrations(isCjs = typeof require !== 'undefined'): Integra /** Get the default integrations for the Node Experimental SDK. */ export function getDefaultIntegrations(options: Options): Integration[] { - // TODO return [ // Common inboundFiltersIntegration(), diff --git a/packages/opentelemetry/src/constants.ts b/packages/opentelemetry/src/constants.ts index 0826ee6cb653..0673b40b6af3 100644 --- a/packages/opentelemetry/src/constants.ts +++ b/packages/opentelemetry/src/constants.ts @@ -5,9 +5,6 @@ export const SENTRY_BAGGAGE_HEADER = 'baggage'; export const SENTRY_TRACE_STATE_DSC = 'sentry.trace'; export const SENTRY_TRACE_STATE_PARENT_SPAN_ID = 'sentry.parent_span_id'; -/** Context Key to hold a Hub. */ -export const SENTRY_HUB_CONTEXT_KEY = createContextKey('sentry_hub'); - export const SENTRY_SCOPES_CONTEXT_KEY = createContextKey('sentry_scopes'); export const SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY = createContextKey('sentry_fork_isolation_scope'); diff --git a/packages/opentelemetry/src/contextManager.ts b/packages/opentelemetry/src/contextManager.ts index 9036667c2b40..43568fb34398 100644 --- a/packages/opentelemetry/src/contextManager.ts +++ b/packages/opentelemetry/src/contextManager.ts @@ -7,8 +7,7 @@ import { SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, SENTRY_FORK_SET_SCOPE_CONTEXT_KEY, } from './constants'; -import { getCurrentHub } from './custom/getCurrentHub'; -import { getScopesFromContext, setContextOnScope, setHubOnContext, setScopesOnContext } from './utils/contextData'; +import { getScopesFromContext, setContextOnScope, setScopesOnContext } from './utils/contextData'; import { setIsSetup } from './utils/setupCheck'; /** @@ -59,25 +58,17 @@ export function wrapContextManagerClass newCurrentScope, - getIsolationScope: () => newIsolationScope, - }; - - const ctx1 = setHubOnContext(context, mockHub); - const ctx2 = setScopesOnContext(ctx1, scopes); + const ctx1 = setScopesOnContext(context, scopes); // Remove the unneeded values again - const ctx3 = ctx2 + const ctx2 = ctx1 .deleteValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) .deleteValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY) .deleteValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY); - setContextOnScope(newCurrentScope, ctx3); + setContextOnScope(newCurrentScope, ctx2); - return super.with(ctx3, fn, thisArg, ...args); + return super.with(ctx2, fn, thisArg, ...args); } } diff --git a/packages/opentelemetry/src/index.ts b/packages/opentelemetry/src/index.ts index a2aeb9163e82..3599a21f1123 100644 --- a/packages/opentelemetry/src/index.ts +++ b/packages/opentelemetry/src/index.ts @@ -7,7 +7,6 @@ export { wrapClientClass } from './custom/client'; export { getSpanKind } from './utils/getSpanKind'; export { - getSpanHub, getSpanMetadata, getSpanParent, setSpanMetadata, diff --git a/packages/opentelemetry/src/spanProcessor.ts b/packages/opentelemetry/src/spanProcessor.ts index df1f6fcc2068..ba00d69f7799 100644 --- a/packages/opentelemetry/src/spanProcessor.ts +++ b/packages/opentelemetry/src/spanProcessor.ts @@ -2,42 +2,39 @@ import type { Context } from '@opentelemetry/api'; import { ROOT_CONTEXT, trace } from '@opentelemetry/api'; import type { Span, SpanProcessor as SpanProcessorInterface } from '@opentelemetry/sdk-trace-base'; import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'; -import { getClient, getCurrentHub } from '@sentry/core'; +import { getClient, getDefaultCurrentScope, getDefaultIsolationScope } from '@sentry/core'; import { logger } from '@sentry/utils'; import { DEBUG_BUILD } from './debug-build'; import { SentrySpanExporter } from './spanExporter'; import { maybeCaptureExceptionForTimedEvent } from './utils/captureExceptionForTimedEvent'; -import { getHubFromContext } from './utils/contextData'; +import { getScopesFromContext } from './utils/contextData'; import { setIsSetup } from './utils/setupCheck'; -import { getSpanHub, setSpanHub, setSpanParent, setSpanScopes } from './utils/spanData'; +import { setSpanParent, setSpanScopes } from './utils/spanData'; function onSpanStart(span: Span, parentContext: Context): void { // This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK const parentSpan = trace.getSpan(parentContext); - const hub = getHubFromContext(parentContext); + + let scopes = getScopesFromContext(parentContext); // We need access to the parent span in order to be able to move up the span tree for breadcrumbs if (parentSpan) { setSpanParent(span, parentSpan); } - // The root context does not have a hub stored, so we check for this specifically - // We do this instead of just falling back to `getCurrentHub` to avoid attaching the wrong hub - let actualHub = hub; + // The root context does not have scopes stored, so we check for this specifically + // As fallback we attach the global scopes if (parentContext === ROOT_CONTEXT) { - // eslint-disable-next-line deprecation/deprecation - actualHub = getCurrentHub(); + scopes = { + scope: getDefaultCurrentScope(), + isolationScope: getDefaultIsolationScope(), + }; } // We need the scope at time of span creation in order to apply it to the event when the span is finished - if (actualHub) { - // eslint-disable-next-line deprecation/deprecation - const scope = actualHub.getScope(); - // eslint-disable-next-line deprecation/deprecation - const isolationScope = actualHub.getIsolationScope(); - setSpanHub(span, actualHub); - setSpanScopes(span, { scope, isolationScope }); + if (scopes) { + setSpanScopes(span, scopes); } const client = getClient(); @@ -46,10 +43,8 @@ function onSpanStart(span: Span, parentContext: Context): void { function onSpanEnd(span: Span): void { // Capture exceptions as events - // eslint-disable-next-line deprecation/deprecation - const hub = getSpanHub(span) || getCurrentHub(); span.events.forEach(event => { - maybeCaptureExceptionForTimedEvent(hub, event, span); + maybeCaptureExceptionForTimedEvent(event, span); }); const client = getClient(); diff --git a/packages/opentelemetry/src/utils/captureExceptionForTimedEvent.ts b/packages/opentelemetry/src/utils/captureExceptionForTimedEvent.ts index dcf02b671044..d67e26a21475 100644 --- a/packages/opentelemetry/src/utils/captureExceptionForTimedEvent.ts +++ b/packages/opentelemetry/src/utils/captureExceptionForTimedEvent.ts @@ -1,13 +1,13 @@ import type { Span as OtelSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; -import type { Hub } from '@sentry/types'; +import { captureException } from '@sentry/core'; import { isString } from '@sentry/utils'; /** * Maybe capture a Sentry exception for an OTEL timed event. * This will check if the event is exception-like and in that case capture it as an exception. */ -export function maybeCaptureExceptionForTimedEvent(hub: Hub, event: TimedEvent, otelSpan?: OtelSpan): void { +export function maybeCaptureExceptionForTimedEvent(event: TimedEvent, otelSpan?: OtelSpan): void { if (event.name !== 'exception') { return; } @@ -35,8 +35,7 @@ export function maybeCaptureExceptionForTimedEvent(hub: Hub, event: TimedEvent, syntheticError.name = type; } - // eslint-disable-next-line deprecation/deprecation - hub.captureException(syntheticError, { + captureException(syntheticError, { captureContext: otelSpan ? { contexts: { diff --git a/packages/opentelemetry/src/utils/contextData.ts b/packages/opentelemetry/src/utils/contextData.ts index 5cb5cac79e3e..e6e4ab0548d3 100644 --- a/packages/opentelemetry/src/utils/contextData.ts +++ b/packages/opentelemetry/src/utils/contextData.ts @@ -1,27 +1,11 @@ import type { Context } from '@opentelemetry/api'; -import type { Hub, Scope } from '@sentry/types'; +import type { Scope } from '@sentry/types'; -import { SENTRY_HUB_CONTEXT_KEY, SENTRY_SCOPES_CONTEXT_KEY } from '../constants'; +import { SENTRY_SCOPES_CONTEXT_KEY } from '../constants'; import type { CurrentScopes } from '../types'; const SCOPE_CONTEXT_MAP = new WeakMap(); -/** - * Try to get the Hub from the given OTEL context. - * This requires a Context Manager that was wrapped with getWrappedContextManager. - */ -export function getHubFromContext(context: Context): Hub | undefined { - return context.getValue(SENTRY_HUB_CONTEXT_KEY) as Hub | undefined; -} - -/** - * Set a Hub on an OTEL context.. - * This will return a forked context with the Propagation Context set. - */ -export function setHubOnContext(context: Context, hub: Hub): Context { - return context.setValue(SENTRY_HUB_CONTEXT_KEY, hub); -} - /** * Try to get the current scopes from the given OTEL context. * This requires a Context Manager that was wrapped with getWrappedContextManager. diff --git a/packages/opentelemetry/src/utils/spanData.ts b/packages/opentelemetry/src/utils/spanData.ts index 174573bd19f9..56651c1e58bc 100644 --- a/packages/opentelemetry/src/utils/spanData.ts +++ b/packages/opentelemetry/src/utils/spanData.ts @@ -1,5 +1,5 @@ import type { Span } from '@opentelemetry/api'; -import type { Hub, Scope, TransactionMetadata } from '@sentry/types'; +import type { Scope, TransactionMetadata } from '@sentry/types'; import type { AbstractSpan } from '../types'; @@ -13,20 +13,9 @@ const SpanScopes = new WeakMap< isolationScope: Scope; } >(); -const SpanHub = new WeakMap(); const SpanParent = new WeakMap(); const SpanMetadata = new WeakMap>(); -/** Set the Sentry hub on an OTEL span. */ -export function setSpanHub(span: AbstractSpan, hub: Hub): void { - SpanHub.set(span, hub); -} - -/** Get the Sentry hub of an OTEL span. */ -export function getSpanHub(span: AbstractSpan): Hub | undefined { - return SpanHub.get(span); -} - /** Set the parent OTEL span on an OTEL span. */ export function setSpanParent(span: AbstractSpan, parentSpan: Span): void { SpanParent.set(span, parentSpan); diff --git a/packages/opentelemetry/test/utils/captureExceptionForTimedEvent.test.ts b/packages/opentelemetry/test/utils/captureExceptionForTimedEvent.test.ts index 4d0c39b3a8b9..4e6103e9a7a8 100644 --- a/packages/opentelemetry/test/utils/captureExceptionForTimedEvent.test.ts +++ b/packages/opentelemetry/test/utils/captureExceptionForTimedEvent.test.ts @@ -1,22 +1,22 @@ import type { Span as OtelSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; -import type { Hub } from '@sentry/types'; +import * as SentryCore from '@sentry/core'; import { maybeCaptureExceptionForTimedEvent } from '../../src/utils/captureExceptionForTimedEvent'; describe('maybeCaptureExceptionForTimedEvent', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + it('ignores non-exception events', async () => { const event: TimedEvent = { time: [12345, 0], name: 'test event', }; - const captureException = jest.fn(); - const hub = { - captureException, - } as unknown as Hub; - - maybeCaptureExceptionForTimedEvent(hub, event); + const captureException = jest.spyOn(SentryCore, 'captureException'); + maybeCaptureExceptionForTimedEvent(event); expect(captureException).not.toHaveBeenCalled(); }); @@ -27,12 +27,8 @@ describe('maybeCaptureExceptionForTimedEvent', () => { name: 'exception', }; - const captureException = jest.fn(); - const hub = { - captureException, - } as unknown as Hub; - - maybeCaptureExceptionForTimedEvent(hub, event); + const captureException = jest.spyOn(SentryCore, 'captureException'); + maybeCaptureExceptionForTimedEvent(event); expect(captureException).not.toHaveBeenCalled(); }); @@ -46,12 +42,8 @@ describe('maybeCaptureExceptionForTimedEvent', () => { }, }; - const captureException = jest.fn(); - const hub = { - captureException, - } as unknown as Hub; - - maybeCaptureExceptionForTimedEvent(hub, event); + const captureException = jest.spyOn(SentryCore, 'captureException'); + maybeCaptureExceptionForTimedEvent(event); expect(captureException).toHaveBeenCalledTimes(1); expect(captureException).toHaveBeenCalledWith(expect.objectContaining({ message: 'test-message' }), { @@ -73,12 +65,8 @@ describe('maybeCaptureExceptionForTimedEvent', () => { }, }; - const captureException = jest.fn(); - const hub = { - captureException, - } as unknown as Hub; - - maybeCaptureExceptionForTimedEvent(hub, event); + const captureException = jest.spyOn(SentryCore, 'captureException'); + maybeCaptureExceptionForTimedEvent(event); expect(captureException).toHaveBeenCalledTimes(1); expect(captureException).toHaveBeenCalledWith( @@ -116,12 +104,8 @@ describe('maybeCaptureExceptionForTimedEvent', () => { }, } as unknown as OtelSpan; - const captureException = jest.fn(); - const hub = { - captureException, - } as unknown as Hub; - - maybeCaptureExceptionForTimedEvent(hub, event, span); + const captureException = jest.spyOn(SentryCore, 'captureException'); + maybeCaptureExceptionForTimedEvent(event, span); expect(captureException).toHaveBeenCalledTimes(1); expect(captureException).toHaveBeenCalledWith(expect.objectContaining({ message: 'test-message' }), {