Skip to content

ref: Remove more usages of getCurrentHub in the codebase #11281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 15 additions & 32 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import type {
ClientOptions,
Hub,
Scope,
Span,
SpanTimeInput,
StartSpanOptions,
TransactionContext,
} from '@sentry/types';
import type { ClientOptions, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';

import { propagationContextFromHeaders } from '@sentry/utils';
import type { AsyncContextStrategy } from '../asyncContext';
Expand Down Expand Up @@ -51,15 +43,13 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
const spanContext = normalizeContext(context);

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

const shouldSkipSpan = context.onlyIfParent && !parentSpan;
const activeSpan = shouldSkipSpan
? new SentryNonRecordingSpan()
: createChildSpanOrTransaction(hub, {
: createChildSpanOrTransaction({
parentSpan,
spanContext,
forceTransaction: context.forceTransaction,
Expand Down Expand Up @@ -103,15 +93,13 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
const spanContext = normalizeContext(context);

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

const shouldSkipSpan = context.onlyIfParent && !parentSpan;
const activeSpan = shouldSkipSpan
? new SentryNonRecordingSpan()
: createChildSpanOrTransaction(hub, {
: createChildSpanOrTransaction({
parentSpan,
spanContext,
forceTransaction: context.forceTransaction,
Expand Down Expand Up @@ -155,8 +143,6 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
}

const spanContext = normalizeContext(context);
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
const parentSpan = context.scope
? // eslint-disable-next-line deprecation/deprecation
(context.scope.getSpan() as SentrySpan | undefined)
Expand All @@ -170,7 +156,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span {

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

return createChildSpanOrTransaction(hub, {
return createChildSpanOrTransaction({
parentSpan,
spanContext,
forceTransaction: context.forceTransaction,
Expand Down Expand Up @@ -225,20 +211,17 @@ export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) =>
});
}

function createChildSpanOrTransaction(
hub: Hub,
{
parentSpan,
spanContext,
forceTransaction,
scope,
}: {
parentSpan: SentrySpan | undefined;
spanContext: TransactionContext;
forceTransaction?: boolean;
scope: Scope;
},
): Span {
function createChildSpanOrTransaction({
parentSpan,
spanContext,
forceTransaction,
scope,
}: {
parentSpan: SentrySpan | undefined;
spanContext: TransactionContext;
forceTransaction?: boolean;
scope: Scope;
}): Span {
if (!hasTracingEnabled()) {
return new SentryNonRecordingSpan();
}
Expand Down
7 changes: 2 additions & 5 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { defineIntegration, getIsolationScope, hasTracingEnabled } from '@sentry
import {
addBreadcrumb,
getClient,
getCurrentHub,
getCurrentScope,
getDynamicSamplingContextFromClient,
getDynamicSamplingContextFromSpan,
Expand Down Expand Up @@ -166,8 +165,7 @@ export class Http implements Integration {
* @inheritDoc
*/
public setupOnce(): void {
// eslint-disable-next-line deprecation/deprecation
const clientOptions = getCurrentHub().getClient<NodeClient>()?.getOptions();
const clientOptions = getClient<NodeClient>()?.getOptions();

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

Expand Down
23 changes: 5 additions & 18 deletions packages/remix/src/utils/serverAdapters/express.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getClient, getCurrentHub, hasTracingEnabled, setHttpStatus, withIsolationScope } from '@sentry/core';
import { getClient, hasTracingEnabled, setHttpStatus, withIsolationScope } from '@sentry/core';
import { flush } from '@sentry/node';
import type { Hub, Span } from '@sentry/types';
import type { Span } from '@sentry/types';
import { extractRequestData, fill, isString, logger } from '@sentry/utils';

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

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

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

startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, hub, url);
startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, url);
});
} else {
routes = createRoutes(resolvedBuild.routes);

return startRequestHandlerTransactionWithRoutes.call(
this,
origRequestHandler,
routes,
req,
res,
next,
hub,
url,
);
return startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, url);
}
} else {
routes = createRoutes(build.routes);
}

return startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, hub, url);
return startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, url);
});
};
}
Expand All @@ -87,7 +76,6 @@ function startRequestHandlerTransactionWithRoutes(
req: ExpressRequest,
res: ExpressResponse,
next: ExpressNextFunction,
hub: Hub,
url: URL,
): unknown {
const [name, source] = getTransactionName(routes, url);
Expand Down Expand Up @@ -154,7 +142,6 @@ export function wrapExpressCreateRequestHandler(
origCreateRequestHandler: ExpressCreateRequestHandler,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): (options: any) => ExpressRequestHandler {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function (this: unknown, options: ExpressCreateRequestHandlerOptions): ExpressRequestHandler {
if (!('getLoadContext' in options)) {
options['getLoadContext'] = () => ({});
Expand Down
4 changes: 2 additions & 2 deletions packages/replay-internal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Sentry.init({

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

// Client can be undefined
client?.addIntegration(Sentry.replayIntegration());
Expand Down Expand Up @@ -105,7 +105,7 @@ Sentry.init({
integrations: [replay]
});

const client = Sentry.getCurrentHub().getClient<BrowserClient>();
const client = Sentry.getClient<BrowserClient>();

// Add replay integration, will start recoring
client?.addIntegration(replay);
Expand Down
2 changes: 1 addition & 1 deletion packages/sveltekit/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export declare const defaultStackParser: StackParser;
export declare const getClient: typeof clientSdk.getClient;
// eslint-disable-next-line deprecation/deprecation
export declare const getCurrentHub: typeof clientSdk.getCurrentHub;
// eslint-disable-next-line deprecation/deprecation

export declare function close(timeout?: number | undefined): PromiseLike<boolean>;
export declare function flush(timeout?: number | undefined): PromiseLike<boolean>;

Expand Down