|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { SpanJSON, TransactionEvent } from '@sentry/core'; |
| 3 | +import { sentryTest } from '../../../utils/fixtures'; |
| 4 | +import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../utils/helpers'; |
| 5 | + |
| 6 | +sentryTest('should link spans with addLinks() in trace context', async ({ getLocalTestUrl, page }) => { |
| 7 | + if (shouldSkipTracingTest()) { |
| 8 | + sentryTest.skip(); |
| 9 | + } |
| 10 | + |
| 11 | + const rootSpan1Promise = waitForTransactionRequest(page, event => event.transaction === 'rootSpan1'); |
| 12 | + const rootSpan2Promise = waitForTransactionRequest(page, event => event.transaction === 'rootSpan2'); |
| 13 | + const rootSpan3Promise = waitForTransactionRequest(page, event => event.transaction === 'rootSpan3'); |
| 14 | + |
| 15 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 16 | + await page.goto(url); |
| 17 | + |
| 18 | + const rootSpan1 = envelopeRequestParser<TransactionEvent>(await rootSpan1Promise); |
| 19 | + const rootSpan2 = envelopeRequestParser<TransactionEvent>(await rootSpan2Promise); |
| 20 | + const rootSpan3 = envelopeRequestParser<TransactionEvent>(await rootSpan3Promise); |
| 21 | + |
| 22 | + const rootSpan1_traceId = rootSpan1.contexts?.trace?.trace_id as string; |
| 23 | + const rootSpan1_spanId = rootSpan1.contexts?.trace?.span_id as string; |
| 24 | + |
| 25 | + expect(rootSpan1.transaction).toBe('rootSpan1'); |
| 26 | + expect(rootSpan1.spans).toEqual([]); |
| 27 | + |
| 28 | + const rootSpan2_traceId = rootSpan2.contexts?.trace?.trace_id as string; |
| 29 | + const rootSpan2_spanId = rootSpan2.contexts?.trace?.span_id as string; |
| 30 | + |
| 31 | + expect(rootSpan2.transaction).toBe('rootSpan2'); |
| 32 | + expect(rootSpan2.spans).toEqual([]); |
| 33 | + |
| 34 | + expect(rootSpan3.transaction).toBe('rootSpan3'); |
| 35 | + expect(rootSpan3.spans).toEqual([]); |
| 36 | + expect(rootSpan3.contexts?.trace?.links?.length).toBe(2); |
| 37 | + expect(rootSpan3.contexts?.trace?.links).toEqual([ |
| 38 | + { |
| 39 | + sampled: true, |
| 40 | + span_id: rootSpan1_spanId, |
| 41 | + trace_id: rootSpan1_traceId, |
| 42 | + }, |
| 43 | + { |
| 44 | + attributes: { 'sentry.link.type': 'previous_trace' }, |
| 45 | + sampled: true, |
| 46 | + span_id: rootSpan2_spanId, |
| 47 | + trace_id: rootSpan2_traceId, |
| 48 | + }, |
| 49 | + ]); |
| 50 | +}); |
| 51 | + |
| 52 | +sentryTest('should link spans with addLinks() in nested startSpan() calls', async ({ getLocalTestUrl, page }) => { |
| 53 | + if (shouldSkipTracingTest()) { |
| 54 | + sentryTest.skip(); |
| 55 | + } |
| 56 | + |
| 57 | + const rootSpan2Promise = waitForTransactionRequest(page, event => event.transaction === 'rootSpan2'); |
| 58 | + const rootSpan4Promise = waitForTransactionRequest(page, event => event.transaction === 'rootSpan4'); |
| 59 | + |
| 60 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 61 | + await page.goto(url); |
| 62 | + |
| 63 | + const rootSpan2 = envelopeRequestParser<TransactionEvent>(await rootSpan2Promise); |
| 64 | + const rootSpan4 = envelopeRequestParser<TransactionEvent>(await rootSpan4Promise); |
| 65 | + |
| 66 | + const rootSpan2_traceId = rootSpan2.contexts?.trace?.trace_id as string; |
| 67 | + const rootSpan2_spanId = rootSpan2.contexts?.trace?.span_id as string; |
| 68 | + |
| 69 | + const [childSpan_4_1, childSpan_4_2] = rootSpan4.spans as [SpanJSON, SpanJSON]; |
| 70 | + const rootSpan4_traceId = rootSpan4.contexts?.trace?.trace_id as string; |
| 71 | + const rootSpan4_spanId = rootSpan4.contexts?.trace?.span_id as string; |
| 72 | + |
| 73 | + expect(rootSpan4.transaction).toBe('rootSpan4'); |
| 74 | + |
| 75 | + expect(childSpan_4_1.description).toBe('childSpan4.1'); |
| 76 | + expect(childSpan_4_1.links).toBe(undefined); |
| 77 | + |
| 78 | + expect(childSpan_4_2.description).toBe('childSpan4.2'); |
| 79 | + expect(childSpan_4_2.links?.length).toBe(2); |
| 80 | + expect(childSpan_4_2.links).toEqual([ |
| 81 | + { |
| 82 | + sampled: true, |
| 83 | + span_id: rootSpan4_spanId, |
| 84 | + trace_id: rootSpan4_traceId, |
| 85 | + }, |
| 86 | + { |
| 87 | + attributes: { 'sentry.link.type': 'previous_trace' }, |
| 88 | + sampled: true, |
| 89 | + span_id: rootSpan2_spanId, |
| 90 | + trace_id: rootSpan2_traceId, |
| 91 | + }, |
| 92 | + ]); |
| 93 | +}); |
0 commit comments