|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Event } from '@sentry/types'; |
| 3 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 4 | +import { |
| 5 | + getFirstSentryEnvelopeRequest, |
| 6 | + getMultipleSentryEnvelopeRequests, |
| 7 | + shouldSkipTracingTest, |
| 8 | +} from '../../../../utils/helpers'; |
| 9 | + |
| 10 | +const META_TAG_TRACE_ID = '12345678901234567890123456789012'; |
| 11 | +const META_TAG_PARENT_SPAN_ID = '1234567890123456'; |
| 12 | +const META_TAG_BAGGAGE = |
| 13 | + 'sentry-trace_id=12345678901234567890123456789012,sentry-sample_rate=0.2,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod'; |
| 14 | + |
| 15 | +sentryTest( |
| 16 | + 'create a new trace for a navigation after the <meta> tag pageload trace', |
| 17 | + async ({ getLocalTestPath, page }) => { |
| 18 | + if (shouldSkipTracingTest()) { |
| 19 | + sentryTest.skip(); |
| 20 | + } |
| 21 | + |
| 22 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 23 | + |
| 24 | + const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url); |
| 25 | + const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`); |
| 26 | + |
| 27 | + const pageloadTraceContext = pageloadEvent.contexts?.trace; |
| 28 | + const navigationTraceContext = navigationEvent.contexts?.trace; |
| 29 | + |
| 30 | + expect(pageloadTraceContext).toMatchObject({ |
| 31 | + op: 'pageload', |
| 32 | + trace_id: META_TAG_TRACE_ID, |
| 33 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 34 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 35 | + }); |
| 36 | + expect(navigationTraceContext).toMatchObject({ |
| 37 | + op: 'navigation', |
| 38 | + trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 39 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 40 | + }); |
| 41 | + // navigation span is head of trace, so there's no parent span: |
| 42 | + expect(navigationTraceContext?.trace_id).not.toHaveProperty('parent_span_id'); |
| 43 | + |
| 44 | + expect(pageloadTraceContext?.trace_id).not.toEqual(navigationTraceContext?.trace_id); |
| 45 | + }, |
| 46 | +); |
| 47 | + |
| 48 | +sentryTest('error after <meta> tag pageload has pageload traceId', async ({ getLocalTestPath, page }) => { |
| 49 | + if (shouldSkipTracingTest()) { |
| 50 | + sentryTest.skip(); |
| 51 | + } |
| 52 | + |
| 53 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 54 | + |
| 55 | + const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url); |
| 56 | + expect(pageloadEvent.contexts?.trace).toMatchObject({ |
| 57 | + op: 'pageload', |
| 58 | + trace_id: META_TAG_TRACE_ID, |
| 59 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 60 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 61 | + }); |
| 62 | + |
| 63 | + const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page); |
| 64 | + await page.locator('#errorBtn').click(); |
| 65 | + const errorEvent = await errorEventPromise; |
| 66 | + |
| 67 | + expect(errorEvent.contexts?.trace).toMatchObject({ |
| 68 | + trace_id: META_TAG_TRACE_ID, |
| 69 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 70 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +sentryTest('error during <meta> tag pageload has pageload traceId', async ({ getLocalTestPath, page }) => { |
| 75 | + if (shouldSkipTracingTest()) { |
| 76 | + sentryTest.skip(); |
| 77 | + } |
| 78 | + |
| 79 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 80 | + |
| 81 | + const envelopeRequestsPromise = getMultipleSentryEnvelopeRequests<Event>(page, 2); |
| 82 | + await page.goto(url); |
| 83 | + await page.locator('#errorBtn').click(); |
| 84 | + const events = await envelopeRequestsPromise; |
| 85 | + |
| 86 | + const pageloadEvent = events.find(event => event.type === 'transaction'); |
| 87 | + const errorEvent = events.find(event => !event.type); |
| 88 | + |
| 89 | + expect(pageloadEvent?.contexts?.trace).toMatchObject({ |
| 90 | + op: 'pageload', |
| 91 | + trace_id: META_TAG_TRACE_ID, |
| 92 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 93 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 94 | + }); |
| 95 | + |
| 96 | + expect(errorEvent?.contexts?.trace).toMatchObject({ |
| 97 | + trace_id: META_TAG_TRACE_ID, |
| 98 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 99 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +sentryTest( |
| 104 | + 'outgoing fetch request after <meta> tag pageload has pageload traceId in headers', |
| 105 | + async ({ getLocalTestPath, page }) => { |
| 106 | + if (shouldSkipTracingTest()) { |
| 107 | + sentryTest.skip(); |
| 108 | + } |
| 109 | + |
| 110 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 111 | + |
| 112 | + const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url); |
| 113 | + expect(pageloadEvent?.contexts?.trace).toMatchObject({ |
| 114 | + op: 'pageload', |
| 115 | + trace_id: META_TAG_TRACE_ID, |
| 116 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 117 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 118 | + }); |
| 119 | + |
| 120 | + const requestPromise = page.waitForRequest('http://example.com/*'); |
| 121 | + await page.locator('#fetchBtn').click(); |
| 122 | + const request = await requestPromise; |
| 123 | + const headers = request.headers(); |
| 124 | + |
| 125 | + // sampling decision is propagated from meta tag's sentry-trace sampled flag |
| 126 | + expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}-1$`)); |
| 127 | + expect(headers['baggage']).toBe(META_TAG_BAGGAGE); |
| 128 | + }, |
| 129 | +); |
| 130 | + |
| 131 | +sentryTest( |
| 132 | + 'outgoing fetch request during <meta> tag pageload has pageload traceId in headers', |
| 133 | + async ({ getLocalTestPath, page }) => { |
| 134 | + if (shouldSkipTracingTest()) { |
| 135 | + sentryTest.skip(); |
| 136 | + } |
| 137 | + |
| 138 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 139 | + |
| 140 | + const pageloadEventPromise = getFirstSentryEnvelopeRequest<Event>(page); |
| 141 | + const requestPromise = page.waitForRequest('http://example.com/*'); |
| 142 | + await page.goto(url); |
| 143 | + await page.locator('#fetchBtn').click(); |
| 144 | + const [pageloadEvent, request] = await Promise.all([pageloadEventPromise, requestPromise]); |
| 145 | + |
| 146 | + expect(pageloadEvent?.contexts?.trace).toMatchObject({ |
| 147 | + op: 'pageload', |
| 148 | + trace_id: META_TAG_TRACE_ID, |
| 149 | + parent_span_id: META_TAG_PARENT_SPAN_ID, |
| 150 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 151 | + }); |
| 152 | + |
| 153 | + const headers = request.headers(); |
| 154 | + |
| 155 | + // sampling decision is propagated from meta tag's sentry-trace sampled flag |
| 156 | + expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}-1$`)); |
| 157 | + expect(headers['baggage']).toBe(META_TAG_BAGGAGE); |
| 158 | + }, |
| 159 | +); |
0 commit comments