Skip to content

Commit 409624b

Browse files
authored
feat(core): Remove getCurrentHub from AsyncContextStrategy (#11581)
Removes the `getCurrentHub` property from the `AsyncContextStrategy` interface and consequently also the property in the interface implementers.
1 parent 49e095f commit 409624b

File tree

8 files changed

+38
-99
lines changed

8 files changed

+38
-99
lines changed

packages/core/src/asyncContext.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Hub, Integration } from '@sentry/types';
1+
import type { Integration } from '@sentry/types';
22
import type { Scope } from '@sentry/types';
33
import { GLOBAL_OBJ } from '@sentry/utils';
44
import type { startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan } from './tracing/trace';
@@ -10,12 +10,6 @@ import type { getActiveSpan } from './utils/spanUtils';
1010
* Strategy used to track async context.
1111
*/
1212
export interface AsyncContextStrategy {
13-
/**
14-
* Gets the currently active hub.
15-
*/
16-
// eslint-disable-next-line deprecation/deprecation
17-
getCurrentHub: () => Hub;
18-
1913
/**
2014
* Fork the isolation scope inside of the provided callback.
2115
*/

packages/core/src/getCurrentHubShim.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ export function getCurrentHubShim(): Hub {
7070
};
7171
}
7272

73+
/**
74+
* Returns the default hub instance.
75+
*
76+
* If a hub is already registered in the global carrier but this module
77+
* contains a more recent version, it replaces the registered version.
78+
* Otherwise, the currently registered hub will be returned.
79+
*
80+
* @deprecated Use the respective replacement method directly instead.
81+
*/
82+
// eslint-disable-next-line deprecation/deprecation
83+
export const getCurrentHub = getCurrentHubShim;
84+
7385
/**
7486
* Sends the current Session on the scope
7587
*/

packages/core/src/hub.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -496,24 +496,6 @@ export class Hub implements HubInterface {
496496
}
497497
}
498498

