Skip to content

Commit 9690d7d

Browse files
authored
fix(tracing): Only create request span if there is active span (#10375)
This was a regression introduced with #10236, we shouldn't arbitrarily call `startInactiveSpan` given we create transactions under the hood currently.
1 parent 7dc9260 commit 9690d7d

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

packages/tracing-internal/src/browser/request.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable max-lines */
22
import {
33
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4+
getActiveSpan,
45
getClient,
56
getCurrentScope,
67
getDynamicSamplingContextFromClient,
@@ -279,18 +280,21 @@ export function xhrCallback(
279280
const scope = getCurrentScope();
280281
const isolationScope = getIsolationScope();
281282

282-
const span = shouldCreateSpanResult
283-
? startInactiveSpan({
284-
attributes: {
285-
type: 'xhr',
286-
'http.method': sentryXhrData.method,
287-
url: sentryXhrData.url,
288-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser',
289-
},
290-
name: `${sentryXhrData.method} ${sentryXhrData.url}`,
291-
op: 'http.client',
292-
})
293-
: undefined;
283+
// only create a child span if there is an active span. This is because
284+
// `startInactiveSpan` can still create a transaction under the hood
285+
const span =
286+
shouldCreateSpanResult && getActiveSpan()
287+
? startInactiveSpan({
288+
attributes: {
289+
type: 'xhr',
290+
'http.method': sentryXhrData.method,
291+
url: sentryXhrData.url,
292+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser',
293+
},
294+
name: `${sentryXhrData.method} ${sentryXhrData.url}`,
295+
op: 'http.client',
296+
})
297+
: undefined;
294298

295299
if (span) {
296300
xhr.__sentry_xhr_span_id__ = span.spanContext().spanId;

packages/tracing-internal/src/common/fetch.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
3+
getActiveSpan,
34
getClient,
45
getCurrentScope,
56
getDynamicSamplingContextFromClient,
@@ -81,18 +82,21 @@ export function instrumentFetchRequest(
8182

8283
const { method, url } = handlerData.fetchData;
8384

84-
const span = shouldCreateSpanResult
85-
? startInactiveSpan({
86-
attributes: {
87-
url,
88-
type: 'fetch',
89-
'http.method': method,
90-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
91-
},
92-
name: `${method} ${url}`,
93-
op: 'http.client',
94-
})
95-
: undefined;
85+
// only create a child span if there is an active span. This is because
86+
// `startInactiveSpan` can still create a transaction under the hood
87+
const span =
88+
shouldCreateSpanResult && getActiveSpan()
89+
? startInactiveSpan({
90+
attributes: {
91+
url,
92+
type: 'fetch',
93+
'http.method': method,
94+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
95+
},
96+
name: `${method} ${url}`,
97+
op: 'http.client',
98+
})
99+
: undefined;
96100

97101
if (span) {
98102
handlerData.fetchData.__span = span.spanContext().spanId;

0 commit comments

Comments
 (0)