|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Event } from '@sentry/types'; |
| 3 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 4 | +import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; |
| 5 | + |
| 6 | +const META_TAG_TRACE_ID = '12345678901234567890123456789012'; |
| 7 | +const META_TAG_PARENT_SPAN_ID = '1234567890123456'; |
| 8 | +const META_TAG_BAGGAGE = |
| 9 | + 'sentry-trace_id=12345678901234567890123456789012,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod'; |
| 10 | + |
| 11 | +sentryTest('error has new traceId after navigation', async ({ getLocalTestPath, page }) => { |
| 12 | + if (shouldSkipTracingTest()) { |
| 13 | + sentryTest.skip(); |
| 14 | + } |
| 15 | + |
| 16 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 17 | + await page.goto(url); |
| 18 | + |
| 19 | + const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page); |
| 20 | + await page.locator('#errorBtn').click(); |
| 21 | + const errorEvent = await errorEventPromise; |
| 22 | + |
| 23 | + expect(errorEvent.contexts?.trace).toEqual({ |
| 24 | + trace_id: META_TAG_TRACE_ID, |
| 25 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 26 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 27 | + }); |
| 28 | + |
| 29 | + const errorEventPromise2 = getFirstSentryEnvelopeRequest<Event>(page, `${url}#navigation`); |
| 30 | + await page.locator('#errorBtn').click(); |
| 31 | + const errorEvent2 = await errorEventPromise2; |
| 32 | + |
| 33 | + expect(errorEvent2.contexts?.trace).toEqual({ |
| 34 | + trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 35 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 36 | + }); |
| 37 | + expect(errorEvent2.contexts?.trace?.trace_id).not.toBe(META_TAG_TRACE_ID); |
| 38 | +}); |
| 39 | + |
| 40 | +sentryTest('outgoing fetch requests have new traceId after navigation', async ({ getLocalTestPath, page }) => { |
| 41 | + if (shouldSkipTracingTest()) { |
| 42 | + sentryTest.skip(); |
| 43 | + } |
| 44 | + |
| 45 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 46 | + await page.goto(url); |
| 47 | + |
| 48 | + const requestPromise = page.waitForRequest('http://example.com/*'); |
| 49 | + await page.locator('#fetchBtn').click(); |
| 50 | + const request = await requestPromise; |
| 51 | + const headers = request.headers(); |
| 52 | + |
| 53 | + // sampling decision is deferred because TwP means we didn't sample any span |
| 54 | + expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}$`)); |
| 55 | + expect(headers['baggage']).toBe(META_TAG_BAGGAGE); |
| 56 | + |
| 57 | + await page.goto(`${url}#navigation`); |
| 58 | + |
| 59 | + const requestPromise2 = page.waitForRequest('http://example.com/*'); |
| 60 | + await page.locator('#fetchBtn').click(); |
| 61 | + const request2 = await requestPromise2; |
| 62 | + const headers2 = request2.headers(); |
| 63 | + |
| 64 | + // sampling decision is deferred because TwP means we didn't sample any span |
| 65 | + expect(headers2['sentry-trace']).toMatch(/^[0-9a-f]{32}-[0-9a-f]{16}$/); |
| 66 | + expect(headers2['baggage']).not.toBe(`${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}`); |
| 67 | + expect(headers2['baggage']).toMatch( |
| 68 | + /sentry-environment=production,sentry-public_key=public,sentry-trace_id=[0-9a-f]{32}/, |
| 69 | + ); |
| 70 | + expect(headers2['baggage']).not.toContain(`sentry-trace_id=${META_TAG_TRACE_ID}`); |
| 71 | +}); |
| 72 | + |
| 73 | +sentryTest('outgoing XHR requests have new traceId after navigation', async ({ getLocalTestPath, page }) => { |
| 74 | + if (shouldSkipTracingTest()) { |
| 75 | + sentryTest.skip(); |
| 76 | + } |
| 77 | + |
| 78 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 79 | + await page.goto(url); |
| 80 | + |
| 81 | + const requestPromise = page.waitForRequest('http://example.com/*'); |
| 82 | + await page.locator('#xhrBtn').click(); |
| 83 | + const request = await requestPromise; |
| 84 | + const headers = request.headers(); |
| 85 | + |
| 86 | + // sampling decision is deferred because TwP means we didn't sample any span |
| 87 | + expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}$`)); |
| 88 | + expect(headers['baggage']).toBe(META_TAG_BAGGAGE); |
| 89 | + |
| 90 | + await page.goto(`${url}#navigation`); |
| 91 | + |
| 92 | + const requestPromise2 = page.waitForRequest('http://example.com/*'); |
| 93 | + await page.locator('#xhrBtn').click(); |
| 94 | + const request2 = await requestPromise2; |
| 95 | + const headers2 = request2.headers(); |
| 96 | + |
| 97 | + // sampling decision is deferred because TwP means we didn't sample any span |
| 98 | + expect(headers2['sentry-trace']).toMatch(/^[0-9a-f]{32}-[0-9a-f]{16}$/); |
| 99 | + expect(headers2['baggage']).not.toBe(`${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}`); |
| 100 | + expect(headers2['baggage']).toMatch( |
| 101 | + /sentry-environment=production,sentry-public_key=public,sentry-trace_id=[0-9a-f]{32}/, |
| 102 | + ); |
| 103 | + expect(headers2['baggage']).not.toContain(`sentry-trace_id=${META_TAG_TRACE_ID}`); |
| 104 | +}); |
0 commit comments