Skip to content

test(V7/browser-integration-tests): Check for sentry-trace header in TwP #11555

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
Apr 12, 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
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 }),
],
Comment on lines +8 to +10
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was broken before but still succeeded because our assertions didn't check anything specific other than ensuring that requests were made.

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);
},
);