Skip to content

Commit a7f5911

Browse files
authored
ref: Avoid unnecessary hub.getScope() checks (#9008)
We've changed this some time ago so that `hub.getScope()` _always_ returns a scope, so we can actually update our code where we still check for the existence of scope.
1 parent 868a3cd commit a7f5911

File tree

17 files changed

+106
-144
lines changed

17 files changed

+106
-144
lines changed

packages/angular/src/tracing.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ export function getActiveTransaction(): Transaction | undefined {
5555

5656
if (currentHub) {
5757
const scope = currentHub.getScope();
58-
if (scope) {
59-
return scope.getTransaction();
60-
}
58+
return scope.getTransaction();
6159
}
6260

6361
return undefined;

packages/nextjs/src/common/utils/edgeWrapperUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function withEdgeWrapping<H extends EdgeRouteHandler>(
1414
return async function (this: unknown, ...args) {
1515
const req = args[0];
1616
const currentScope = getCurrentHub().getScope();
17-
const prevSpan = currentScope?.getSpan();
17+
const prevSpan = currentScope.getSpan();
1818

1919
let span: Span | undefined;
2020

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

Lines changed: 73 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -89,75 +89,73 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
8989
const currentScope = hub.getScope();
9090
const options = hub.getClient()?.getOptions();
9191

92-
if (currentScope) {
93-
currentScope.setSDKProcessingMetadata({ request: req });
94-
95-
if (hasTracingEnabled(options) && options?.instrumenter === 'sentry') {
96-
const sentryTrace =
97-
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
98-
const baggage = req.headers?.baggage;
99-
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
100-
sentryTrace,
101-
baggage,
102-
);
103-
hub.getScope().setPropagationContext(propagationContext);
104-
105-
if (__DEBUG_BUILD__ && traceparentData) {
106-
logger.log(`[Tracing] Continuing trace ${traceparentData.traceId}.`);
107-
}
92+
currentScope.setSDKProcessingMetadata({ request: req });
93+
94+
if (hasTracingEnabled(options) && options?.instrumenter === 'sentry') {
95+
const sentryTrace =
96+
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
97+
const baggage = req.headers?.baggage;
98+
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
99+
sentryTrace,
100+
baggage,
101+
);
102+
currentScope.setPropagationContext(propagationContext);
103+
104+
if (__DEBUG_BUILD__ && traceparentData) {
105+
logger.log(`[Tracing] Continuing trace ${traceparentData.traceId}.`);
106+
}
108107

109-
// prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
110-
let reqPath = parameterizedRoute;
111-
112-
// If not, fake it by just replacing parameter values with their names, hoping that none of them match either
113-
// each other or any hard-coded parts of the path
114-
if (!reqPath) {
115-
const url = `${req.url}`;
116-
// pull off query string, if any
117-
reqPath = stripUrlQueryAndFragment(url);
118-
// Replace with placeholder
119-
if (req.query) {
120-
for (const [key, value] of Object.entries(req.query)) {
121-
reqPath = reqPath.replace(`${value}`, `[${key}]`);
122-
}
108+
// prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
109+
let reqPath = parameterizedRoute;
110+
111+
// If not, fake it by just replacing parameter values with their names, hoping that none of them match either
112+
// each other or any hard-coded parts of the path
113+
if (!reqPath) {
114+
const url = `${req.url}`;
115+
// pull off query string, if any
116+
reqPath = stripUrlQueryAndFragment(url);
117+
// Replace with placeholder
118+
if (req.query) {
119+
for (const [key, value] of Object.entries(req.query)) {
120+
reqPath = reqPath.replace(`${value}`, `[${key}]`);
123121
}
124122
}
123+
}
125124

126-
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
127-
128-
transaction = startTransaction(
129-
{
130-
name: `${reqMethod}${reqPath}`,
131-
op: 'http.server',
132-
origin: 'auto.http.nextjs',
133-
...traceparentData,
134-
metadata: {
135-
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
136-
source: 'route',
137-
request: req,
138-
},
125+
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
126+
127+
transaction = startTransaction(
128+
{
129+
name: `${reqMethod}${reqPath}`,
130+
op: 'http.server',
131+
origin: 'auto.http.nextjs',
132+
...traceparentData,
133+
metadata: {
134+
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
135+
source: 'route',
136+
request: req,
139137
},
140-
// extra context passed to the `tracesSampler`
141-
{ request: req },
142-
);
143-
currentScope.setSpan(transaction);
144-
if (platformSupportsStreaming() && !wrappingTarget.__sentry_test_doesnt_support_streaming__) {
145-
autoEndTransactionOnResponseEnd(transaction, res);
146-
} else {
147-
// If we're not on a platform that supports streaming, we're blocking res.end() until the queue is flushed.
148-
// res.json() and res.send() will implicitly call res.end(), so it is enough to wrap res.end().
149-
150-
// eslint-disable-next-line @typescript-eslint/unbound-method
151-
const origResEnd = res.end;
152-
res.end = async function (this: unknown, ...args: unknown[]) {
153-
if (transaction) {
154-
await finishTransaction(transaction, res);
155-
await flushQueue();
156-
}
157-
158-
origResEnd.apply(this, args);
159-
};
160-
}
138+
},
139+
// extra context passed to the `tracesSampler`
140+
{ request: req },
141+
);
142+
currentScope.setSpan(transaction);
143+
if (platformSupportsStreaming() && !wrappingTarget.__sentry_test_doesnt_support_streaming__) {
144+
autoEndTransactionOnResponseEnd(transaction, res);
145+
} else {
146+
// If we're not on a platform that supports streaming, we're blocking res.end() until the queue is flushed.
147+
// res.json() and res.send() will implicitly call res.end(), so it is enough to wrap res.end().
148+
149+
// eslint-disable-next-line @typescript-eslint/unbound-method
150+
const origResEnd = res.end;
151+
res.end = async function (this: unknown, ...args: unknown[]) {
152+
if (transaction) {
153+
await finishTransaction(transaction, res);
154+
await flushQueue();
155+
}
156+
157+
origResEnd.apply(this, args);
158+
};
161159
}
162160
}
163161

@@ -187,21 +185,19 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
187185
// way to prevent it from actually being reported twice.)
188186
const objectifiedErr = objectify(e);
189187

190-
if (currentScope) {
191-
currentScope.addEventProcessor(event => {
192-
addExceptionMechanism(event, {
193-
type: 'instrument',
194-
handled: false,
195-
data: {
196-
wrapped_handler: wrappingTarget.name,
197-
function: 'withSentry',
198-
},
199-
});
200-
return event;
188+
currentScope.addEventProcessor(event => {
189+
addExceptionMechanism(event, {
190+
type: 'instrument',
191+
handled: false,
192+
data: {
193+
wrapped_handler: wrappingTarget.name,
194+
function: 'withSentry',
195+
},
201196
});
197+
return event;
198+
});
202199

203-
captureException(objectifiedErr);
204-
}
200+
captureException(objectifiedErr);
205201

206202
// Because we're going to finish and send the transaction before passing the error onto nextjs, it won't yet
207203
// have had a chance to set the status to 500, so unless we do it ourselves now, we'll incorrectly report that

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
5151
},
5252
});
5353

54-
if (currentScope) {
55-
currentScope.setSpan(transaction);
56-
}
54+
currentScope.setSpan(transaction);
5755

5856
const handleErrorCase = (e: unknown): void => {
5957
if (isNotFoundNavigationError(e)) {

packages/node/src/handlers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,8 @@ export function requestHandler(
198198
const client = currentHub.getClient<NodeClient>();
199199
if (isAutoSessionTrackingEnabled(client)) {
200200
const scope = currentHub.getScope();
201-
if (scope) {
202-
// Set `status` of `RequestSession` to Ok, at the beginning of the request
203-
scope.setRequestSession({ status: 'ok' });
204-
}
201+
// Set `status` of `RequestSession` to Ok, at the beginning of the request
202+
scope.setRequestSession({ status: 'ok' });
205203
}
206204
});
207205

packages/react/src/profiler.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ export { withProfiler, Profiler, useProfiler };
217217
export function getActiveTransaction<T extends Transaction>(hub: Hub = getCurrentHub()): T | undefined {
218218
if (hub) {
219219
const scope = hub.getScope();
220-
if (scope) {
221-
return scope.getTransaction() as T | undefined;
222-
}
220+
return scope.getTransaction() as T | undefined;
223221
}
224222

225223
return undefined;

packages/remix/src/utils/instrumentServer.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ function makeWrappedDocumentRequestFunction(
131131
let res: Response;
132132

133133
const activeTransaction = getActiveTransaction();
134-
const currentScope = getCurrentHub().getScope();
135-
136-
if (!currentScope) {
137-
return origDocumentRequestFunction.call(this, request, responseStatusCode, responseHeaders, context, loadContext);
138-
}
139134

140135
try {
141136
const span = activeTransaction?.startChild({
@@ -176,10 +171,6 @@ function makeWrappedDataFunction(origFn: DataFunction, id: string, name: 'action
176171
const activeTransaction = getActiveTransaction();
177172
const currentScope = getCurrentHub().getScope();
178173

179-
if (!currentScope) {
180-
return origFn.call(this, args);
181-
}
182-
183174
try {
184175
const span = activeTransaction?.startChild({
185176
op: `function.remix.${name}`,
@@ -228,17 +219,15 @@ function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string }
228219
const currentScope = getCurrentHub().getScope();
229220

230221
if (isNodeEnv() && hasTracingEnabled()) {
231-
if (currentScope) {
232-
const span = currentScope.getSpan();
222+
const span = currentScope.getSpan();
233223

234-
if (span && transaction) {
235-
const dynamicSamplingContext = transaction.getDynamicSamplingContext();
224+
if (span && transaction) {
225+
const dynamicSamplingContext = transaction.getDynamicSamplingContext();
236226

237-
return {
238-
sentryTrace: span.toTraceparent(),
239-
sentryBaggage: dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext),
240-
};
241-
}
227+
return {
228+
sentryTrace: span.toTraceparent(),
229+
sentryBaggage: dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext),
230+
};
242231
}
243232
}
244233

@@ -376,16 +365,14 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
376365
const url = new URL(request.url);
377366
const [name, source] = getTransactionName(routes, url, pkg);
378367

379-
if (scope) {
380-
scope.setSDKProcessingMetadata({
381-
request: {
382-
...normalizedRequest,
383-
route: {
384-
path: name,
385-
},
368+
scope.setSDKProcessingMetadata({
369+
request: {
370+
...normalizedRequest,
371+
route: {
372+
path: name,
386373
},
387-
});
388-
}
374+
},
375+
});
389376

390377
if (!options || !hasTracingEnabled(options)) {
391378
return origRequestHandler.call(this, request, loadContext);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ function wrapExpressRequestHandler(
6161
const options = hub.getClient()?.getOptions();
6262
const scope = hub.getScope();
6363

64-
if (scope) {
65-
scope.setSDKProcessingMetadata({ request });
66-
}
64+
scope.setSDKProcessingMetadata({ request });
6765

6866
if (!options || !hasTracingEnabled(options) || !request.url || !request.method) {
6967
return origRequestHandler.call(this, req, res, next);

packages/replay/src/util/addGlobalListeners.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export function addGlobalListeners(replay: ReplayContainer): void {
1919
const scope = getCurrentHub().getScope();
2020
const client = getCurrentHub().getClient();
2121

22-
if (scope) {
23-
scope.addScopeListener(handleScopeListener(replay));
24-
}
22+
scope.addScopeListener(handleScopeListener(replay));
2523
addInstrumentationHandler('dom', handleDomListener(replay));
2624
addInstrumentationHandler('history', handleHistorySpanListener(replay));
2725
handleNetworkBreadcrumbs(replay);

packages/serverless/src/awsservices.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub } from '@sentry/node';
2-
import type { Integration, Span, Transaction } from '@sentry/types';
2+
import type { Integration, Span } from '@sentry/types';
33
import { fill } from '@sentry/utils';
44
// 'aws-sdk/global' import is expected to be type-only so it's erased in the final .js file.
55
// When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here.
@@ -56,12 +56,9 @@ function wrapMakeRequest<TService extends AWSService, TResult>(
5656
orig: MakeRequestFunction<GenericParams, TResult>,
5757
): MakeRequestFunction<GenericParams, TResult> {
5858
return function (this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback<TResult>) {
59-
let transaction: Transaction | undefined;
6059
let span: Span | undefined;
6160
const scope = getCurrentHub().getScope();
62-
if (scope) {
63-
transaction = scope.getTransaction();
64-
}
61+
const transaction = scope.getTransaction();
6562
const req = orig.call(this, operation, params);
6663
req.on('afterBuild', () => {
6764
if (transaction) {

packages/serverless/src/google-cloud-grpc.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub } from '@sentry/node';
2-
import type { Integration, Span, Transaction } from '@sentry/types';
2+
import type { Integration, Span } from '@sentry/types';
33
import { fill } from '@sentry/utils';
44
import type { EventEmitter } from 'events';
55

@@ -107,12 +107,9 @@ function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: str
107107
if (typeof ret?.on !== 'function') {
108108
return ret;
109109
}
110-
let transaction: Transaction | undefined;
111110
let span: Span | undefined;
112111
const scope = getCurrentHub().getScope();
113-
if (scope) {
114-
transaction = scope.getTransaction();
115-
}
112+
const transaction = scope.getTransaction();
116113
if (transaction) {
117114
span = transaction.startChild({
118115
description: `${callType} ${methodName}`,

packages/serverless/src/google-cloud-http.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here.
33
import type * as common from '@google-cloud/common';
44
import { getCurrentHub } from '@sentry/node';
5-
import type { Integration, Span, Transaction } from '@sentry/types';
5+
import type { Integration, Span } from '@sentry/types';
66
import { fill } from '@sentry/utils';
77

88
type RequestOptions = common.DecorateRequestOptions;
@@ -51,12 +51,9 @@ export class GoogleCloudHttp implements Integration {
5151
/** Returns a wrapped function that makes a request with tracing enabled */
5252
function wrapRequestFunction(orig: RequestFunction): RequestFunction {
5353
return function (this: common.Service, reqOpts: RequestOptions, callback: ResponseCallback): void {
54-
let transaction: Transaction | undefined;
5554
let span: Span | undefined;
5655
const scope = getCurrentHub().getScope();
57-
if (scope) {
58-
transaction = scope.getTransaction();
59-
}
56+
const transaction = scope.getTransaction();
6057
if (transaction) {
6158
const httpMethod = reqOpts.method || 'GET';
6259
span = transaction.startChild({

packages/tracing-internal/src/node/integrations/apollo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function wrapResolver(
188188
fill(model[resolverGroupName], resolverName, function (orig: () => unknown | Promise<unknown>) {
189189
return function (this: unknown, ...args: unknown[]) {
190190
const scope = getCurrentHub().getScope();
191-
const parentSpan = scope?.getSpan();
191+
const parentSpan = scope.getSpan();
192192
const span = parentSpan?.startChild({
193193
description: `${resolverGroupName}.${resolverName}`,
194194
op: 'graphql.resolve',

0 commit comments

Comments
 (0)