From 496995c09669289baff25a01bbbe7f575427c8fc Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 10 Apr 2024 14:19:21 +0000 Subject: [PATCH 1/4] feat: Hoist `getCurrentHub` shim to core as `getCurrentHubShim` --- .../src/custom/getCurrentHub.ts => core/src/getCurrentHubShim.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/{opentelemetry/src/custom/getCurrentHub.ts => core/src/getCurrentHubShim.ts} (100%) diff --git a/packages/opentelemetry/src/custom/getCurrentHub.ts b/packages/core/src/getCurrentHubShim.ts similarity index 100% rename from packages/opentelemetry/src/custom/getCurrentHub.ts rename to packages/core/src/getCurrentHubShim.ts From 7cf8268d9b932a4f049eebe198e127b419e7a8aa Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 10 Apr 2024 14:21:31 +0000 Subject: [PATCH 2/4] Adjust for core --- packages/core/src/getCurrentHubShim.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/core/src/getCurrentHubShim.ts b/packages/core/src/getCurrentHubShim.ts index 9db09297d670..0880a4a0ab85 100644 --- a/packages/core/src/getCurrentHubShim.ts +++ b/packages/core/src/getCurrentHubShim.ts @@ -1,12 +1,9 @@ import type { Client, EventHint, Hub, Integration, IntegrationClass, SeverityLevel } from '@sentry/types'; - +import { addBreadcrumb } from './breadcrumbs'; +import { getClient, getCurrentScope, getIsolationScope, withScope } from './currentScopes'; import { - addBreadcrumb, captureEvent, endSession, - getClient, - getCurrentScope, - getIsolationScope, setContext, setExtra, setExtras, @@ -14,8 +11,7 @@ import { setTags, setUser, startSession, - withScope, -} from '@sentry/core'; +} from './exports'; /** * This is for legacy reasons, and returns a proxy object instead of a hub to be used. @@ -48,7 +44,8 @@ export function getCurrentHub(): Hub { setContext, getIntegration(integration: IntegrationClass): T | null { - return getClient()?.getIntegrationByName(integration.id) || null; + const client = getClient(); + return (client && client.getIntegrationByName(integration.id)) || null; }, startSession, From 1ce9030a10562a01f40fc1a992f923170650d513 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 10 Apr 2024 14:27:45 +0000 Subject: [PATCH 3/4] Update usages --- packages/core/src/getCurrentHubShim.ts | 3 ++- packages/core/src/index.ts | 3 +++ packages/opentelemetry/src/asyncContextStrategy.ts | 10 +++++++--- packages/opentelemetry/src/index.ts | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/core/src/getCurrentHubShim.ts b/packages/core/src/getCurrentHubShim.ts index 0880a4a0ab85..435004ce9f57 100644 --- a/packages/core/src/getCurrentHubShim.ts +++ b/packages/core/src/getCurrentHubShim.ts @@ -15,9 +15,10 @@ import { /** * This is for legacy reasons, and returns a proxy object instead of a hub to be used. + * * @deprecated Use the methods directly. */ -export function getCurrentHub(): Hub { +export function getCurrentHubShim(): Hub { return { bindClient(client: Client): void { const scope = getCurrentScope(); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 48bb5baf6afc..e3f827605aeb 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -106,3 +106,6 @@ export { BrowserMetricsAggregator } from './metrics/browser-aggregator'; export { getMetricSummaryJsonForSpan } from './metrics/metric-summary'; export { addTracingHeadersToFetchRequest, instrumentFetchRequest } from './fetch'; export { trpcMiddleware } from './trpc'; + +// eslint-disable-next-line deprecation/deprecation +export { getCurrentHubShim } from './getCurrentHubShim'; diff --git a/packages/opentelemetry/src/asyncContextStrategy.ts b/packages/opentelemetry/src/asyncContextStrategy.ts index b7f66870d07f..e28995a5d805 100644 --- a/packages/opentelemetry/src/asyncContextStrategy.ts +++ b/packages/opentelemetry/src/asyncContextStrategy.ts @@ -1,5 +1,10 @@ import * as api from '@opentelemetry/api'; -import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core'; +import { + getCurrentHubShim, + getDefaultCurrentScope, + getDefaultIsolationScope, + setAsyncContextStrategy, +} from '@sentry/core'; import type { withActiveSpan as defaultWithActiveSpan } from '@sentry/core'; import type { Hub, Scope } from '@sentry/types'; @@ -8,7 +13,6 @@ import { SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, SENTRY_FORK_SET_SCOPE_CONTEXT_KEY, } from './constants'; -import { getCurrentHub as _getCurrentHub } from './custom/getCurrentHub'; import { startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from './trace'; import type { CurrentScopes } from './types'; import { getScopesFromContext } from './utils/contextData'; @@ -38,7 +42,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void { function getCurrentHub(): Hub { // eslint-disable-next-line deprecation/deprecation - const hub = _getCurrentHub(); + const hub = getCurrentHubShim(); return { ...hub, getScope: () => { diff --git a/packages/opentelemetry/src/index.ts b/packages/opentelemetry/src/index.ts index 3d727c707897..db5abb951f4c 100644 --- a/packages/opentelemetry/src/index.ts +++ b/packages/opentelemetry/src/index.ts @@ -28,7 +28,7 @@ export { suppressTracing } from './utils/suppressTracing'; // eslint-disable-next-line deprecation/deprecation export { setupGlobalHub } from './custom/hub'; // eslint-disable-next-line deprecation/deprecation -export { getCurrentHub } from './custom/getCurrentHub'; +export { getCurrentHubShim } from '@sentry/core'; export { setupEventContextTrace } from './setupEventContextTrace'; export { setOpenTelemetryContextAsyncContextStrategy } from './asyncContextStrategy'; From a6abf99dca907e0d586753e024ec3bcea0bc29a0 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 10 Apr 2024 14:42:48 +0000 Subject: [PATCH 4/4] feat(vercel-edge): Stop using hub --- packages/vercel-edge/src/async.ts | 93 +++++++++++++++---------------- 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/packages/vercel-edge/src/async.ts b/packages/vercel-edge/src/async.ts index 50e2d80ad652..d0a9b8644d3f 100644 --- a/packages/vercel-edge/src/async.ts +++ b/packages/vercel-edge/src/async.ts @@ -1,5 +1,9 @@ -import { Hub as HubClass, getGlobalHub } from '@sentry/core'; -import { setAsyncContextStrategy } from '@sentry/core'; +import { + getCurrentHubShim, + getDefaultCurrentScope, + getDefaultIsolationScope, + setAsyncContextStrategy, +} from '@sentry/core'; import type { Hub, Scope } from '@sentry/types'; import { GLOBAL_OBJ, logger } from '@sentry/utils'; @@ -11,7 +15,7 @@ interface AsyncLocalStorage { run(store: T, callback: (...args: TArgs) => R, ...args: TArgs): R; } -let asyncStorage: AsyncLocalStorage; +let asyncStorage: AsyncLocalStorage<{ scope: Scope; isolationScope: Scope }>; /** * Sets the async context strategy to use AsyncLocalStorage which should be available in the edge runtime. @@ -32,68 +36,63 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void { asyncStorage = new MaybeGlobalAsyncLocalStorage(); } - function getCurrentAsyncStorageHub(): Hub | undefined { - return asyncStorage.getStore(); + function getScopes(): { scope: Scope; isolationScope: Scope } { + const scopes = asyncStorage.getStore(); + + if (scopes) { + return scopes; + } + + // fallback behavior: + // if, for whatever reason, we can't find scopes on the context here, we have to fix this somehow + return { + scope: getDefaultCurrentScope(), + isolationScope: getDefaultIsolationScope(), + }; } function getCurrentHub(): Hub { - return getCurrentAsyncStorageHub() || getGlobalHub(); + // 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 parentHub = getCurrentHub(); - - /* eslint-disable deprecation/deprecation */ - const client = parentHub.getClient(); - const scope = parentHub.getScope().clone(); - const isolationScope = parentHub.getIsolationScope(); - const newHub = new HubClass(client, scope, isolationScope); - /* eslint-enable deprecation/deprecation */ - - return asyncStorage.run(newHub, () => { + const scope = getScopes().scope.clone(); + const isolationScope = getScopes().isolationScope; + return asyncStorage.run({ scope, isolationScope }, () => { return callback(scope); }); } function withSetScope(scope: Scope, callback: (scope: Scope) => T): T { - const parentHub = getCurrentHub(); - - /* eslint-disable deprecation/deprecation */ - const client = parentHub.getClient(); - const isolationScope = parentHub.getIsolationScope(); - const newHub = new HubClass(client, scope, isolationScope); - /* eslint-enable deprecation/deprecation */ - - return asyncStorage.run(newHub, () => { + const isolationScope = getScopes().isolationScope.clone(); + return asyncStorage.run({ scope, isolationScope }, () => { return callback(scope); }); } function withIsolationScope(callback: (isolationScope: Scope) => T): T { - const parentHub = getCurrentHub(); - - /* eslint-disable deprecation/deprecation */ - const client = parentHub.getClient(); - const scope = parentHub.getScope().clone(); - const isolationScope = parentHub.getIsolationScope().clone(); - const newHub = new HubClass(client, scope, isolationScope); - /* eslint-enable deprecation/deprecation */ - - return asyncStorage.run(newHub, () => { + const scope = getScopes().scope; + const isolationScope = getScopes().isolationScope.clone(); + return asyncStorage.run({ scope, isolationScope }, () => { return callback(isolationScope); }); } function withSetIsolationScope(isolationScope: Scope, callback: (isolationScope: Scope) => T): T { - const parentHub = getCurrentHub(); - - /* eslint-disable deprecation/deprecation */ - const client = parentHub.getClient(); - const scope = parentHub.getScope().clone(); - const newHub = new HubClass(client, scope, isolationScope); - /* eslint-enable deprecation/deprecation */ - - return asyncStorage.run(newHub, () => { + const scope = getScopes().scope; + return asyncStorage.run({ scope, isolationScope }, () => { return callback(isolationScope); }); } @@ -104,9 +103,7 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void { withSetScope, withIsolationScope, withSetIsolationScope, - // eslint-disable-next-line deprecation/deprecation - getCurrentScope: () => getCurrentHub().getScope(), - // eslint-disable-next-line deprecation/deprecation - getIsolationScope: () => getCurrentHub().getIsolationScope(), + getCurrentScope: () => getScopes().scope, + getIsolationScope: () => getScopes().isolationScope, }); }