From 82f5428716033737d6771e5980966c73878f142a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 12 Apr 2024 15:49:07 +0200 Subject: [PATCH 1/6] feat(core): Remove `getCurrentHub` from `AsyncContextStrategy` --- packages/core/src/asyncContext.ts | 8 +----- packages/core/src/hub.ts | 10 ++----- packages/core/src/sdk.ts | 19 +------------ .../opentelemetry/src/asyncContextStrategy.ts | 18 ------------- packages/vercel-edge/src/async.ts | 27 ++----------------- 5 files changed, 6 insertions(+), 76 deletions(-) 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/hub.ts b/packages/core/src/hub.ts index e68ff4cafc53..db677ce96db5 100644 --- a/packages/core/src/hub.ts +++ b/packages/core/src/hub.ts @@ -24,6 +24,7 @@ import type { AsyncContextStrategy, Carrier } from './asyncContext'; import { getMainCarrier, getSentryCarrier } from './asyncContext'; import { DEFAULT_ENVIRONMENT } from './constants'; import { DEBUG_BUILD } from './debug-build'; +import { getCurrentHubShim } from './getCurrentHubShim'; import { Scope } from './scope'; import { closeSession, makeSession, updateSession } from './session'; import { SDK_VERSION } from './version'; @@ -506,13 +507,7 @@ export class Hub implements HubInterface { * @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(); -} +export const getCurrentHub = getCurrentHubShim; /** Get the default current scope. */ export function getDefaultCurrentScope(): Scope { @@ -586,7 +581,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/sdk.ts b/packages/core/src/sdk.ts index d4e4dafa5db7..d4c974242e1b 100644 --- a/packages/core/src/sdk.ts +++ b/packages/core/src/sdk.ts @@ -1,10 +1,8 @@ -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 { 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 +42,4 @@ export function initAndBind( */ export function setCurrentClient(client: Client): void { getCurrentScope().setClient(client); - - // is there a hub too? - // eslint-disable-next-line deprecation/deprecation - const hub = getCurrentHub(); - if (isHubClass(hub)) { - // eslint-disable-next-line deprecation/deprecation - const top = hub.getStackTop(); - top.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/opentelemetry/src/asyncContextStrategy.ts b/packages/opentelemetry/src/asyncContextStrategy.ts index 3ef7bf9ba2f2..6e51b66552f9 100644 --- a/packages/opentelemetry/src/asyncContextStrategy.ts +++ b/packages/opentelemetry/src/asyncContextStrategy.ts @@ -40,23 +40,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 +97,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, From 5c9a354e22b849d528ade6f537a88595feb29401 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 12 Apr 2024 15:59:13 +0200 Subject: [PATCH 2/6] biome... --- packages/opentelemetry/src/asyncContextStrategy.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry/src/asyncContextStrategy.ts b/packages/opentelemetry/src/asyncContextStrategy.ts index 6e51b66552f9..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, From b0ae26da49d47a360b2f3d130652a41644ad53d8 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 12 Apr 2024 17:27:22 +0200 Subject: [PATCH 3/6] fix circular deps in core --- packages/core/src/getCurrentHubShim.ts | 12 ++++++++++++ packages/core/src/hub.ts | 13 ------------- packages/core/src/index.ts | 4 +--- packages/core/src/trpc.ts | 11 ++++------- 4 files changed, 17 insertions(+), 23 deletions(-) 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 db677ce96db5..1bd3ec7c64b6 100644 --- a/packages/core/src/hub.ts +++ b/packages/core/src/hub.ts @@ -24,7 +24,6 @@ import type { AsyncContextStrategy, Carrier } from './asyncContext'; import { getMainCarrier, getSentryCarrier } from './asyncContext'; import { DEFAULT_ENVIRONMENT } from './constants'; import { DEBUG_BUILD } from './debug-build'; -import { getCurrentHubShim } from './getCurrentHubShim'; import { Scope } from './scope'; import { closeSession, makeSession, updateSession } from './session'; import { SDK_VERSION } from './version'; @@ -497,18 +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 const getCurrentHub = getCurrentHubShim; - /** Get the default current scope. */ export function getDefaultCurrentScope(): Scope { return getGlobalSingleton('defaultCurrentScope', () => new Scope()); 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/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`. */ From e658aff0ff1f567b803d2972964bb7533a369faa Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 12 Apr 2024 17:43:05 +0200 Subject: [PATCH 4/6] test --- packages/browser/src/sdk.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index d64981cb0c7a..a66a9cfd4589 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -1,4 +1,4 @@ -import { getCurrentScope } from '@sentry/core'; +import { getCurrentScope, withScope } from '@sentry/core'; import { functionToStringIntegration, inboundFiltersIntegration } from '@sentry/core'; import { captureSession, @@ -157,6 +157,11 @@ export function init(browserOptions: BrowserOptions = {}): void { initAndBind(BrowserClient, clientOptions); + // tmp test + getCurrentScope(); + // eslint-disable-next-line @typescript-eslint/no-empty-function + withScope(() => {}); + if (options.autoSessionTracking) { startSessionTracking(); } From 7766011985d397fa7aadacec639343d227d09eb0 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 15 Apr 2024 10:45:39 +0200 Subject: [PATCH 5/6] reset test --- packages/browser/src/sdk.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index a66a9cfd4589..d64981cb0c7a 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -1,4 +1,4 @@ -import { getCurrentScope, withScope } from '@sentry/core'; +import { getCurrentScope } from '@sentry/core'; import { functionToStringIntegration, inboundFiltersIntegration } from '@sentry/core'; import { captureSession, @@ -157,11 +157,6 @@ export function init(browserOptions: BrowserOptions = {}): void { initAndBind(BrowserClient, clientOptions); - // tmp test - getCurrentScope(); - // eslint-disable-next-line @typescript-eslint/no-empty-function - withScope(() => {}); - if (options.autoSessionTracking) { startSessionTracking(); } From 97d4e7dec3a76a7d69fa4d239c49d7ae6e3281e5 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 15 Apr 2024 11:23:32 +0200 Subject: [PATCH 6/6] set client on hub stack top --- packages/core/src/sdk.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/core/src/sdk.ts b/packages/core/src/sdk.ts index d4c974242e1b..ebe8f9a6ca22 100644 --- a/packages/core/src/sdk.ts +++ b/packages/core/src/sdk.ts @@ -2,7 +2,9 @@ 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'; /** A class object that can instantiate Client objects. */ export type ClientClass = new (options: O) => F; @@ -42,4 +44,22 @@ export function initAndBind( */ export function setCurrentClient(client: Client): void { getCurrentScope().setClient(client); + registerClientOnGlobalHub(client); +} + +/** + * 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 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 + sentryGlobal.hub.getStackTop().client = client; + } }