499-
/**
500-
* Returns the default hub instance.
501-
*
502-
* If a hub is already registered in the global carrier but this module
503-
* contains a more recent version, it replaces the registered version.
504-
* Otherwise, the currently registered hub will be returned.
505-
*
506-
* @deprecated Use the respective replacement method directly instead.
507-
*/
508-
// eslint-disable-next-line deprecation/deprecation
509-
export function getCurrentHub(): HubInterface {
510-
// Get main carrier (global for every environment)
511-
const carrier = getMainCarrier();
512-
513-
const acs = getAsyncContextStrategy(carrier);
514-
return acs.getCurrentHub() || getGlobalHub();
515-
}
516-
517499
/** Get the default current scope. */
518500
export function getDefaultCurrentScope(): Scope {
519501
return getGlobalSingleton('defaultCurrentScope', () => new Scope());
@@ -586,7 +568,6 @@ function withIsolationScope<T>(callback: (isolationScope: ScopeInterface) => T):
586568
/* eslint-disable deprecation/deprecation */
587569
function getHubStackAsyncContextStrategy(): AsyncContextStrategy {
588570
return {
589-
getCurrentHub: getGlobalHub,
590571
withIsolationScope,
591572
withScope,
592573
withSetScope,

packages/core/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ export {
3030
addEventProcessor,
3131
} from './exports';
3232
export {
33-
// eslint-disable-next-line deprecation/deprecation
34-
getCurrentHub,
3533
getDefaultCurrentScope,
3634
getDefaultIsolationScope,
3735
} from './hub';
@@ -107,4 +105,4 @@ export { addTracingHeadersToFetchRequest, instrumentFetchRequest } from './fetch
107105
export { trpcMiddleware } from './trpc';
108106

109107
// eslint-disable-next-line deprecation/deprecation
110-
export { getCurrentHubShim } from './getCurrentHubShim';
108+
export { getCurrentHubShim, getCurrentHub } from './getCurrentHubShim';

packages/core/src/sdk.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { Client, ClientOptions, Hub as HubInterface } from '@sentry/types';
1+
import type { Client, ClientOptions } from '@sentry/types';
22
import { consoleSandbox, logger } from '@sentry/utils';
33
import { getCurrentScope } from './currentScopes';
44

5+
import { getMainCarrier, getSentryCarrier } from './asyncContext';
56
import { DEBUG_BUILD } from './debug-build';
67
import type { Hub } from './hub';
7-
import { getCurrentHub } from './hub';
88

99
/** A class object that can instantiate Client objects. */
1010
export type ClientClass<F extends Client, O extends ClientOptions> = new (options: O) => F;
@@ -44,19 +44,22 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
4444
*/
4545
export function setCurrentClient(client: Client): void {
4646
getCurrentScope().setClient(client);
47+
registerClientOnGlobalHub(client);
48+
}
4749

48-
// is there a hub too?
50+
/**
51+
* Unfortunately, we still have to manually bind the client to the "hub" set on the global
52+
* Sentry carrier object. This is because certain scripts (e.g. our loader script) obtain
53+
* the client via `window.__SENTRY__.hub.getClient()`.
54+
*
55+
* @see {@link hub.ts getGlobalHub}
56+
*/
57+
function registerClientOnGlobalHub(client: Client): void {
4958
// eslint-disable-next-line deprecation/deprecation
50-
const hub = getCurrentHub();
51-
if (isHubClass(hub)) {
59+
const sentryGlobal = getSentryCarrier(getMainCarrier()) as { hub?: Hub };
60+
// eslint-disable-next-line deprecation/deprecation
61+
if (sentryGlobal.hub && typeof sentryGlobal.hub.getStackTop === 'function') {
5262
// eslint-disable-next-line deprecation/deprecation
53-
const top = hub.getStackTop();
54-
top.client = client;
63+
sentryGlobal.hub.getStackTop().client = client;
5564
}
5665
}
57-
58-
// eslint-disable-next-line deprecation/deprecation
59-
function isHubClass(hub: HubInterface): hub is Hub {
60-
// eslint-disable-next-line deprecation/deprecation
61-
return !!(hub as Hub).getStackTop;
62-
}

packages/core/src/trpc.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { isThenable, normalize } from '@sentry/utils';
2-
import {
3-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4-
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
5-
captureException,
6-
setContext,
7-
startSpanManual,
8-
} from '.';
2+
93
import { getClient } from './currentScopes';
4+
import { captureException, setContext } from './exports';
5+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from './semanticAttributes';
6+
import { startSpanManual } from './tracing';
107

118
interface SentryTrpcMiddlewareOptions {
129
/** Whether to include procedure inputs in reported events. Defaults to `false`. */

packages/opentelemetry/src/asyncContextStrategy.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import * as api from '@opentelemetry/api';
2-
import {
3-
getCurrentHubShim,
4-
getDefaultCurrentScope,
5-
getDefaultIsolationScope,
6-
setAsyncContextStrategy,
7-
} from '@sentry/core';
2+
import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core';
83
import type { withActiveSpan as defaultWithActiveSpan } from '@sentry/core';
9-
import type { Hub, Scope } from '@sentry/types';
4+
import type { Scope } from '@sentry/types';
105

116
import {
127
SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY,
@@ -40,23 +35,6 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
4035
};
4136
}
4237

43-
// eslint-disable-next-line deprecation/deprecation
44-
function getCurrentHub(): Hub {
45-
// eslint-disable-next-line deprecation/deprecation
46-
const hub = getCurrentHubShim();
47-
return {
48-
...hub,
49-
getScope: () => {
50-
const scopes = getScopes();
51-
return scopes.scope;
52-
},
53-
getIsolationScope: () => {
54-
const scopes = getScopes();
55-
return scopes.isolationScope;
56-
},
57-
};
58-
}
59-
6038
function withScope<T>(callback: (scope: Scope) => T): T {
6139
const ctx = api.context.active();
6240

@@ -114,7 +92,6 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
11492
}
11593

11694
setAsyncContextStrategy({
117-
getCurrentHub,
11895
withScope,
11996
withSetScope,
12097
withSetIsolationScope,

packages/vercel-edge/src/async.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
getCurrentHubShim,
3-
getDefaultCurrentScope,
4-
getDefaultIsolationScope,
5-
setAsyncContextStrategy,
6-
} from '@sentry/core';
7-
import type { Hub, Scope } from '@sentry/types';
1+
import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core';
2+
import type { Scope } from '@sentry/types';
83
import { GLOBAL_OBJ, logger } from '@sentry/utils';
94

105
import { DEBUG_BUILD } from './debug-build';
@@ -51,23 +46,6 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
5146
};
5247
}
5348

54-
// eslint-disable-next-line deprecation/deprecation
55-
function getCurrentHub(): Hub {
56-
// eslint-disable-next-line deprecation/deprecation
57-
const hub = getCurrentHubShim();
58-
return {
59-
...hub,
60-
getScope: () => {
61-
const scopes = getScopes();
62-
return scopes.scope;
63-
},
64-
getIsolationScope: () => {
65-
const scopes = getScopes();
66-
return scopes.isolationScope;
67-
},
68-
};
69-
}
70-
7149
function withScope<T>(callback: (scope: Scope) => T): T {
7250
const scope = getScopes().scope.clone();
7351
const isolationScope = getScopes().isolationScope;
@@ -99,7 +77,6 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
9977
}
10078

10179
setAsyncContextStrategy({
102-
getCurrentHub,
10380
withScope,
10481
withSetScope,
10582
withIsolationScope,

0 commit comments

Comments
 (0)