Skip to content

Commit 22b5887

Browse files
authored
ref: Remove undefined checks for hub.getScope() (#8401)
1 parent e583fe9 commit 22b5887

File tree

20 files changed

+29
-38
lines changed

20 files changed

+29
-38
lines changed

packages/e2e-tests/test-applications/nextjs-app-dir/components/transaction-context.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function TransactionContextProvider({ children }: PropsWithChildren) {
2929
transactionActive: false,
3030
start: (transactionName: string) => {
3131
const t = startTransaction({ name: transactionName });
32-
getCurrentHub().getScope()?.setSpan(t);
32+
getCurrentHub().getScope().setSpan(t);
3333
setTransaction(t);
3434
},
3535
}

packages/ember/addon/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function InitSentryForEmber(_runtimeConfig?: BrowserOptions) {
6363
}
6464

6565
export const getActiveTransaction = () => {
66-
return Sentry.getCurrentHub()?.getScope()?.getTransaction();
66+
return Sentry.getCurrentHub().getScope().getTransaction();
6767
};
6868

6969
export const instrumentRoutePerformance = (BaseRoute: any) => {

packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
1414
apply: (wrappingTarget, thisArg, args: Parameters<H>) => {
1515
const req = args[0];
1616

17-
const activeSpan = !!getCurrentHub().getScope()?.getSpan();
17+
const activeSpan = getCurrentHub().getScope().getSpan();
1818

1919
const wrappedHandler = withEdgeWrapping(wrappingTarget, {
2020
spanDescription:

packages/nextjs/test/edge/withSentryAPI.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('wrapApiHandlerWithSentry', () => {
7373

7474
it('should return a function that starts a span on the current transaction with the correct description when there is an active transaction and no request is being passed', async () => {
7575
const testTransaction = coreSdk.startTransaction({ name: 'testTransaction' });
76-
coreSdk.getCurrentHub().getScope()?.setSpan(testTransaction);
76+
coreSdk.getCurrentHub().getScope().setSpan(testTransaction);
7777

7878
const startChildSpy = jest.spyOn(testTransaction, 'startChild');
7979

@@ -92,6 +92,6 @@ describe('wrapApiHandlerWithSentry', () => {
9292
);
9393

9494
testTransaction.finish();
95-
coreSdk.getCurrentHub().getScope()?.setSpan(undefined);
95+
coreSdk.getCurrentHub().getScope().setSpan(undefined);
9696
});
9797
});

packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ app.use(Sentry.Handlers.tracingHandler());
2525
app.use(cors());
2626

2727
app.get('/test/express', (_req, res) => {
28-
const transaction = Sentry.getCurrentHub().getScope()?.getTransaction();
28+
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
2929
if (transaction) {
3030
transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd';
3131
}

packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app.use(Sentry.Handlers.tracingHandler());
2727
app.use(cors());
2828

2929
app.get('/test/express', (_req, res) => {
30-
const transaction = Sentry.getCurrentHub().getScope()?.getTransaction();
30+
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
3131
if (transaction) {
3232
transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd';
3333
transaction.setMetadata({ source: 'route' });

packages/node/src/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function requestHandler(
163163

164164
// If Scope contains a Single mode Session, it is removed in favor of using Session Aggregates mode
165165
const scope = currentHub.getScope();
166-
if (scope && scope.getSession()) {
166+
if (scope.getSession()) {
167167
scope.setSession();
168168
}
169169
}
@@ -339,7 +339,7 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
339339
return function <T>({ path, type, next, rawInput }: TrpcMiddlewareArguments<T>): T {
340340
const hub = getCurrentHub();
341341
const clientOptions = hub.getClient()?.getOptions();
342-
const sentryTransaction = hub.getScope()?.getTransaction();
342+
const sentryTransaction = hub.getScope().getTransaction();
343343

344344
if (sentryTransaction) {
345345
sentryTransaction.setName(`trpc/${path}`, 'route');

packages/node/src/integrations/http.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ function _createWrappedRequestMethodFactory(
192192
}
193193

194194
let requestSpan: Span | undefined;
195-
let parentSpan: Span | undefined;
196-
197-
const scope = getCurrentHub().getScope();
195+
const parentSpan = getCurrentHub().getScope().getSpan();
198196

199197
const method = requestOptions.method || 'GET';
200198
const requestSpanData: SanitizedRequestData = {
@@ -210,9 +208,7 @@ function _createWrappedRequestMethodFactory(
210208
requestSpanData['http.query'] = requestOptions.search.substring(1);
211209
}
212210

213-
if (scope && tracingOptions && shouldCreateSpan(rawRequestUrl)) {
214-
parentSpan = scope.getSpan();
215-
211+
if (tracingOptions && shouldCreateSpan(rawRequestUrl)) {
216212
if (parentSpan) {
217213
requestSpan = parentSpan.startChild({
218214
description: `${method} ${requestSpanData.url}`,

packages/node/src/sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function startSessionTracking(): void {
276276
// such as calling process.exit() or uncaught exceptions.
277277
// Ref: https://nodejs.org/api/process.html#process_event_beforeexit
278278
process.on('beforeExit', () => {
279-
const session = hub.getScope()?.getSession();
279+
const session = hub.getScope().getSession();
280280
const terminalStates: SessionStatus[] = ['exited', 'crashed'];
281281
// Only call endSession, if the Session exists on Scope and SessionStatus is not a
282282
// Terminal Status i.e. Exited or Crashed because

packages/node/test/handlers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe('tracingHandler', () => {
319319

320320
sentryTracingMiddleware(req, res, next);
321321

322-
const transaction = sentryCore.getCurrentHub().getScope()?.getTransaction();
322+
const transaction = sentryCore.getCurrentHub().getScope().getTransaction();
323323

324324
expect(transaction).toBeDefined();
325325
expect(transaction).toEqual(
@@ -439,7 +439,7 @@ describe('tracingHandler', () => {
439439

440440
sentryTracingMiddleware(req, res, next);
441441

442-
const transaction = sentryCore.getCurrentHub().getScope()?.getTransaction();
442+
const transaction = sentryCore.getCurrentHub().getScope().getTransaction();
443443

444444
expect(transaction?.metadata.request).toEqual(req);
445445
});

packages/node/test/integrations/http.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('tracing', () => {
4949
...customContext,
5050
});
5151

52-
hub.getScope()?.setSpan(transaction);
52+
hub.getScope().setSpan(transaction);
5353

5454
return transaction;
5555
}
@@ -266,7 +266,7 @@ describe('tracing', () => {
266266
function createTransactionAndPutOnScope(hub: Hub) {
267267
addTracingExtensions();
268268
const transaction = hub.startTransaction({ name: 'dogpark' });
269-
hub.getScope()?.setSpan(transaction);
269+
hub.getScope().setSpan(transaction);
270270
return transaction;
271271
}
272272

packages/node/test/integrations/requestdata.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('`RequestData` integration', () => {
126126
type GCPHandler = (req: PolymorphicRequest, res: http.ServerResponse) => void;
127127
const mockGCPWrapper = (origHandler: GCPHandler, options: Record<string, unknown>): GCPHandler => {
128128
const wrappedHandler: GCPHandler = (req, res) => {
129-
getCurrentHub().getScope()?.setSDKProcessingMetadata({
129+
getCurrentHub().getScope().setSDKProcessingMetadata({
130130
request: req,
131131
requestDataOptionsFromGCPWrapper: options,
132132
});

packages/opentelemetry-node/test/spanprocessor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('SentrySpanProcessor', () => {
114114
expect(sentrySpan?.spanId).toEqual(childOtelSpan.spanContext().spanId);
115115
expect(sentrySpan?.parentSpanId).toEqual(sentrySpanTransaction?.spanId);
116116

117-
expect(hub.getScope()?.getSpan()).toBeUndefined();
117+
expect(hub.getScope().getSpan()).toBeUndefined();
118118

119119
child.end(endTime);
120120

packages/remix/src/utils/instrumentServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export function startRequestHandlerTransaction(
306306
},
307307
});
308308

309-
hub.getScope()?.setSpan(transaction);
309+
hub.getScope().setSpan(transaction);
310310
return transaction;
311311
}
312312

packages/replay/src/util/sendReplayRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function sendReplayRequest({
3434
const transport = client && client.getTransport();
3535
const dsn = client && client.getDsn();
3636

37-
if (!client || !scope || !transport || !dsn || !session.sampled) {
37+
if (!client || !transport || !dsn || !session.sampled) {
3838
return;
3939
}
4040

packages/replay/test/integration/coreHandlers/handleScope.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Integration | coreHandlers | handleScope', () => {
2323

2424
expect(mockHandleScopeListener).toHaveBeenCalledTimes(1);
2525

26-
getCurrentHub().getScope()?.addBreadcrumb({ category: 'console', message: 'testing' });
26+
getCurrentHub().getScope().addBreadcrumb({ category: 'console', message: 'testing' });
2727

2828
expect(mockHandleScope).toHaveBeenCalledTimes(1);
2929
expect(mockHandleScope).toHaveReturnedWith(expect.objectContaining({ category: 'console', message: 'testing' }));
@@ -32,7 +32,7 @@ describe('Integration | coreHandlers | handleScope', () => {
3232

3333
// This will trigger breadcrumb/scope listener, but handleScope should return
3434
// null because breadcrumbs has not changed
35-
getCurrentHub().getScope()?.setUser({ email: 'foo@foo.com' });
35+
getCurrentHub().getScope().setUser({ email: 'foo@foo.com' });
3636
expect(mockHandleScope).toHaveBeenCalledTimes(1);
3737
expect(mockHandleScope).toHaveReturnedWith(null);
3838
});

packages/svelte/src/performance.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,5 @@ function recordUpdateSpans(componentName: string, initSpan?: Span): void {
9090
}
9191

9292
function getActiveTransaction(): Transaction | undefined {
93-
const currentHub = getCurrentHub();
94-
const scope = currentHub && currentHub.getScope();
95-
return scope && scope.getTransaction();
93+
return getCurrentHub().getScope().getTransaction();
9694
}

packages/tracing-internal/src/browser/request.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ export function fetchCallback(
195195
return;
196196
}
197197

198-
const currentScope = getCurrentHub().getScope();
199-
const currentSpan = currentScope && currentScope.getSpan();
198+
const currentSpan = getCurrentHub().getScope().getSpan();
200199
const activeTransaction = currentSpan && currentSpan.transaction;
201200

202201
if (currentSpan && activeTransaction) {
@@ -336,8 +335,7 @@ export function xhrCallback(
336335
return;
337336
}
338337

339-
const currentScope = getCurrentHub().getScope();
340-
const currentSpan = currentScope && currentScope.getSpan();
338+
const currentSpan = getCurrentHub().getScope().getSpan();
341339
const activeTransaction = currentSpan && currentSpan.transaction;
342340

343341
if (currentSpan && activeTransaction) {

packages/tracing/test/hub.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('Hub', () => {
4444
scope.setSpan(transaction);
4545
});
4646

47-
expect(hub.getScope()?.getTransaction()).toBe(transaction);
47+
expect(hub.getScope().getTransaction()).toBe(transaction);
4848
});
4949

5050
it('should find a transaction which has been set on the scope if sampled = false', () => {
@@ -57,7 +57,7 @@ describe('Hub', () => {
5757
scope.setSpan(transaction);
5858
});
5959

60-
expect(hub.getScope()?.getTransaction()).toBe(transaction);
60+
expect(hub.getScope().getTransaction()).toBe(transaction);
6161
});
6262

6363
it("should not find an open transaction if it's not on the scope", () => {
@@ -66,7 +66,7 @@ describe('Hub', () => {
6666
makeMain(hub);
6767
hub.startTransaction({ name: 'dogpark' });
6868

69-
expect(hub.getScope()?.getTransaction()).toBeUndefined();
69+
expect(hub.getScope().getTransaction()).toBeUndefined();
7070
});
7171
});
7272

packages/vue/src/tracing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ const HOOKS: { [key in Operation]: Hook[] } = {
3030

3131
/** Grabs active transaction off scope, if any */
3232
export function getActiveTransaction(): Transaction | undefined {
33-
const scope = getCurrentHub().getScope();
34-
return scope && scope.getTransaction();
33+
return getCurrentHub().getScope().getTransaction();
3534
}
3635

3736
/** Finish top-level span and activity with a debounce configured using `timeout` option */

0 commit comments

Comments
 (0)