From 76bd029f7c0623b8c069a112772b21eee90b7ce4 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Sat, 14 Jan 2023 11:53:14 +0000 Subject: [PATCH 1/2] fix(tracing): Attach request instrumentation span to active span instead of current transaction --- packages/tracing/src/browser/request.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/tracing/src/browser/request.ts b/packages/tracing/src/browser/request.ts index ca6c15b0e96d..c3f53f74d256 100644 --- a/packages/tracing/src/browser/request.ts +++ b/packages/tracing/src/browser/request.ts @@ -1,4 +1,5 @@ /* eslint-disable max-lines */ +import { getCurrentHub } from '@sentry/core'; import type { DynamicSamplingContext, Span } from '@sentry/types'; import { addInstrumentationHandler, @@ -8,7 +9,7 @@ import { stringMatchesSomePattern, } from '@sentry/utils'; -import { getActiveTransaction, hasTracingEnabled } from '../utils'; +import { hasTracingEnabled } from '../utils'; export const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\//]; @@ -186,9 +187,12 @@ export function fetchCallback( return; } - const activeTransaction = getActiveTransaction(); - if (activeTransaction) { - const span = activeTransaction.startChild({ + const currentScope = getCurrentHub().getScope(); + const currentSpan = currentScope ? currentScope.getSpan() : undefined; + const activeTransaction = currentSpan ? currentSpan.transaction : undefined; + + if (currentSpan && activeTransaction) { + const span = currentSpan.startChild({ data: { ...handlerData.fetchData, type: 'fetch', @@ -320,10 +324,12 @@ export function xhrCallback( return; } - // if not, create a new span to track it - const activeTransaction = getActiveTransaction(); - if (activeTransaction) { - const span = activeTransaction.startChild({ + const currentScope = getCurrentHub().getScope(); + const currentSpan = currentScope ? currentScope.getSpan() : undefined; + const activeTransaction = currentSpan ? currentSpan.transaction : undefined; + + if (currentSpan && activeTransaction) { + const span = currentSpan.startChild({ data: { ...xhr.data, type: 'xhr', From e56c6b8aa80becfe06889a1de9f4c804be0f4193 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 11:45:05 +0000 Subject: [PATCH 2/2] Short circuit --- packages/tracing/src/browser/request.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/tracing/src/browser/request.ts b/packages/tracing/src/browser/request.ts index c3f53f74d256..c8b9411e7c3f 100644 --- a/packages/tracing/src/browser/request.ts +++ b/packages/tracing/src/browser/request.ts @@ -188,8 +188,8 @@ export function fetchCallback( } const currentScope = getCurrentHub().getScope(); - const currentSpan = currentScope ? currentScope.getSpan() : undefined; - const activeTransaction = currentSpan ? currentSpan.transaction : undefined; + const currentSpan = currentScope && currentScope.getSpan(); + const activeTransaction = currentSpan && currentSpan.transaction; if (currentSpan && activeTransaction) { const span = currentSpan.startChild({ @@ -325,8 +325,8 @@ export function xhrCallback( } const currentScope = getCurrentHub().getScope(); - const currentSpan = currentScope ? currentScope.getSpan() : undefined; - const activeTransaction = currentSpan ? currentSpan.transaction : undefined; + const currentSpan = currentScope && currentScope.getSpan(); + const activeTransaction = currentSpan && currentSpan.transaction; if (currentSpan && activeTransaction) { const span = currentSpan.startChild({