Skip to content

fix(tracing): Only create request span if there is active span #10375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions packages/tracing-internal/src/browser/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-lines */
import {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
getActiveSpan,
getClient,
getCurrentScope,
getDynamicSamplingContextFromClient,
Expand Down Expand Up @@ -279,18 +280,21 @@ export function xhrCallback(
const scope = getCurrentScope();
const isolationScope = getIsolationScope();

const span = shouldCreateSpanResult
? startInactiveSpan({
attributes: {
type: 'xhr',
'http.method': sentryXhrData.method,
url: sentryXhrData.url,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser',
},
name: `${sentryXhrData.method} ${sentryXhrData.url}`,
op: 'http.client',
})
: undefined;
// only create a child span if there is an active span. This is because
// `startInactiveSpan` can still create a transaction under the hood
const span =
shouldCreateSpanResult && getActiveSpan()
? startInactiveSpan({
attributes: {
type: 'xhr',
'http.method': sentryXhrData.method,
url: sentryXhrData.url,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.browser',
},
name: `${sentryXhrData.method} ${sentryXhrData.url}`,
op: 'http.client',
})
: undefined;

if (span) {
xhr.__sentry_xhr_span_id__ = span.spanContext().spanId;
Expand Down
28 changes: 16 additions & 12 deletions packages/tracing-internal/src/common/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
getActiveSpan,
getClient,
getCurrentScope,
getDynamicSamplingContextFromClient,
Expand Down Expand Up @@ -81,18 +82,21 @@ export function instrumentFetchRequest(

const { method, url } = handlerData.fetchData;

const span = shouldCreateSpanResult
? startInactiveSpan({
attributes: {
url,
type: 'fetch',
'http.method': method,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
},
name: `${method} ${url}`,
op: 'http.client',
})
: undefined;
// only create a child span if there is an active span. This is because
// `startInactiveSpan` can still create a transaction under the hood
const span =
shouldCreateSpanResult && getActiveSpan()
? startInactiveSpan({
attributes: {
url,
type: 'fetch',
'http.method': method,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
},
name: `${method} ${url}`,
op: 'http.client',
})
: undefined;

if (span) {
handlerData.fetchData.__span = span.spanContext().spanId;
Expand Down