Skip to content

Commit ade0410

Browse files
authored
ref: Remove more usages of getCurrentHub in the codebase (#11281)
The remaining usages are shims + stuff we have for tests. Not sure how much test code we should delete.
1 parent 3b2d4ba commit ade0410

File tree

5 files changed

+25
-58
lines changed

5 files changed

+25
-58
lines changed

packages/core/src/tracing/trace.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import type {
2-
ClientOptions,
3-
Hub,
4-
Scope,
5-
Span,
6-
SpanTimeInput,
7-
StartSpanOptions,
8-
TransactionContext,
9-
} from '@sentry/types';
1+
import type { ClientOptions, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';
102

113
import { propagationContextFromHeaders } from '@sentry/utils';
124
import type { AsyncContextStrategy } from '../asyncContext';
@@ -52,15 +44,13 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
5244
const spanContext = normalizeContext(context);
5345

5446
return withScope(context.scope, scope => {
55-
// eslint-disable-next-line deprecation/deprecation
56-
const hub = getCurrentHub();
5747
// eslint-disable-next-line deprecation/deprecation
5848
const parentSpan = scope.getSpan() as SentrySpan | undefined;
5949

6050
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
6151
const activeSpan = shouldSkipSpan
6252
? new SentryNonRecordingSpan()
63-
: createChildSpanOrTransaction(hub, {
53+
: createChildSpanOrTransaction({
6454
parentSpan,
6555
spanContext,
6656
forceTransaction: context.forceTransaction,
@@ -104,15 +94,13 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
10494
const spanContext = normalizeContext(context);
10595

10696
return withScope(context.scope, scope => {
107-
// eslint-disable-next-line deprecation/deprecation
108-
const hub = getCurrentHub();
10997
// eslint-disable-next-line deprecation/deprecation
11098
const parentSpan = scope.getSpan() as SentrySpan | undefined;
11199

112100
const shouldSkipSpan = context.onlyIfParent && !parentSpan;
113101
const activeSpan = shouldSkipSpan
114102
? new SentryNonRecordingSpan()
115-
: createChildSpanOrTransaction(hub, {
103+
: createChildSpanOrTransaction({
116104
parentSpan,
117105
spanContext,
118106
forceTransaction: context.forceTransaction,
@@ -156,8 +144,6 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
156144
}
157145

158146
const spanContext = normalizeContext(context);
159-
// eslint-disable-next-line deprecation/deprecation
160-
const hub = getCurrentHub();
161147
const parentSpan = context.scope
162148
? // eslint-disable-next-line deprecation/deprecation
163149
(context.scope.getSpan() as SentrySpan | undefined)
@@ -171,7 +157,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
171157

172158
const scope = context.scope || getCurrentScope();
173159

174-
return createChildSpanOrTransaction(hub, {
160+
return createChildSpanOrTransaction({
175161
parentSpan,
176162
spanContext,
177163
forceTransaction: context.forceTransaction,
@@ -226,20 +212,17 @@ export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) =>
226212
});
227213
}
228214

229-
function createChildSpanOrTransaction(
230-
hub: Hub,
231-
{
232-
parentSpan,
233-
spanContext,
234-
forceTransaction,
235-
scope,
236-
}: {
237-
parentSpan: SentrySpan | undefined;
238-
spanContext: TransactionContext;
239-
forceTransaction?: boolean;
240-
scope: Scope;
241-
},
242-
): Span {
215+
function createChildSpanOrTransaction({
216+
parentSpan,
217+
spanContext,
218+
forceTransaction,
219+
scope,
220+
}: {
221+
parentSpan: SentrySpan | undefined;
222+
spanContext: TransactionContext;
223+
forceTransaction?: boolean;
224+
scope: Scope;
225+
}): Span {
243226
if (!hasTracingEnabled()) {
244227
return new SentryNonRecordingSpan();
245228
}

packages/node/src/integrations/http.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { defineIntegration, getIsolationScope, hasTracingEnabled } from '@sentry
66
import {
77
addBreadcrumb,
88
getClient,
9-
getCurrentHub,
109
getCurrentScope,
1110
getDynamicSamplingContextFromClient,
1211
getDynamicSamplingContextFromSpan,
@@ -166,8 +165,7 @@ export class Http implements Integration {
166165
* @inheritDoc
167166
*/
168167
public setupOnce(): void {
169-
// eslint-disable-next-line deprecation/deprecation
170-
const clientOptions = getCurrentHub().getClient<NodeClient>()?.getOptions();
168+
const clientOptions = getClient<NodeClient>()?.getOptions();
171169

172170
// If `tracing` is not explicitly set, we default this based on whether or not tracing is enabled.
173171
// But for compatibility, we only do that if `enableIfHasTracingEnabled` is set.
@@ -275,8 +273,7 @@ function _createWrappedRequestMethodFactory(
275273
req: http.ClientRequest,
276274
res?: http.IncomingMessage,
277275
): void {
278-
// eslint-disable-next-line deprecation/deprecation
279-
if (!getCurrentHub().getIntegration(Http)) {
276+
if (!getClient()?.getIntegrationByName('Http')) {
280277
return;
281278
}
282279

packages/remix/src/utils/serverAdapters/express.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getClient, getCurrentHub, hasTracingEnabled, setHttpStatus, withIsolationScope } from '@sentry/core';
1+
import { getClient, hasTracingEnabled, setHttpStatus, withIsolationScope } from '@sentry/core';
22
import { flush } from '@sentry/node';
3-
import type { Hub, Span } from '@sentry/types';
3+
import type { Span } from '@sentry/types';
44
import { extractRequestData, fill, isString, logger } from '@sentry/utils';
55

66
import { DEBUG_BUILD } from '../debug-build';
@@ -35,8 +35,6 @@ function wrapExpressRequestHandler(
3535
res.end = wrapEndMethod(res.end);
3636

3737
const request = extractRequestData(req);
38-
// eslint-disable-next-line deprecation/deprecation
39-
const hub = getCurrentHub();
4038
const options = getClient()?.getOptions();
4139

4240
isolationScope.setSDKProcessingMetadata({ request });
@@ -55,27 +53,18 @@ function wrapExpressRequestHandler(
5553
return resolvedBuild.then(resolved => {
5654
routes = createRoutes(resolved.routes);
5755

58-
startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, hub, url);
56+
startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, url);
5957
});
6058
} else {
6159
routes = createRoutes(resolvedBuild.routes);
6260

63-
return startRequestHandlerTransactionWithRoutes.call(
64-
this,
65-
origRequestHandler,
66-
routes,
67-
req,
68-
res,
69-
next,
70-
hub,
71-
url,
72-
);
61+
return startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, url);
7362
}
7463
} else {
7564
routes = createRoutes(build.routes);
7665
}
7766

78-
return startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, hub, url);
67+
return startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, url);
7968
});
8069
};
8170
}
@@ -87,7 +76,6 @@ function startRequestHandlerTransactionWithRoutes(
8776
req: ExpressRequest,
8877
res: ExpressResponse,
8978
next: ExpressNextFunction,
90-
hub: Hub,
9179
url: URL,
9280
): unknown {
9381
const [name, source] = getTransactionName(routes, url);
@@ -154,7 +142,6 @@ export function wrapExpressCreateRequestHandler(
154142
origCreateRequestHandler: ExpressCreateRequestHandler,
155143
// eslint-disable-next-line @typescript-eslint/no-explicit-any
156144
): (options: any) => ExpressRequestHandler {
157-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
158145
return function (this: unknown, options: ExpressCreateRequestHandlerOptions): ExpressRequestHandler {
159146
if (!('getLoadContext' in options)) {
160147
options['getLoadContext'] = () => ({});

packages/replay-internal/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Sentry.init({
7272

7373
// Sometime later
7474
const { Replay } = await import('@sentry/browser');
75-
const client = Sentry.getCurrentHub().getClient<BrowserClient>();
75+
const client = Sentry.getClient<BrowserClient>();
7676

7777
// Client can be undefined
7878
client?.addIntegration(Sentry.replayIntegration());
@@ -105,7 +105,7 @@ Sentry.init({
105105
integrations: [replay]
106106
});
107107

108-
const client = Sentry.getCurrentHub().getClient<BrowserClient>();
108+
const client = Sentry.getClient<BrowserClient>();
109109

110110
// Add replay integration, will start recoring
111111
client?.addIntegration(replay);

packages/sveltekit/src/index.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export declare const defaultStackParser: StackParser;
4545
export declare const getClient: typeof clientSdk.getClient;
4646
// eslint-disable-next-line deprecation/deprecation
4747
export declare const getCurrentHub: typeof clientSdk.getCurrentHub;
48-
// eslint-disable-next-line deprecation/deprecation
48+
4949
export declare function close(timeout?: number | undefined): PromiseLike<boolean>;
5050
export declare function flush(timeout?: number | undefined): PromiseLike<boolean>;
5151

0 commit comments

Comments
 (0)