Skip to content

fix(browser): Respect manually set sentry tracing headers in XHR requests #16184

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 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const xhr = new XMLHttpRequest();

xhr.open('GET', 'http://sentry-test-site.example/1');
xhr.setRequestHeader('X-Test-Header', 'existing-header');
xhr.setRequestHeader('baggage', 'someVendor-foo=bar');

xhr.send();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from '@playwright/test';
import { TRACEPARENT_REGEXP } from '@sentry/core';
import { sentryTest } from '../../../../utils/fixtures';
import { shouldSkipTracingTest } from '../../../../utils/helpers';

sentryTest('merges `baggage` headers of pre-existing non-sentry XHR requests', async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });

const requestPromise = page.waitForRequest('http://sentry-test-site.example/1');

await page.goto(url);

const request = await requestPromise;

const requestHeaders = request.headers();
expect(requestHeaders).toMatchObject({
'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP),
baggage: expect.stringMatching(/^someVendor-foo=bar, sentry-.*$/),
'x-test-header': 'existing-header',
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const xhr = new XMLHttpRequest();

xhr.open('GET', 'http://sentry-test-site.example/1');
xhr.setRequestHeader('X-Test-Header', 'existing-header');
xhr.setRequestHeader('sentry-trace', '123-abc-1');
xhr.setRequestHeader('baggage', 'sentry-release=1.1.1');

xhr.send();
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../../utils/fixtures';
import { shouldSkipTracingTest } from '../../../../utils/helpers';

sentryTest(
'attaches manually passed in `sentry-trace` and `baggage` headers to XHR requests',
async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });

const requestPromise = page.waitForRequest('http://sentry-test-site.example/1');

await page.goto(url);

const request = await requestPromise;

const requestHeaders = request.headers();
expect(requestHeaders).toMatchObject({
'sentry-trace': '123-abc-1',
baggage: 'sentry-release=1.1.1',
'x-test-header': 'existing-header',
});
},
);
25 changes: 20 additions & 5 deletions packages/browser/src/tracing/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,36 @@ function setHeaderOnXhr(
sentryTraceHeader: string,
sentryBaggageHeader: string | undefined,
): void {
const originalHeaders = xhr.__sentry_xhr_v3__?.request_headers;

if (originalHeaders?.['sentry-trace']) {
// bail if a sentry-trace header is already set
return;
}

try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
xhr.setRequestHeader!('sentry-trace', sentryTraceHeader);
if (sentryBaggageHeader) {
// From MDN: "If this method is called several times with the same header, the values are merged into one single request header."
// We can therefore simply set a baggage header without checking what was there before
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
xhr.setRequestHeader!('baggage', sentryBaggageHeader);
// bail if a pre-existing baggage header is set and already contains sentry values
const originalBaggageHeader = originalHeaders?.['baggage'];
if (!originalBaggageHeader || !baggageHeaderHasSentryValues(originalBaggageHeader)) {
// From MDN: "If this method is called several times with the same header, the values are merged into one single request header."
// We can therefore simply set a baggage header without checking what was there before
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
xhr.setRequestHeader!('baggage', sentryBaggageHeader);
}
}
} catch (_) {
// Error: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
}
}

function baggageHeaderHasSentryValues(baggageHeader: string): boolean {
return baggageHeader.split(',').some(value => value.startsWith('sentry-'));
}

function getFullURL(url: string): string | undefined {
try {
// By adding a base URL to new URL(), this will also work for relative urls
Expand Down
Loading