Skip to content

Commit aadf872

Browse files
AbhiPrasadonurtemizkan
authored andcommitted
ref(tracing): Remove optional chaining (#4293)
1 parent fc5b7d6 commit aadf872

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class MetricsInstrumentation {
2323
private _clsEntry: LayoutShift | undefined;
2424

2525
public constructor(private _reportAllChanges: boolean = false) {
26-
if (!isNodeEnv() && global?.performance && global?.document) {
26+
if (!isNodeEnv() && global && global.performance && global.document) {
2727
if (global.performance.mark) {
2828
global.performance.mark('sentry-tracing-init');
2929
}

packages/tracing/src/browser/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ export function xhrCallback(
214214
): void {
215215
if (
216216
!hasTracingEnabled() ||
217-
handlerData.xhr?.__sentry_own_request__ ||
218-
!(handlerData.xhr?.__sentry_xhr__ && shouldCreateSpan(handlerData.xhr.__sentry_xhr__.url))
217+
(handlerData.xhr && handlerData.xhr.__sentry_own_request__) ||
218+
!(handlerData.xhr && handlerData.xhr.__sentry_xhr__ && shouldCreateSpan(handlerData.xhr.__sentry_xhr__.url))
219219
) {
220220
return;
221221
}

packages/tracing/src/hubextensions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ function _startTransaction(
166166
transactionContext: TransactionContext,
167167
customSamplingContext?: CustomSamplingContext,
168168
): Transaction {
169-
const options = this.getClient()?.getOptions() || {};
169+
const client = this.getClient();
170+
const options = (client && client.getOptions()) || {};
170171

171172
let transaction = new Transaction(transactionContext, this);
172173
transaction = sample(transaction, options, {
@@ -175,7 +176,7 @@ function _startTransaction(
175176
...customSamplingContext,
176177
});
177178
if (transaction.sampled) {
178-
transaction.initSpanRecorder(options._experiments?.maxSpans as number);
179+
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
179180
}
180181
return transaction;
181182
}
@@ -190,7 +191,8 @@ export function startIdleTransaction(
190191
onScope?: boolean,
191192
customSamplingContext?: CustomSamplingContext,
192193
): IdleTransaction {
193-
const options = hub.getClient()?.getOptions() || {};
194+
const client = hub.getClient();
195+
const options = (client && client.getOptions()) || {};
194196

195197
let transaction = new IdleTransaction(transactionContext, hub, idleTimeout, onScope);
196198
transaction = sample(transaction, options, {
@@ -199,7 +201,7 @@ export function startIdleTransaction(
199201
...customSamplingContext,
200202
});
201203
if (transaction.sampled) {
202-
transaction.initSpanRecorder(options._experiments?.maxSpans as number);
204+
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
203205
}
204206
return transaction;
205207
}

packages/tracing/src/transaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ export class Transaction extends SpanClass implements TransactionInterface {
104104
// At this point if `sampled !== true` we want to discard the transaction.
105105
logger.log('[Tracing] Discarding transaction because its trace was not chosen to be sampled.');
106106

107-
this._hub
108-
.getClient()
109-
?.getTransport?.()
110-
.recordLostEvent?.(Outcome.SampleRate, 'transaction');
111-
107+
const client = this._hub.getClient();
108+
const transport = client && client.getTransport && client.getTransport();
109+
if (transport && transport.recordLostEvent) {
110+
transport.recordLostEvent(Outcome.SampleRate, 'transaction');
111+
}
112112
return undefined;
113113
}
114114

packages/tracing/src/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ export const TRACEPARENT_REGEXP = new RegExp(
1414
*
1515
* Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config.
1616
*/
17-
export function hasTracingEnabled(
18-
options: Options | undefined = getCurrentHub()
19-
.getClient()
20-
?.getOptions(),
21-
): boolean {
17+
export function hasTracingEnabled(maybeOptions?: Options | undefined): boolean {
18+
const client = getCurrentHub().getClient();
19+
const options = maybeOptions || (client && client.getOptions());
2220
return !!options && ('tracesSampleRate' in options || 'tracesSampler' in options);
2321
}
2422

@@ -48,8 +46,10 @@ export function extractTraceparentData(traceparent: string): TraceparentData | u
4846
}
4947

5048
/** Grabs active transaction off scope, if any */
51-
export function getActiveTransaction<T extends Transaction>(hub: Hub = getCurrentHub()): T | undefined {
52-
return hub?.getScope()?.getTransaction() as T | undefined;
49+
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
50+
const hub = maybeHub || getCurrentHub();
51+
const scope = hub.getScope();
52+
return scope && (scope.getTransaction() as T | undefined);
5353
}
5454

5555
/**

0 commit comments

Comments
 (0)