Skip to content

Commit 40654a5

Browse files
msonnbcodex
andcommitted
test(browser): synchronize tracing tests with telemetry
Co-Authored-By: GPT-6 <codex@openai.com>
1 parent f791e61 commit 40654a5

8 files changed

Lines changed: 66 additions & 74 deletions

File tree

  • dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration
    • backgroundtab-pageload-static
    • http-timings-static
    • linked-traces-static/consistent-sampling
    • linked-traces/consistent-sampling/tracesSampler-precedence
    • long-animation-frame-enabled-static
    • long-tasks-and-animation-frame-enabled-static
    • spotlight-interaction-filter-static
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import { PAGELOAD } from '@sentry/conventions/op';
12
import { SENTRY_CANCELLATION_REASON } from '@sentry/conventions/attributes';
23
import { expect } from '@playwright/test';
3-
import type { Event } from '@sentry/core';
44
import { sentryTest } from '../../../../utils/fixtures';
5-
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
66

77
sentryTest('should finish pageload transaction when the page goes background', async ({ getLocalTestUrl, page }) => {
88
if (shouldSkipTracingTest()) {
99
sentryTest.skip();
1010
}
1111
const url = await getLocalTestUrl({ testDir: __dirname });
1212

13+
const pageloadPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
14+
1315
await page.goto(url);
1416
await page.locator('#go-background').click();
1517

16-
const pageloadTransaction = await getFirstSentryEnvelopeRequest<Event>(page);
18+
const pageloadTransaction = envelopeRequestParser(await pageloadPromise);
1719

18-
expect(pageloadTransaction.contexts?.trace?.op).toBe('pageload');
20+
expect(pageloadTransaction.contexts?.trace?.op).toBe(PAGELOAD);
1921
expect(pageloadTransaction.contexts?.trace?.status).toBe('cancelled');
2022
expect(pageloadTransaction.contexts?.trace?.data?.[SENTRY_CANCELLATION_REASON]).toBe('document.hidden');
2123
});

