|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/event-proxy-server'; |
| 3 | + |
| 4 | +test('Propagates trace for outgoing http requests', async ({ baseURL, request }) => { |
| 5 | + const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 6 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-http/check'; |
| 7 | + }); |
| 8 | + |
| 9 | + const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 10 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-http'; |
| 11 | + }); |
| 12 | + |
| 13 | + const { headers } = await (await request.get(`${baseURL}/propagation/test-outgoing-http`)).json(); |
| 14 | + |
| 15 | + const inboundTransaction = await inboundTransactionPromise; |
| 16 | + const outboundTransaction = await outboundTransactionPromise; |
| 17 | + |
| 18 | + expect(inboundTransaction.contexts?.trace?.trace_id).toStrictEqual(expect.any(String)); |
| 19 | + expect(inboundTransaction.contexts?.trace?.trace_id).toBe(outboundTransaction.contexts?.trace?.trace_id); |
| 20 | + |
| 21 | + const httpClientSpan = outboundTransaction.spans?.find(span => span.op === 'http.client'); |
| 22 | + |
| 23 | + expect(httpClientSpan).toBeDefined(); |
| 24 | + expect(httpClientSpan?.span_id).toStrictEqual(expect.any(String)); |
| 25 | + expect(inboundTransaction.contexts?.trace?.parent_span_id).toBe(httpClientSpan?.span_id); |
| 26 | + |
| 27 | + expect(headers).toMatchObject({ |
| 28 | + baggage: expect.any(String), |
| 29 | + 'sentry-trace': `${outboundTransaction.contexts?.trace?.trace_id}-${httpClientSpan?.span_id}-1`, |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +test('Propagates trace for outgoing fetch requests', async ({ baseURL, request }) => { |
| 34 | + const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 35 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch/check'; |
| 36 | + }); |
| 37 | + |
| 38 | + const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 39 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch'; |
| 40 | + }); |
| 41 | + |
| 42 | + const { headers } = await (await request.get(`${baseURL}/propagation/test-outgoing-fetch`)).json(); |
| 43 | + |
| 44 | + const inboundTransaction = await inboundTransactionPromise; |
| 45 | + const outboundTransaction = await outboundTransactionPromise; |
| 46 | + |
| 47 | + expect(inboundTransaction.contexts?.trace?.trace_id).toStrictEqual(expect.any(String)); |
| 48 | + expect(inboundTransaction.contexts?.trace?.trace_id).toBe(outboundTransaction.contexts?.trace?.trace_id); |
| 49 | + |
| 50 | + const httpClientSpan = outboundTransaction.spans?.find( |
| 51 | + span => span.op === 'http.client' && span.data?.['sentry.origin'] === 'auto.http.otel.node_fetch', |
| 52 | + ); |
| 53 | + |
| 54 | + // Right now we assert that the OTEL span is the last span before propagating |
| 55 | + expect(httpClientSpan).toBeDefined(); |
| 56 | + expect(httpClientSpan?.span_id).toStrictEqual(expect.any(String)); |
| 57 | + expect(inboundTransaction.contexts?.trace?.parent_span_id).toBe(httpClientSpan?.span_id); |
| 58 | + |
| 59 | + expect(headers).toMatchObject({ |
| 60 | + baggage: expect.any(String), |
| 61 | + 'sentry-trace': `${outboundTransaction.contexts?.trace?.trace_id}-${httpClientSpan?.span_id}-1`, |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +test('Does not propagate outgoing http requests not covered by tracePropagationTargets', async ({ |
| 66 | + baseURL, |
| 67 | + request, |
| 68 | +}) => { |
| 69 | + const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 70 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-http-external-disallowed/check'; |
| 71 | + }); |
| 72 | + |
| 73 | + const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 74 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-http-external-disallowed'; |
| 75 | + }); |
| 76 | + |
| 77 | + const { headers } = await (await request.get(`${baseURL}/propagation/test-outgoing-http-external-disallowed`)).json(); |
| 78 | + |
| 79 | + expect(headers.baggage).toBeUndefined(); |
| 80 | + expect(headers['sentry-trace']).toBeUndefined(); |
| 81 | + |
| 82 | + const inboundTransaction = await inboundTransactionPromise; |
| 83 | + const outboundTransaction = await outboundTransactionPromise; |
| 84 | + |
| 85 | + expect(typeof outboundTransaction.contexts?.trace?.trace_id).toBe('string'); |
| 86 | + expect(inboundTransaction.contexts?.trace?.trace_id).not.toBe(outboundTransaction.contexts?.trace?.trace_id); |
| 87 | +}); |
| 88 | + |
| 89 | +test('Does not propagate outgoing fetch requests not covered by tracePropagationTargets', async ({ |
| 90 | + baseURL, |
| 91 | + request, |
| 92 | +}) => { |
| 93 | + const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 94 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch-external-disallowed/check'; |
| 95 | + }); |
| 96 | + |
| 97 | + const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { |
| 98 | + return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch-external-disallowed'; |
| 99 | + }); |
| 100 | + |
| 101 | + const { headers } = await ( |
| 102 | + await request.get(`${baseURL}/propagation/test-outgoing-fetch-external-disallowed`) |
| 103 | + ).json(); |
| 104 | + |
| 105 | + expect(headers.baggage).toBeUndefined(); |
| 106 | + expect(headers['sentry-trace']).toBeUndefined(); |
| 107 | + |
| 108 | + const inboundTransaction = await inboundTransactionPromise; |
| 109 | + const outboundTransaction = await outboundTransactionPromise; |
| 110 | + |
| 111 | + expect(typeof outboundTransaction.contexts?.trace?.trace_id).toBe('string'); |
| 112 | + expect(inboundTransaction.contexts?.trace?.trace_id).not.toBe(outboundTransaction.contexts?.trace?.trace_id); |
| 113 | +}); |
0 commit comments