Skip to content

ref(core): Remove guards around scope usage #7554

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 1 commit into from
Mar 21, 2023
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
4 changes: 2 additions & 2 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export class Hub implements HubInterface {
*/
public configureScope(callback: (scope: Scope) => void): void {
const { scope, client } = this.getStackTop();
if (scope && client) {
if (client) {
callback(scope);
}
}
Expand Down Expand Up @@ -413,7 +413,7 @@ export class Hub implements HubInterface {
const session = makeSession({
release,
environment,
...(scope && { user: scope.getUser() }),
user: scope.getUser(),
...(userAgent && { userAgent }),
...context,
});
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
}
const hub = getCurrentHub();
const scope = hub.getScope();
if (scope) {
scope.update(options.initialScope);
}
scope.update(options.initialScope);

const client = new clientClass(options);
hub.bindClient(client);
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/sessionflusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ export class SessionFlusher implements SessionFlusherLike {
return;
}
const scope = getCurrentHub().getScope();
const requestSession = scope && scope.getRequestSession();
const requestSession = scope.getRequestSession();

if (requestSession && requestSession.status) {
this._incrementSessionStatusCount(requestSession.status, new Date());
// This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in
// case captureRequestSession is called more than once to prevent double count
if (scope) {
scope.setRequestSession(undefined);
}
scope.setRequestSession(undefined);
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
}
}
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/tracing/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import { Transaction } from './transaction';
/** Returns all trace headers that are currently on the top scope. */
function traceHeaders(this: Hub): { [key: string]: string } {
const scope = this.getScope();
if (scope) {
const span = scope.getSpan();
if (span) {
return {
const span = scope.getSpan();

return span
? {
'sentry-trace': span.toTraceparent(),
};
}
}
return {};
}
: {};
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/tracing/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,7 @@ export class IdleTransaction extends Transaction {
*/
function clearActiveTransaction(hub: Hub): void {
const scope = hub.getScope();
if (scope) {
const transaction = scope.getTransaction();
if (transaction) {
scope.setSpan(undefined);
}
if (scope.getTransaction()) {
scope.setSpan(undefined);
}
}
3 changes: 1 addition & 2 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
const maybeSampleRate = this.metadata.sampleRate;
const sample_rate = maybeSampleRate !== undefined ? maybeSampleRate.toString() : undefined;

const scope = hub.getScope();
const { segment: user_segment } = (scope && scope.getUser()) || {};
const { segment: user_segment } = hub.getScope().getUser() || {};

const source = this.metadata.source;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tracing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export { TRACEPARENT_REGEXP, extractTraceparentData } from '@sentry/utils';
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
const hub = maybeHub || getCurrentHub();
const scope = hub.getScope();
return scope && (scope.getTransaction() as T | undefined);
return scope.getTransaction() as T | undefined;
}

// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils
Expand Down