Skip to content

Commit

Permalink
test(V7/browser-integration-tests): Check for sentry-trace header i…
Browse files Browse the repository at this point in the history
…n TwP (#11555)
  • Loading branch information
Lms24 committed Apr 12, 2024
1 parent f6b284d commit 8abd97c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module.exports = [
name: '@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped)',
path: 'packages/browser/build/bundles/bundle.tracing.es5.min.js',
gzip: true,
limit: '40 KB',
limit: '41 KB',
},

// React
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ window.Sentry = Sentry;
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
// disable pageload transaction
integrations: [Sentry.BrowserTracing({ tracingOrigins: ['http://example.com'], startTransactionOnPageLoad: false })],
integrations: [
new Sentry.BrowserTracing({ tracingOrigins: ['http://example.com'], startTransactionOnPageLoad: false }),
],
tracesSampleRate: 1,
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sentryTest } from '../../../../utils/fixtures';
import { envelopeUrlRegex, shouldSkipTracingTest } from '../../../../utils/helpers';

sentryTest(
'there should be no span created for fetch requests with no active span',
'should not create span for fetch requests with no active span but should attach sentry-trace header',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
Expand All @@ -13,7 +13,12 @@ sentryTest(
const url = await getLocalTestPath({ testDir: __dirname });

let requestCount = 0;
const sentryTraceHeaders: string[] = [];
page.on('request', request => {
const sentryTraceHeader = request.headers()['sentry-trace'];
if (sentryTraceHeader) {
sentryTraceHeaders.push(sentryTraceHeader);
}
expect(envelopeUrlRegex.test(request.url())).toBe(false);
requestCount++;
});
Expand All @@ -31,5 +36,12 @@ sentryTest(
} else {
expect(requestCount).toBe(6);
}

expect(sentryTraceHeaders).toHaveLength(3);
expect(sentryTraceHeaders).toEqual([
expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/),
expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/),
expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/),
]);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
// disable pageload transaction
integrations: [new Sentry.BrowserTracing({ tracePropagationTargets: ['http://example.com'] })],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fetch('http://example.com/0').then(fetch('http://example.com/1').then(fetch('http://example.com/2')));
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { envelopeUrlRegex, shouldSkipTracingTest } from '../../../../utils/helpers';

sentryTest(
'should not create span for fetch requests with no active span but should attach sentry-trace header if no sample rate is set',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

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

let requestCount = 0;
const sentryTraceHeaders: string[] = [];
page.on('request', request => {
const sentryTraceHeader = request.headers()['sentry-trace'];
if (sentryTraceHeader) {
sentryTraceHeaders.push(sentryTraceHeader);
}
expect(envelopeUrlRegex.test(request.url())).toBe(false);
requestCount++;
});

await page.goto(url);

// Here are the requests that should exist:
// 1. HTML page
// 2. Init JS bundle
// 3. Subject JS bundle
// 4 [OPTIONAl] CDN JS bundle
// and then 3 fetch requests
if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle_')) {
expect(requestCount).toBe(7);
} else {
expect(requestCount).toBe(6);
}

// TODO: This is incorrect behavior. Even if `tracesSampleRate` is not set (which in browser
// realistically is the only way to truly get "Tracing without performance"), we should still
// attach the `sentry-trace` header to the fetch requests.
// Right now, we don't do this, as this test demonstrates.
expect(sentryTraceHeaders).toHaveLength(0);
},
);

0 comments on commit 8abd97c

Please sign in to comment.