Skip to content

Commit ed136de

Browse files
author
Luca Forstner
authored
fix(tracing): Attach request instrumentation span to active span instead of current transaction (#6778)
1 parent 8ffc57f commit ed136de

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

packages/tracing/src/browser/request.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable max-lines */
2+
import { getCurrentHub } from '@sentry/core';
23
import type { DynamicSamplingContext, Span } from '@sentry/types';
34
import {
45
addInstrumentationHandler,
@@ -8,7 +9,7 @@ import {
89
stringMatchesSomePattern,
910
} from '@sentry/utils';
1011

11-
import { getActiveTransaction, hasTracingEnabled } from '../utils';
12+
import { hasTracingEnabled } from '../utils';
1213

1314
export const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\//];
1415

@@ -186,9 +187,12 @@ export function fetchCallback(
186187
return;
187188
}
188189

189-
const activeTransaction = getActiveTransaction();
190-
if (activeTransaction) {
191-
const span = activeTransaction.startChild({
190+
const currentScope = getCurrentHub().getScope();
191+
const currentSpan = currentScope && currentScope.getSpan();
192+
const activeTransaction = currentSpan && currentSpan.transaction;
193+
194+
if (currentSpan && activeTransaction) {
195+
const span = currentSpan.startChild({
192196
data: {
193197
...handlerData.fetchData,
194198
type: 'fetch',
@@ -320,10 +324,12 @@ export function xhrCallback(
320324
return;
321325
}
322326

323-
// if not, create a new span to track it
324-
const activeTransaction = getActiveTransaction();
325-
if (activeTransaction) {
326-
const span = activeTransaction.startChild({
327+
const currentScope = getCurrentHub().getScope();
328+
const currentSpan = currentScope && currentScope.getSpan();
329+
const activeTransaction = currentSpan && currentSpan.transaction;
330+
331+
if (currentSpan && activeTransaction) {
332+
const span = currentSpan.startChild({
327333
data: {
328334
...xhr.data,
329335
type: 'xhr',

0 commit comments

Comments
 (0)