Skip to content

Commit 4f3fc0a

Browse files
Lms24andreiborza
authored andcommitted
fix(browser): Only start http.client spans if there is an active parent span (#11974)
We introduced sending standalone `http.client` spans in #11783. Subsequently, we had to revert this change due to internal timeline problems (😢). However, in the revert PR (#11879) we missed that originally, we checked for an active parent span and only then created an `http.client` span. The result were `http.client` _transactions_ which we also don't want. This patch fixes that by re-introducing the `hasParent` check as it was pre-#11783.
1 parent 06f47c3 commit 4f3fc0a

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

packages/browser/src/tracing/request.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SEMANTIC_ATTRIBUTE_SENTRY_OP,
88
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
99
SentryNonRecordingSpan,
10+
getActiveSpan,
1011
getClient,
1112
getCurrentScope,
1213
getDynamicSamplingContextFromClient,
@@ -324,20 +325,23 @@ export function xhrCallback(
324325
const fullUrl = getFullURL(sentryXhrData.url);
325326
const host = fullUrl ? parseUrl(fullUrl).host : undefined;
326327

327-
const span = shouldCreateSpanResult
328-
? startInactiveSpan({
329-
name: `${sentryXhrData.method} ${sentryXhrData.url}`,
330-
attributes: {
331-
type: 'xhr',
332-
'http.method': sentryXhrData.method,
333-
'http.url': fullUrl,
334-
url: sentryXhrData.url,
335-
'server.address': host,
336-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser',
337-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
338-
},
339-
})
340-
: new SentryNonRecordingSpan();
328+
const hasParent = !!getActiveSpan();
329+
330+
const span =
331+
shouldCreateSpanResult && hasParent
332+
? startInactiveSpan({
333+
name: `${sentryXhrData.method} ${sentryXhrData.url}`,
334+
attributes: {
335+
type: 'xhr',
336+
'http.method': sentryXhrData.method,
337+
'http.url': fullUrl,
338+
url: sentryXhrData.url,
339+
'server.address': host,
340+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser',
341+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
342+
},
343+
})
344+
: new SentryNonRecordingSpan();
341345

342346
xhr.__sentry_xhr_span_id__ = span.spanContext().spanId;
343347
spans[xhr.__sentry_xhr_span_id__] = span;
@@ -348,9 +352,10 @@ export function xhrCallback(
348352
addTracingHeadersToXhrRequest(
349353
xhr,
350354
client,
351-
// If performance is disabled (TWP), we do not want to use the span as base for the trace headers,
355+
// If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction),
356+
// we do not want to use the span as base for the trace headers,
352357
// which means that the headers will be generated from the scope and the sampling decision is deferred
353-
hasTracingEnabled() ? span : undefined,
358+
hasTracingEnabled() && hasParent ? span : undefined,
354359
);
355360
}
356361

packages/core/src/fetch.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from './tracing';
1818
import { SentryNonRecordingSpan } from './tracing/sentryNonRecordingSpan';
1919
import { hasTracingEnabled } from './utils/hasTracingEnabled';
20-
import { spanToTraceHeader } from './utils/spanUtils';
20+
import { getActiveSpan, spanToTraceHeader } from './utils/spanUtils';
2121

2222
type PolymorphicRequestHeaders =
2323
| Record<string, string | undefined>
@@ -70,20 +70,23 @@ export function instrumentFetchRequest(
7070
const fullUrl = getFullURL(url);
7171
const host = fullUrl ? parseUrl(fullUrl).host : undefined;
7272

73-
const span = shouldCreateSpanResult
74-
? startInactiveSpan({
75-
name: `${method} ${url}`,
76-
attributes: {
77-
url,
78-
type: 'fetch',
79-
'http.method': method,
80-
'http.url': fullUrl,
81-
'server.address': host,
82-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
83-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
84-
},
85-
})
86-
: new SentryNonRecordingSpan();
73+
const hasParent = !!getActiveSpan();
74+
75+
const span =
76+
shouldCreateSpanResult && hasParent
77+
? startInactiveSpan({
78+
name: `${method} ${url}`,
79+
attributes: {
80+
url,
81+
type: 'fetch',
82+
'http.method': method,
83+
'http.url': fullUrl,
84+
'server.address': host,
85+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
86+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
87+
},
88+
})
89+
: new SentryNonRecordingSpan();
8790

8891
handlerData.fetchData.__span = span.spanContext().spanId;
8992
spans[span.spanContext().spanId] = span;
@@ -102,9 +105,10 @@ export function instrumentFetchRequest(
102105
client,
103106
scope,
104107
options,
105-
// If performance is disabled (TWP), we do not want to use the span as base for the trace headers,
108+
// If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction),
109+
// we do not want to use the span as base for the trace headers,
106110
// which means that the headers will be generated from the scope and the sampling decision is deferred
107-
hasTracingEnabled() ? span : undefined,
111+
hasTracingEnabled() && hasParent ? span : undefined,
108112
);
109113
}
110114

0 commit comments

Comments
 (0)