|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import { SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE } from '@sentry/core'; |
| 3 | + |
| 4 | +import { sentryTest } from '../../../../../utils/fixtures'; |
| 5 | +import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../../utils/helpers'; |
| 6 | + |
| 7 | +/* |
| 8 | + This is quite peculiar behavior but it's a result of the route-based trace lifetime. |
| 9 | + Once we shortened trace lifetime, this whole scenario will change as the interaction |
| 10 | + spans will be their own trace. So most likely, we can replace this test with a new one |
| 11 | + that covers the new default behavior. |
| 12 | +*/ |
| 13 | +sentryTest( |
| 14 | + 'only the first root spans in the trace link back to the previous trace', |
| 15 | + async ({ getLocalTestUrl, page }) => { |
| 16 | + if (shouldSkipTracingTest()) { |
| 17 | + sentryTest.skip(); |
| 18 | + } |
| 19 | + |
| 20 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 21 | + |
| 22 | + const pageloadTraceContext = await sentryTest.step('Initial pageload', async () => { |
| 23 | + const pageloadRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'pageload'); |
| 24 | + await page.goto(url); |
| 25 | + |
| 26 | + const pageloadEvent = envelopeRequestParser(await pageloadRequestPromise); |
| 27 | + const traceContext = pageloadEvent.contexts?.trace; |
| 28 | + |
| 29 | + expect(traceContext).toBeDefined(); |
| 30 | + expect(traceContext?.links).toBeUndefined(); |
| 31 | + |
| 32 | + return traceContext; |
| 33 | + }); |
| 34 | + |
| 35 | + await sentryTest.step('Click Before navigation', async () => { |
| 36 | + const interactionRequestPromise = waitForTransactionRequest(page, evt => { |
| 37 | + return evt.contexts?.trace?.op === 'ui.action.click'; |
| 38 | + }); |
| 39 | + await page.click('#btn'); |
| 40 | + |
| 41 | + const interactionEvent = envelopeRequestParser(await interactionRequestPromise); |
| 42 | + const interactionTraceContext = interactionEvent.contexts?.trace; |
| 43 | + |
| 44 | + // sanity check: route-based trace lifetime means the trace_id should be the same |
| 45 | + expect(interactionTraceContext?.trace_id).toBe(pageloadTraceContext?.trace_id); |
| 46 | + |
| 47 | + // no links yet as previous root span belonged to same trace |
| 48 | + expect(interactionTraceContext?.links).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + const navigationTraceContext = await sentryTest.step('Navigation', async () => { |
| 52 | + const navigationRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'navigation'); |
| 53 | + await page.goto(`${url}#foo`); |
| 54 | + const navigationEvent = envelopeRequestParser(await navigationRequestPromise); |
| 55 | + |
| 56 | + const traceContext = navigationEvent.contexts?.trace; |
| 57 | + |
| 58 | + expect(traceContext?.op).toBe('navigation'); |
| 59 | + expect(traceContext?.links).toEqual([ |
| 60 | + { |
| 61 | + trace_id: pageloadTraceContext?.trace_id, |
| 62 | + span_id: pageloadTraceContext?.span_id, |
| 63 | + sampled: true, |
| 64 | + attributes: { |
| 65 | + [SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE]: 'previous_trace', |
| 66 | + }, |
| 67 | + }, |
| 68 | + ]); |
| 69 | + |
| 70 | + expect(traceContext?.trace_id).not.toEqual(traceContext?.links![0].trace_id); |
| 71 | + return traceContext; |
| 72 | + }); |
| 73 | + |
| 74 | + await sentryTest.step('Click After navigation', async () => { |
| 75 | + const interactionRequestPromise = waitForTransactionRequest(page, evt => { |
| 76 | + return evt.contexts?.trace?.op === 'ui.action.click'; |
| 77 | + }); |
| 78 | + await page.click('#btn'); |
| 79 | + const interactionEvent = envelopeRequestParser(await interactionRequestPromise); |
| 80 | + |
| 81 | + const interactionTraceContext = interactionEvent.contexts?.trace; |
| 82 | + |
| 83 | + // sanity check: route-based trace lifetime means the trace_id should be the same |
| 84 | + expect(interactionTraceContext?.trace_id).toBe(navigationTraceContext?.trace_id); |
| 85 | + |
| 86 | + // since this is the second root span in the trace, it doesn't link back to the previous trace |
| 87 | + expect(interactionTraceContext?.links).toBeUndefined(); |
| 88 | + }); |
| 89 | + }, |
| 90 | +); |
0 commit comments