diff --git a/packages/core/src/asyncContext.ts b/packages/core/src/asyncContext.ts index 4e8fc5ac917e..82d336ded509 100644 --- a/packages/core/src/asyncContext.ts +++ b/packages/core/src/asyncContext.ts @@ -1,4 +1,4 @@ -import type { Hub, Integration } from '@sentry/types'; +import type { Integration } from '@sentry/types'; import type { Scope } from '@sentry/types'; import { GLOBAL_OBJ } from '@sentry/utils'; import type { startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan } from './tracing/trace'; @@ -10,12 +10,6 @@ import type { getActiveSpan } from './utils/spanUtils'; * Strategy used to track async context. */ export interface AsyncContextStrategy { - /** - * Gets the currently active hub. - */ - // eslint-disable-next-line deprecation/deprecation - getCurrentHub: () => Hub; - /** * Fork the isolation scope inside of the provided callback. */ diff --git a/packages/core/src/getCurrentHubShim.ts b/packages/core/src/getCurrentHubShim.ts index dd5df3e479e6..3da344fcba1a 100644 --- a/packages/core/src/getCurrentHubShim.ts +++ b/packages/core/src/getCurrentHubShim.ts @@ -70,6 +70,18 @@ export function getCurrentHubShim(): Hub { }; } +/** + * Returns the default hub instance. + * + * If a hub is already registered in the global carrier but this module + * contains a more recent version, it replaces the registered version. + * Otherwise, the currently registered hub will be returned. + * + * @deprecated Use the respective replacement method directly instead. + */ +// eslint-disable-next-line deprecation/deprecation +export const getCurrentHub = getCurrentHubShim; + /** * Sends the current Session on the scope */ diff --git a/packages/core/src/hub.ts b/packages/core/src/hub.ts index e68ff4cafc53..1bd3ec7c64b6 100644 --- a/packages/core/src/hub.ts +++ b/packages/core/src/hub.ts @@ -496,24 +496,6 @@ export class Hub implements HubInterface { } } -/** - * Returns the default hub instance. - * - * If a hub is already registered in the global carrier but this module - * contains a more recent version, it replaces the registered version. - * Otherwise, the currently registered hub will be returned. - * - * @deprecated Use the respective replacement method directly instead. - */ -// eslint-disable-next-line deprecation/deprecation -export function getCurrentHub(): HubInterface { - // Get main carrier (global for every environment) - const carrier = getMainCarrier(); - - const acs = getAsyncContextStrategy(carrier); - return acs.getCurrentHub() || getGlobalHub(); -} - /** Get the default current scope. */ export function getDefaultCurrentScope(): Scope { return getGlobalSingleton('defaultCurrentScope', () => new Scope()); @@ -586,7 +568,6 @@ function withIsolationScope(callback: (isolationScope: ScopeInterface) => T): /* eslint-disable deprecation/deprecation */ function getHubStackAsyncContextStrategy(): AsyncContextStrategy { return { - getCurrentHub: getGlobalHub, withIsolationScope, withScope, withSetScope, diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e818ab1ffb01..cf3415302314 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -30,8 +30,6 @@ export { addEventProcessor, } from './exports'; export { - // eslint-disable-next-line deprecation/deprecation - getCurrentHub, getDefaultCurrentScope, getDefaultIsolationScope, } from './hub'; @@ -107,4 +105,4 @@ export { addTracingHeadersToFetchRequest, instrumentFetchRequest } from './fetch export { trpcMiddleware } from './trpc'; // eslint-disable-next-line deprecation/deprecation -export { getCurrentHubShim } from './getCurrentHubShim'; +export { getCurrentHubShim, getCurrentHub } from './getCurrentHubShim'; diff --git a/packages/core/src/sdk.ts b/packages/core/src/sdk.ts index d4e4dafa5db7..ebe8f9a6ca22 100644 --- a/packages/core/src/sdk.ts +++ b/packages/core/src/sdk.ts @@ -1,10 +1,10 @@ -import type { Client, ClientOptions, Hub as HubInterface } from '@sentry/types'; +import type { Client, ClientOptions } from '@sentry/types'; import { consoleSandbox, logger } from '@sentry/utils'; import { getCurrentScope } from './currentScopes'; +import { getMainCarrier, getSentryCarrier } from './asyncContext'; import { DEBUG_BUILD } from './debug-build'; import type { Hub } from './hub'; -import { getCurrentHub } from './hub'; /** A class object that can instantiate Client objects. */ export type ClientClass = new (options: O) => F; @@ -44,19 +44,22 @@ export function initAndBind( */ export function setCurrentClient(client: Client): void { getCurrentScope().setClient(client); + registerClientOnGlobalHub(client); +} - // is there a hub too? +/** + * Unfortunately, we still have to manually bind the client to the "hub" set on the global + * Sentry carrier object. This is because certain scripts (e.g. our loader script) obtain + * the client via `window.__SENTRY__.hub.getClient()`. + * + * @see {@link hub.ts getGlobalHub} + */ +function registerClientOnGlobalHub(client: Client): void { // eslint-disable-next-line deprecation/deprecation - const hub = getCurrentHub(); - if (isHubClass(hub)) { + const sentryGlobal = getSentryCarrier(getMainCarrier()) as { hub?: Hub }; + // eslint-disable-next-line deprecation/deprecation + if (sentryGlobal.hub && typeof sentryGlobal.hub.getStackTop === 'function') { // eslint-disable-next-line deprecation/deprecation - const top = hub.getStackTop(); - top.client = client; + sentryGlobal.hub.getStackTop().client = client; } } - -// eslint-disable-next-line deprecation/deprecation -function isHubClass(hub: HubInterface): hub is Hub { - // eslint-disable-next-line deprecation/deprecation - return !!(hub as Hub).getStackTop; -} diff --git a/packages/core/src/trpc.ts b/packages/core/src/trpc.ts index f36722b34594..f2cc6656d62d 100644 --- a/packages/core/src/trpc.ts +++ b/packages/core/src/trpc.ts @@ -1,12 +1,9 @@ import { isThenable, normalize } from '@sentry/utils'; -import { - SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, - SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, - captureException, - setContext, - startSpanManual, -} from '.'; + import { getClient } from './currentScopes'; +import { captureException, setContext } from './exports'; +import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from './semanticAttributes'; +import { startSpanManual } from './tracing'; interface SentryTrpcMiddlewareOptions { /** Whether to include procedure inputs in reported events. Defaults to `false`. */ diff --git a/packages/opentelemetry/src/asyncContextStrategy.ts b/packages/opentelemetry/src/asyncContextStrategy.ts index 3ef7bf9ba2f2..69878d27b252 100644 --- a/packages/opentelemetry/src/asyncContextStrategy.ts +++ b/packages/opentelemetry/src/asyncContextStrategy.ts @@ -1,12 +1,7 @@ import * as api from '@opentelemetry/api'; -import { - getCurrentHubShim, - getDefaultCurrentScope, - getDefaultIsolationScope, - setAsyncContextStrategy, -} from '@sentry/core'; +import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core'; import type { withActiveSpan as defaultWithActiveSpan } from '@sentry/core'; -import type { Hub, Scope } from '@sentry/types'; +import type { Scope } from '@sentry/types'; import { SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY, @@ -40,23 +35,6 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void { }; } - // eslint-disable-next-line deprecation/deprecation - function getCurrentHub(): Hub { - // eslint-disable-next-line deprecation/deprecation - const hub = getCurrentHubShim(); - return { - ...hub, - getScope: () => { - const scopes = getScopes(); - return scopes.scope; - }, - getIsolationScope: () => { - const scopes = getScopes(); - return scopes.isolationScope; - }, - }; - } - function withScope(callback: (scope: Scope) => T): T { const ctx = api.context.active(); @@ -114,7 +92,6 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void { } setAsyncContextStrategy({ - getCurrentHub, withScope, withSetScope, withSetIsolationScope, diff --git a/packages/vercel-edge/src/async.ts b/packages/vercel-edge/src/async.ts index 8fbc8306492e..dd7432c8e959 100644 --- a/packages/vercel-edge/src/async.ts +++ b/packages/vercel-edge/src/async.ts @@ -1,10 +1,5 @@ -import { - getCurrentHubShim, - getDefaultCurrentScope, - getDefaultIsolationScope, - setAsyncContextStrategy, -} from '@sentry/core'; -import type { Hub, Scope } from '@sentry/types'; +import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core'; +import type { Scope } from '@sentry/types'; import { GLOBAL_OBJ, logger } from '@sentry/utils'; import { DEBUG_BUILD } from './debug-build'; @@ -51,23 +46,6 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void { }; } - // eslint-disable-next-line deprecation/deprecation - function getCurrentHub(): Hub { - // eslint-disable-next-line deprecation/deprecation - const hub = getCurrentHubShim(); - return { - ...hub, - getScope: () => { - const scopes = getScopes(); - return scopes.scope; - }, - getIsolationScope: () => { - const scopes = getScopes(); - return scopes.isolationScope; - }, - }; - } - function withScope(callback: (scope: Scope) => T): T { const scope = getScopes().scope.clone(); const isolationScope = getScopes().isolationScope; @@ -99,7 +77,6 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void { } setAsyncContextStrategy({ - getCurrentHub, withScope, withSetScope, withIsolationScope,