Skip to content

Commit

Permalink
fix(tracing): Attach request instrumentation span to active span inst…
Browse files Browse the repository at this point in the history
…ead of current transaction (#6778)
  • Loading branch information
lforst committed Jan 16, 2023
1 parent 8ffc57f commit ed136de
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/tracing/src/browser/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-lines */
import { getCurrentHub } from '@sentry/core';
import type { DynamicSamplingContext, Span } from '@sentry/types';
import {
addInstrumentationHandler,
Expand All @@ -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', /^\//];

Expand Down Expand Up @@ -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();
const activeTransaction = currentSpan && currentSpan.transaction;

if (currentSpan && activeTransaction) {
const span = currentSpan.startChild({
data: {
...handlerData.fetchData,
type: 'fetch',
Expand Down Expand Up @@ -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();
const activeTransaction = currentSpan && currentSpan.transaction;

if (currentSpan && activeTransaction) {
const span = currentSpan.startChild({
data: {
...xhr.data,
type: 'xhr',
Expand Down

0 comments on commit ed136de

Please sign in to comment.