|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 3 | +import type { EventAndTraceHeader } from '../../../../utils/helpers'; |
| 4 | +import { |
| 5 | + eventAndTraceHeaderRequestParser, |
| 6 | + getFirstSentryEnvelopeRequest, |
| 7 | + shouldSkipTracingTest, |
| 8 | + waitForTransactionRequest, |
| 9 | +} from '../../../../utils/helpers'; |
| 10 | + |
| 11 | +sentryTest( |
| 12 | + 'new trace started with `startNewTrace` is sampled according to the `tracesSampler`', |
| 13 | + async ({ getLocalTestUrl, page }) => { |
| 14 | + if (shouldSkipTracingTest()) { |
| 15 | + sentryTest.skip(); |
| 16 | + } |
| 17 | + |
| 18 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 19 | + |
| 20 | + await page.route('http://sentry-test-site.example/**', route => { |
| 21 | + return route.fulfill({ |
| 22 | + status: 200, |
| 23 | + contentType: 'application/json', |
| 24 | + body: JSON.stringify({}), |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + const [pageloadEvent, pageloadTraceHeaders] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>( |
| 29 | + page, |
| 30 | + url, |
| 31 | + eventAndTraceHeaderRequestParser, |
| 32 | + ); |
| 33 | + |
| 34 | + const pageloadTraceContext = pageloadEvent.contexts?.trace; |
| 35 | + |
| 36 | + expect(pageloadEvent.type).toEqual('transaction'); |
| 37 | + |
| 38 | + expect(pageloadTraceContext).toMatchObject({ |
| 39 | + op: 'pageload', |
| 40 | + trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 41 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 42 | + data: { |
| 43 | + 'sentry.sample_rate': 0.5, |
| 44 | + }, |
| 45 | + }); |
| 46 | + expect(pageloadTraceContext).not.toHaveProperty('parent_span_id'); |
| 47 | + |
| 48 | + expect(pageloadTraceHeaders).toEqual({ |
| 49 | + environment: 'production', |
| 50 | + public_key: 'public', |
| 51 | + sample_rate: '0.5', |
| 52 | + sampled: 'true', |
| 53 | + trace_id: pageloadTraceContext?.trace_id, |
| 54 | + sample_rand: '0.45', |
| 55 | + }); |
| 56 | + |
| 57 | + const transactionPromise = waitForTransactionRequest(page, event => { |
| 58 | + return event.transaction === 'new-trace'; |
| 59 | + }); |
| 60 | + |
| 61 | + await page.locator('#newTrace').click(); |
| 62 | + |
| 63 | + const [newTraceTransactionEvent, newTraceTransactionTraceHeaders] = eventAndTraceHeaderRequestParser( |
| 64 | + await transactionPromise, |
| 65 | + ); |
| 66 | + |
| 67 | + const newTraceTransactionTraceContext = newTraceTransactionEvent.contexts?.trace; |
| 68 | + expect(newTraceTransactionTraceContext).toMatchObject({ |
| 69 | + op: 'ui.interaction.click', |
| 70 | + trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 71 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 72 | + data: { |
| 73 | + 'sentry.sample_rate': 0.9, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(newTraceTransactionTraceHeaders).toEqual({ |
| 78 | + environment: 'production', |
| 79 | + public_key: 'public', |
| 80 | + sample_rate: '0.9', |
| 81 | + sampled: 'true', |
| 82 | + trace_id: newTraceTransactionTraceContext?.trace_id, |
| 83 | + transaction: 'new-trace', |
| 84 | + sample_rand: '0.85', |
| 85 | + }); |
| 86 | + |
| 87 | + expect(newTraceTransactionTraceContext?.trace_id).not.toEqual(pageloadTraceContext?.trace_id); |
| 88 | + }, |
| 89 | +); |
0 commit comments