‎dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/http-timings-static/test.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PAGELOAD, HTTP_CLIENT } from '@sentry/conventions/op';
12
import {
23
HTTP_REQUEST_REDIRECT_START,
34
HTTP_REQUEST_REDIRECT_END,
@@ -15,9 +16,8 @@ import {
1516
NETWORK_PROTOCOL_VERSION,
1617
} from '@sentry/conventions/attributes';
1718
import { expect } from '@playwright/test';
18-
import type { Event } from '@sentry/core';
1919
import { sentryTest } from '../../../../utils/fixtures';
20-
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';
20+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
2121

2222
sentryTest('creates fetch spans with http timing', async ({ browserName, getLocalTestUrl, page }) => {
2323
const supportedBrowsers = ['chromium', 'firefox'];
@@ -38,10 +38,11 @@ sentryTest('creates fetch spans with http timing', async ({ browserName, getLoca
3838

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

41-
const envelopes = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url, timeout: 10000 });
42-
const tracingEvent = envelopes[envelopes.length - 1]; // last envelope contains tracing data on all browsers
41+
const pageloadPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
42+
await page.goto(url);
43+
const tracingEvent = envelopeRequestParser(await pageloadPromise);
4344

44-
const requestSpans = tracingEvent.spans?.filter(({ op }) => op === 'http.client');
45+
const requestSpans = tracingEvent.spans?.filter(({ op }) => op === HTTP_CLIENT);
4546

4647
expect(requestSpans).toHaveLength(3);
4748

‎dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/linked-traces-static/consistent-sampling/meta-negative/test.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { extractTraceparentData, parseBaggageHeader } from '@sentry/core';
44
import { sentryTest } from '../../../../../../utils/fixtures';
55
import {
66
envelopeRequestParser,
7-
getMultipleSentryEnvelopeRequests,
7+
envelopeUrlRegex,
8+
getEnvelopeType,
89
hidePage,
910
shouldSkipTracingTest,
1011
waitForClientReportRequest,
@@ -26,13 +27,17 @@ sentryTest.describe('When `consistentTraceSampling` is `true` and page contains
2627
const url = await getLocalTestUrl({ testDir: __dirname });
2728

2829
let txnsReceived = 0;
29-
// @ts-expect-error - no need to return something valid here
30-
getMultipleSentryEnvelopeRequests<Event>(page, 1, { envelopeType: 'transaction' }, () => {
31-
++txnsReceived;
32-
return {};
30+
page.on('request', request => {
31+
if (envelopeUrlRegex.test(request.url()) && getEnvelopeType(request) === 'transaction') {
32+
++txnsReceived;
33+
}
3334
});
3435

35-
const clientReportPromise = waitForClientReportRequest(page);
36+
const clientReportPromise = waitForClientReportRequest(page, report =>
37+
report.discarded_events.some(
38+
event => event.category === 'transaction' && event.reason === 'sample_rate' && event.quantity === 4,
39+
),
40+
);
3641

3742
await sentryTest.step('Initial pageload', async () => {
3843
await page.goto(url);
@@ -90,9 +95,8 @@ sentryTest.describe('When `consistentTraceSampling` is `true` and page contains
9095
});
9196
});
9297

93-
await sentryTest.step('Wait for transactions to be discarded', async () => {
94-
// give it a little longer just in case a txn is pending to be sent
95-
await page.waitForTimeout(1000);
98+
await sentryTest.step('No transactions sent after processing completes', async () => {
99+
expect(await page.evaluate(() => (window as any).Sentry.flush())).toBe(true);
96100
expect(txnsReceived).toEqual(0);
97101
});
98102
},

‎dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/linked-traces-static/consistent-sampling/tracesSampler-precedence/test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PAGELOAD, NAVIGATION } from '@sentry/conventions/op';
12
import { SENTRY_LINK_TYPE } from '@sentry/conventions/attributes';
23
import { expect } from '@playwright/test';
34
import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE } from '@sentry/browser';
@@ -27,7 +28,7 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
2728

2829
const { pageloadTraceContext } = await sentryTest.step('Initial pageload', async () => {
2930
const pageloadRequestPromise = waitForTransactionRequest(page, evt => {
30-
return evt.contexts?.trace?.op === 'pageload';
31+
return evt.contexts?.trace?.op === PAGELOAD;
3132
});
3233
await page.goto(url);
3334

@@ -46,7 +47,6 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
4647

4748
await page.locator('#btn1').click();
4849

49-
await page.waitForTimeout(500);
5050
await hidePage(page);
5151

5252
const clientReport = envelopeRequestParser<ClientReport>(await clientReportPromise);
@@ -68,8 +68,6 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
6868

6969
await page.goto(`${url}#foo`);
7070

71-
await page.waitForTimeout(500);
72-
7371
await hidePage(page);
7472

7573
const clientReport = envelopeRequestParser<ClientReport>(await clientReportPromise);
@@ -115,7 +113,7 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
115113
);
116114

117115
await sentryTest.step('Navigation trace is sampled positively (inherited from previous trace)', async () => {
118-
const navigationRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'navigation');
116+
const navigationRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === NAVIGATION);
119117

120118
await page.goto(`${url}#bar`);
121119

‎dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/linked-traces/consistent-sampling/tracesSampler-precedence/test.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PAGELOAD, NAVIGATION } from '@sentry/conventions/op';
12
import { SENTRY_LINK_TYPE } from '@sentry/conventions/attributes';
23
import { expect } from '@playwright/test';
34
import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE } from '@sentry/browser';
@@ -26,12 +27,12 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
2627
const { pageloadSpan } = await sentryTest.step('Initial pageload', async () => {
2728
const pageloadEnvelopePromise = waitForStreamedSpanEnvelope(
2829
page,
29-
env => !!env[1][0][1].items.find(s => getSpanOp(s) === 'pageload'),
30+
env => !!env[1][0][1].items.find(s => getSpanOp(s) === PAGELOAD),
3031
);
3132
await page.goto(url);
3233

3334
const envelope = await pageloadEnvelopePromise;
34-
const pageloadSpan = envelope[1][0][1].items.find(s => getSpanOp(s) === 'pageload')!;
35+
const pageloadSpan = envelope[1][0][1].items.find(s => getSpanOp(s) === PAGELOAD)!;
3536

3637
expect(pageloadSpan.attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]?.value).toBe(1);
3738
expect(Number(envelope[0].trace?.sample_rand)).toBeGreaterThanOrEqual(0);
@@ -44,7 +45,6 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
4445

4546
await page.locator('#btn1').click();
4647

47-
await page.waitForTimeout(500);
4848
await hidePage(page);
4949

5050
const clientReport = envelopeRequestParser<ClientReport>(await clientReportPromise);
@@ -66,8 +66,6 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
6666

6767
await page.goto(`${url}#foo`);
6868

69-
await page.waitForTimeout(500);
70-
7169
await hidePage(page);
7270

7371
const clientReport = envelopeRequestParser<ClientReport>(await clientReportPromise);
@@ -122,13 +120,13 @@ sentryTest.describe('When `consistentTraceSampling` is `true`', () => {
122120
await sentryTest.step('Navigation trace is sampled positively (inherited from previous trace)', async () => {
123121
const navigationEnvelopePromise = waitForStreamedSpanEnvelope(
124122
page,
125-
env => env[0].trace?.sampled === 'true' && !!env[1][0][1].items.find(s => getSpanOp(s) === 'navigation'),
123+
env => env[0].trace?.sampled === 'true' && !!env[1][0][1].items.find(s => getSpanOp(s) === NAVIGATION),
126124
);
127125

128126
await page.goto(`${url}#bar`);
129127

130128
const envelope = await navigationEnvelopePromise;
131-
const navigationSpan = envelope[1][0][1].items.find(s => getSpanOp(s) === 'navigation')!;
129+
const navigationSpan = envelope[1][0][1].items.find(s => getSpanOp(s) === NAVIGATION)!;
132130

133131
expect(navigationSpan.attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]?.value).toBe(1);
134132
expect(navigationSpan.trace_id).not.toEqual(customTrace2Span.trace_id);

‎dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/long-animation-frame-enabled-static/test.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PAGELOAD, UI_LONG_ANIMATION_FRAME } from '@sentry/conventions/op';
12
import {
23
BROWSER_SCRIPT_INVOKER,
34
CODE_FILE_PATH,
@@ -8,9 +9,8 @@ import {
89
} from '@sentry/conventions/attributes';
910
import type { Route } from '@playwright/test';
1011
import { expect } from '@playwright/test';
11-
import type { Event } from '@sentry/core';
1212
import { sentryTest } from '../../../../utils/fixtures';
13-
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';
13+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
1414

1515
sentryTest(
1616
'should capture long animation frame for top-level script.',
@@ -26,15 +26,13 @@ sentryTest(
2626

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

29-
const promise = getFirstSentryEnvelopeRequest<Event>(page);
29+
const promise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
3030

3131
await page.goto(url);
3232

33-
await new Promise(resolve => setTimeout(resolve, 1000));
33+
const eventData = envelopeRequestParser(await promise);
3434

35-
const eventData = await promise;
36-
37-
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith('ui.long_animation_frame'));
35+
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith(UI_LONG_ANIMATION_FRAME));
3836

3937
expect(uiSpans?.length).toBeGreaterThanOrEqual(1);
4038

@@ -43,15 +41,15 @@ sentryTest(
4341
)!;
4442
expect(topLevelUISpan).toEqual(
4543
expect.objectContaining({
46-
op: 'ui.long_animation_frame',
44+
op: UI_LONG_ANIMATION_FRAME,
4745
description: 'Main UI thread blocked',
4846
parent_span_id: eventData.contexts?.trace?.span_id,
4947
data: {
5048
[CODE_FILE_PATH]: 'https://sentry-test-site.example/path/to/script.js',
5149
[BROWSER_SCRIPT_SOURCE_CHAR_POSITION]: 0,
5250
[BROWSER_SCRIPT_INVOKER]: 'https://sentry-test-site.example/path/to/script.js',
5351
[BROWSER_SCRIPT_INVOKER_TYPE]: 'classic-script',
54-
[SENTRY_OP]: 'ui.long_animation_frame',
52+
[SENTRY_OP]: UI_LONG_ANIMATION_FRAME,
5553
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
5654
},
5755
}),
@@ -79,33 +77,31 @@ sentryTest(
7977

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

82-
const promise = getFirstSentryEnvelopeRequest<Event>(page);
80+
const promise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
8381

8482
await page.goto(url);
8583

8684
// trigger long animation frame function
8785
await page.getByRole('button').click();
8886

89-
await new Promise(resolve => setTimeout(resolve, 1000));
90-
91-
const eventData = await promise;
87+
const eventData = envelopeRequestParser(await promise);
9288

93-
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith('ui.long_animation_frame')) || [];
89+
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith(UI_LONG_ANIMATION_FRAME)) || [];
9490

9591
expect(uiSpans.length).toBeGreaterThanOrEqual(2);
9692

9793
const eventListenerUISpan = uiSpans.find(span => span.data[BROWSER_SCRIPT_INVOKER] === 'BUTTON#clickme.onclick')!;
9894

9995
expect(eventListenerUISpan).toEqual(
10096
expect.objectContaining({
101-
op: 'ui.long_animation_frame',
97+
op: UI_LONG_ANIMATION_FRAME,
10298
description: 'Main UI thread blocked',
10399
parent_span_id: eventData.contexts?.trace?.span_id,
104100
data: {
105101
[BROWSER_SCRIPT_INVOKER]: 'BUTTON#clickme.onclick',
106102
[BROWSER_SCRIPT_INVOKER_TYPE]: 'event-listener',
107103
[CODE_FILE_PATH]: 'https://sentry-test-site.example/path/to/script.js',
108-
[SENTRY_OP]: 'ui.long_animation_frame',
104+
[SENTRY_OP]: UI_LONG_ANIMATION_FRAME,
109105
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
110106
},
111107
}),

‎dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/long-tasks-and-animation-frame-enabled-static/test.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PAGELOAD, UI_LONG_ANIMATION_FRAME } from '@sentry/conventions/op';
12
import {
23
BROWSER_SCRIPT_INVOKER,
34
CODE_FILE_PATH,
@@ -8,9 +9,8 @@ import {
89
} from '@sentry/conventions/attributes';
910
import type { Route } from '@playwright/test';
1011
import { expect } from '@playwright/test';
11-
import type { Event } from '@sentry/core';
1212
import { sentryTest } from '../../../../utils/fixtures';
13-
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';
13+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
1414

1515
sentryTest(
1616
'should capture long animation frame for top-level script.',
@@ -28,15 +28,13 @@ sentryTest(
2828

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

31-
const promise = getFirstSentryEnvelopeRequest<Event>(page);
31+
const promise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
3232

3333
await page.goto(url);
3434

35-
await new Promise(resolve => setTimeout(resolve, 1000));
35+
const eventData = envelopeRequestParser(await promise);
3636

37-
const eventData = await promise;
38-
39-
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith('ui.long_animation_frame'));
37+
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith(UI_LONG_ANIMATION_FRAME));
4038

4139
expect(uiSpans?.length).toBeGreaterThanOrEqual(1);
4240

@@ -45,15 +43,15 @@ sentryTest(
4543
)!;
4644
expect(topLevelUISpan).toEqual(
4745
expect.objectContaining({
48-
op: 'ui.long_animation_frame',
46+
op: UI_LONG_ANIMATION_FRAME,
4947
description: 'Main UI thread blocked',
5048
parent_span_id: eventData.contexts?.trace?.span_id,
5149
data: {
5250
[CODE_FILE_PATH]: 'https://sentry-test-site.example/path/to/script.js',
5351
[BROWSER_SCRIPT_SOURCE_CHAR_POSITION]: 0,
5452
[BROWSER_SCRIPT_INVOKER]: 'https://sentry-test-site.example/path/to/script.js',
5553
[BROWSER_SCRIPT_INVOKER_TYPE]: 'classic-script',
56-
[SENTRY_OP]: 'ui.long_animation_frame',
54+
[SENTRY_OP]: UI_LONG_ANIMATION_FRAME,
5755
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
5856
},
5957
}),
@@ -81,33 +79,31 @@ sentryTest(
8179

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

84-
const promise = getFirstSentryEnvelopeRequest<Event>(page);
82+
const promise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
8583

8684
await page.goto(url);
8785

8886
// trigger long animation frame function
8987
await page.getByRole('button').click();
9088

91-
await new Promise(resolve => setTimeout(resolve, 1000));
92-
93-
const eventData = await promise;
89+
const eventData = envelopeRequestParser(await promise);
9490

95-
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith('ui.long_animation_frame')) || [];
91+
const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith(UI_LONG_ANIMATION_FRAME)) || [];
9692

9793
expect(uiSpans.length).toBeGreaterThanOrEqual(2);
9894

9995
const eventListenerUISpan = uiSpans.find(span => span.data[BROWSER_SCRIPT_INVOKER] === 'BUTTON#clickme.onclick')!;
10096

10197
expect(eventListenerUISpan).toEqual(
10298
expect.objectContaining({
103-
op: 'ui.long_animation_frame',
99+
op: UI_LONG_ANIMATION_FRAME,
104100
description: 'Main UI thread blocked',
105101
parent_span_id: eventData.contexts?.trace?.span_id,
106102
data: {
107103
[BROWSER_SCRIPT_INVOKER]: 'BUTTON#clickme.onclick',
108104
[BROWSER_SCRIPT_INVOKER_TYPE]: 'event-listener',
109105
[CODE_FILE_PATH]: 'https://sentry-test-site.example/path/to/script.js',
110-
[SENTRY_OP]: 'ui.long_animation_frame',
106+
[SENTRY_OP]: UI_LONG_ANIMATION_FRAME,
111107
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
112108
},
113109
}),

0 commit comments

Comments
 (0)