|
1 | 1 | import { expect, test } from '@playwright/test'; |
2 | | -import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 2 | +import { collectStreamedSpans, getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +const ORPC_SEGMENT_NAME = 'POST /rpc/[[...rest]]'; |
3 | 5 |
|
4 | 6 | test('should trace orpc server component', async ({ page }) => { |
5 | | - const pageloadPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
6 | | - return transactionEvent.transaction === '/'; |
7 | | - }); |
| 7 | + // The server component calls `planet.list` over HTTP while rendering, so the RPC span belongs to |
| 8 | + // the same trace as the pageload. Every span here is matched on that trace: rendering `/` is also |
| 9 | + // what the error spec does, and streamed spans are flushed in batches, so an identically shaped |
| 10 | + // RPC span from an earlier spec can still arrive inside this window. Waiting for the RPC span on |
| 11 | + // *this* pageload's trace is what asserts the SSR request propagated its trace to the RPC call. |
| 12 | + const spansPromise = collectStreamedSpans('nextjs-orpc', spans => { |
| 13 | + const pageload = spans.find(span => span.name === '/' && getSpanOp(span) === 'pageload' && span.is_segment); |
8 | 14 |
|
9 | | - const orpcTxPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
10 | | - return transactionEvent.transaction === 'POST /rpc/[[...rest]]'; |
| 15 | + return ( |
| 16 | + !!pageload && |
| 17 | + spans.some(span => span.name === ORPC_SEGMENT_NAME && span.is_segment && span.trace_id === pageload.trace_id) && |
| 18 | + spans.some(span => span.name === 'ORPC Middleware' && span.trace_id === pageload.trace_id) |
| 19 | + ); |
11 | 20 | }); |
12 | 21 |
|
13 | 22 | await page.goto('/'); |
14 | | - const pageloadTx = await pageloadPromise; |
15 | | - const orpcTx = await orpcTxPromise; |
| 23 | + const spans = await spansPromise; |
| 24 | + |
| 25 | + const pageloadSpan = spans.find(span => span.name === '/' && getSpanOp(span) === 'pageload' && span.is_segment)!; |
| 26 | + const orpcSpan = spans.find( |
| 27 | + span => span.name === ORPC_SEGMENT_NAME && span.is_segment && span.trace_id === pageloadSpan.trace_id, |
| 28 | + )!; |
| 29 | + const orpcSpans = spans.filter(span => span.trace_id === pageloadSpan.trace_id); |
16 | 30 |
|
17 | | - expect(pageloadTx.contexts?.trace).toMatchObject({ |
18 | | - parent_span_id: expect.any(String), |
19 | | - span_id: expect.any(String), |
20 | | - trace_id: expect.any(String), |
21 | | - data: { |
22 | | - 'sentry.origin': 'auto.pageload.nextjs.app_router_instrumentation', |
23 | | - 'sentry.op': 'pageload', |
24 | | - 'sentry.segment.name.source': 'route', |
25 | | - }, |
26 | | - op: 'pageload', |
27 | | - origin: 'auto.pageload.nextjs.app_router_instrumentation', |
| 31 | + expect(pageloadSpan.parent_span_id).toEqual(expect.any(String)); |
| 32 | + expect(pageloadSpan.span_id).toEqual(expect.any(String)); |
| 33 | + expect(pageloadSpan.trace_id).toEqual(expect.any(String)); |
| 34 | + expect(pageloadSpan.attributes).toMatchObject({ |
| 35 | + 'sentry.origin': { value: 'auto.pageload.nextjs.app_router_instrumentation', type: 'string' }, |
| 36 | + 'sentry.op': { value: 'pageload', type: 'string' }, |
| 37 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
28 | 38 | }); |
29 | 39 |
|
30 | | - expect(orpcTx.contexts?.trace).toMatchObject({ |
31 | | - parent_span_id: expect.any(String), |
32 | | - span_id: expect.any(String), |
33 | | - trace_id: pageloadTx.contexts?.trace?.trace_id, |
34 | | - data: { |
35 | | - 'sentry.op': 'http.server', |
36 | | - 'sentry.origin': 'auto', |
37 | | - 'sentry.segment.name.source': 'route', |
38 | | - 'sentry.kind': 'server', |
39 | | - 'http.response.status_code': 200, |
40 | | - 'next.span_name': 'POST /rpc/[[...rest]]/route', |
41 | | - 'next.span_type': 'BaseServer.handleRequest', |
42 | | - 'http.method': 'POST', |
43 | | - 'http.target': '/rpc/planet/list', |
44 | | - 'next.rsc': false, |
45 | | - 'http.route': '/rpc/[[...rest]]', |
46 | | - 'next.route': '/rpc/[[...rest]]', |
47 | | - 'http.status_code': 200, |
48 | | - }, |
49 | | - op: 'http.server', |
50 | | - origin: 'auto', |
| 40 | + // `orpcSpan` was matched on the pageload's trace above, so its presence is the trace assertion. |
| 41 | + expect(orpcSpan.parent_span_id).toEqual(expect.any(String)); |
| 42 | + expect(orpcSpan.span_id).toEqual(expect.any(String)); |
| 43 | + expect(orpcSpan.attributes).toMatchObject({ |
| 44 | + 'sentry.op': { value: 'http.server', type: 'string' }, |
| 45 | + 'sentry.origin': { value: 'auto', type: 'string' }, |
| 46 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 47 | + 'sentry.kind': { value: 'server', type: 'string' }, |
| 48 | + 'http.response.status_code': { value: 200, type: 'integer' }, |
| 49 | + 'next.span_name': { value: 'POST /rpc/[[...rest]]/route', type: 'string' }, |
| 50 | + 'next.span_type': { value: 'BaseServer.handleRequest', type: 'string' }, |
| 51 | + 'http.method': { value: 'POST', type: 'string' }, |
| 52 | + 'http.target': { value: '/rpc/planet/list', type: 'string' }, |
| 53 | + 'next.rsc': { value: false, type: 'boolean' }, |
| 54 | + 'http.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 55 | + 'next.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 56 | + 'http.status_code': { value: 200, type: 'integer' }, |
51 | 57 | }); |
52 | 58 |
|
53 | | - expect(orpcTx.spans?.map(span => span.description)).toContain('ORPC Middleware'); |
| 59 | + expect(orpcSpans.map(span => span.name)).toContain('ORPC Middleware'); |
54 | 60 | }); |
55 | 61 |
|
56 | 62 | test('should trace orpc client component', async ({ page }) => { |
57 | | - const navigationPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
58 | | - return transactionEvent.transaction === '/client'; |
| 63 | + const navigationSpanPromise = waitForStreamedSpan('nextjs-orpc', span => { |
| 64 | + return span.name === '/client' && getSpanOp(span) === 'navigation' && span.is_segment; |
59 | 65 | }); |
60 | 66 |
|
61 | | - const orpcTxPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
| 67 | + const orpcSpansPromise = collectStreamedSpans('nextjs-orpc', spans => { |
62 | 68 | return ( |
63 | | - transactionEvent.transaction === 'POST /rpc/[[...rest]]' && |
64 | | - transactionEvent.contexts?.trace?.data?.['http.target'] === '/rpc/planet/find' |
| 69 | + spans.some( |
| 70 | + span => |
| 71 | + span.name === ORPC_SEGMENT_NAME && |
| 72 | + span.is_segment && |
| 73 | + span.attributes['http.target']?.value === '/rpc/planet/find', |
| 74 | + ) && spans.some(span => span.name === 'ORPC Middleware') |
65 | 75 | ); |
66 | 76 | }); |
67 | 77 |
|
68 | 78 | await page.goto('/'); |
69 | 79 | await page.waitForTimeout(500); |
70 | 80 | await page.getByRole('link', { name: 'Client' }).click(); |
71 | | - const navigationTx = await navigationPromise; |
72 | | - const orpcTx = await orpcTxPromise; |
| 81 | + const navigationSpan = await navigationSpanPromise; |
| 82 | + const orpcSpans = await orpcSpansPromise; |
| 83 | + const orpcSpan = orpcSpans.find( |
| 84 | + span => |
| 85 | + span.name === ORPC_SEGMENT_NAME && |
| 86 | + span.is_segment && |
| 87 | + span.attributes['http.target']?.value === '/rpc/planet/find', |
| 88 | + )!; |
73 | 89 |
|
74 | | - expect(navigationTx.contexts?.trace).toMatchObject({ |
75 | | - span_id: expect.any(String), |
76 | | - trace_id: expect.any(String), |
77 | | - data: { |
78 | | - 'sentry.op': 'navigation', |
79 | | - 'sentry.origin': 'auto.navigation.nextjs.app_router_instrumentation', |
80 | | - 'sentry.segment.name.source': 'route', |
81 | | - 'sentry.previous_trace': expect.any(String), |
82 | | - }, |
83 | | - op: 'navigation', |
84 | | - origin: 'auto.navigation.nextjs.app_router_instrumentation', |
| 90 | + expect(navigationSpan.span_id).toEqual(expect.any(String)); |
| 91 | + expect(navigationSpan.trace_id).toEqual(expect.any(String)); |
| 92 | + expect(navigationSpan.attributes).toMatchObject({ |
| 93 | + 'sentry.op': { value: 'navigation', type: 'string' }, |
| 94 | + 'sentry.origin': { value: 'auto.navigation.nextjs.app_router_instrumentation', type: 'string' }, |
| 95 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 96 | + 'sentry.previous_trace': { value: expect.any(String), type: 'string' }, |
85 | 97 | }); |
86 | 98 |
|
87 | | - expect(orpcTx?.contexts?.trace).toMatchObject({ |
88 | | - parent_span_id: expect.any(String), |
89 | | - span_id: expect.any(String), |
90 | | - trace_id: navigationTx?.contexts?.trace?.trace_id, |
91 | | - data: { |
92 | | - 'sentry.op': 'http.server', |
93 | | - 'sentry.origin': 'auto', |
94 | | - 'sentry.segment.name.source': 'route', |
95 | | - 'sentry.kind': 'server', |
96 | | - 'http.response.status_code': 200, |
97 | | - 'next.span_name': 'POST /rpc/[[...rest]]/route', |
98 | | - 'next.span_type': 'BaseServer.handleRequest', |
99 | | - 'http.method': 'POST', |
100 | | - 'http.target': '/rpc/planet/find', |
101 | | - 'next.rsc': false, |
102 | | - 'http.route': '/rpc/[[...rest]]', |
103 | | - 'next.route': '/rpc/[[...rest]]', |
104 | | - 'http.status_code': 200, |
105 | | - }, |
106 | | - op: 'http.server', |
107 | | - origin: 'auto', |
| 99 | + expect(orpcSpan.parent_span_id).toEqual(expect.any(String)); |
| 100 | + expect(orpcSpan.span_id).toEqual(expect.any(String)); |
| 101 | + expect(orpcSpan.trace_id).toBe(navigationSpan.trace_id); |
| 102 | + expect(orpcSpan.attributes).toMatchObject({ |
| 103 | + 'sentry.op': { value: 'http.server', type: 'string' }, |
| 104 | + 'sentry.origin': { value: 'auto', type: 'string' }, |
| 105 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 106 | + 'sentry.kind': { value: 'server', type: 'string' }, |
| 107 | + 'http.response.status_code': { value: 200, type: 'integer' }, |
| 108 | + 'next.span_name': { value: 'POST /rpc/[[...rest]]/route', type: 'string' }, |
| 109 | + 'next.span_type': { value: 'BaseServer.handleRequest', type: 'string' }, |
| 110 | + 'http.method': { value: 'POST', type: 'string' }, |
| 111 | + 'http.target': { value: '/rpc/planet/find', type: 'string' }, |
| 112 | + 'next.rsc': { value: false, type: 'boolean' }, |
| 113 | + 'http.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 114 | + 'next.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 115 | + 'http.status_code': { value: 200, type: 'integer' }, |
108 | 116 | }); |
109 | 117 |
|
110 | | - expect(orpcTx.spans?.map(span => span.description)).toContain('ORPC Middleware'); |
| 118 | + expect(orpcSpans.map(span => span.name)).toContain('ORPC Middleware'); |
111 | 119 | }); |
0 commit comments