|
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 | + const pageloadSpanPromise = waitForStreamedSpan('nextjs-orpc', span => { |
| 8 | + return span.name === '/' && getSpanOp(span) === 'pageload' && span.is_segment; |
7 | 9 | }); |
8 | 10 |
|
9 | | - const orpcTxPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
10 | | - return transactionEvent.transaction === 'POST /rpc/[[...rest]]'; |
| 11 | + // The ORPC middleware span is a child of the segment span, so it arrives in a later envelope. |
| 12 | + const orpcSpansPromise = collectStreamedSpans('nextjs-orpc', spans => { |
| 13 | + return ( |
| 14 | + spans.some(span => span.name === ORPC_SEGMENT_NAME && span.is_segment) && |
| 15 | + spans.some(span => span.name === 'ORPC Middleware') |
| 16 | + ); |
11 | 17 | }); |
12 | 18 |
|
13 | 19 | await page.goto('/'); |
14 | | - const pageloadTx = await pageloadPromise; |
15 | | - const orpcTx = await orpcTxPromise; |
| 20 | + const pageloadSpan = await pageloadSpanPromise; |
| 21 | + const orpcSpans = await orpcSpansPromise; |
| 22 | + const orpcSpan = orpcSpans.find(span => span.name === ORPC_SEGMENT_NAME && span.is_segment)!; |
16 | 23 |
|
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', |
| 24 | + expect(pageloadSpan.parent_span_id).toEqual(expect.any(String)); |
| 25 | + expect(pageloadSpan.span_id).toEqual(expect.any(String)); |
| 26 | + expect(pageloadSpan.trace_id).toEqual(expect.any(String)); |
| 27 | + expect(pageloadSpan.attributes).toMatchObject({ |
| 28 | + 'sentry.origin': { value: 'auto.pageload.nextjs.app_router_instrumentation', type: 'string' }, |
| 29 | + 'sentry.op': { value: 'pageload', type: 'string' }, |
| 30 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
28 | 31 | }); |
29 | 32 |
|
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', |
| 33 | + expect(orpcSpan.parent_span_id).toEqual(expect.any(String)); |
| 34 | + expect(orpcSpan.span_id).toEqual(expect.any(String)); |
| 35 | + expect(orpcSpan.trace_id).toBe(pageloadSpan.trace_id); |
| 36 | + expect(orpcSpan.attributes).toMatchObject({ |
| 37 | + 'sentry.op': { value: 'http.server', type: 'string' }, |
| 38 | + 'sentry.origin': { value: 'auto', type: 'string' }, |
| 39 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 40 | + 'sentry.kind': { value: 'server', type: 'string' }, |
| 41 | + 'http.response.status_code': { value: 200, type: 'integer' }, |
| 42 | + 'next.span_name': { value: 'POST /rpc/[[...rest]]/route', type: 'string' }, |
| 43 | + 'next.span_type': { value: 'BaseServer.handleRequest', type: 'string' }, |
| 44 | + 'http.method': { value: 'POST', type: 'string' }, |
| 45 | + 'http.target': { value: '/rpc/planet/list', type: 'string' }, |
| 46 | + 'next.rsc': { value: false, type: 'boolean' }, |
| 47 | + 'http.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 48 | + 'next.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 49 | + 'http.status_code': { value: 200, type: 'integer' }, |
51 | 50 | }); |
52 | 51 |
|
53 | | - expect(orpcTx.spans?.map(span => span.description)).toContain('ORPC Middleware'); |
| 52 | + expect(orpcSpans.map(span => span.name)).toContain('ORPC Middleware'); |
54 | 53 | }); |
55 | 54 |
|
56 | 55 | test('should trace orpc client component', async ({ page }) => { |
57 | | - const navigationPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
58 | | - return transactionEvent.transaction === '/client'; |
| 56 | + const navigationSpanPromise = waitForStreamedSpan('nextjs-orpc', span => { |
| 57 | + return span.name === '/client' && getSpanOp(span) === 'navigation' && span.is_segment; |
59 | 58 | }); |
60 | 59 |
|
61 | | - const orpcTxPromise = waitForTransaction('nextjs-orpc', transactionEvent => { |
| 60 | + const orpcSpansPromise = collectStreamedSpans('nextjs-orpc', spans => { |
62 | 61 | return ( |
63 | | - transactionEvent.transaction === 'POST /rpc/[[...rest]]' && |
64 | | - transactionEvent.contexts?.trace?.data?.['http.target'] === '/rpc/planet/find' |
| 62 | + spans.some( |
| 63 | + span => |
| 64 | + span.name === ORPC_SEGMENT_NAME && |
| 65 | + span.is_segment && |
| 66 | + span.attributes['http.target']?.value === '/rpc/planet/find', |
| 67 | + ) && spans.some(span => span.name === 'ORPC Middleware') |
65 | 68 | ); |
66 | 69 | }); |
67 | 70 |
|
68 | 71 | await page.goto('/'); |
69 | 72 | await page.waitForTimeout(500); |
70 | 73 | await page.getByRole('link', { name: 'Client' }).click(); |
71 | | - const navigationTx = await navigationPromise; |
72 | | - const orpcTx = await orpcTxPromise; |
| 74 | + const navigationSpan = await navigationSpanPromise; |
| 75 | + const orpcSpans = await orpcSpansPromise; |
| 76 | + const orpcSpan = orpcSpans.find( |
| 77 | + span => |
| 78 | + span.name === ORPC_SEGMENT_NAME && |
| 79 | + span.is_segment && |
| 80 | + span.attributes['http.target']?.value === '/rpc/planet/find', |
| 81 | + )!; |
73 | 82 |
|
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', |
| 83 | + expect(navigationSpan.span_id).toEqual(expect.any(String)); |
| 84 | + expect(navigationSpan.trace_id).toEqual(expect.any(String)); |
| 85 | + expect(navigationSpan.attributes).toMatchObject({ |
| 86 | + 'sentry.op': { value: 'navigation', type: 'string' }, |
| 87 | + 'sentry.origin': { value: 'auto.navigation.nextjs.app_router_instrumentation', type: 'string' }, |
| 88 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 89 | + 'sentry.previous_trace': { value: expect.any(String), type: 'string' }, |
85 | 90 | }); |
86 | 91 |
|
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', |
| 92 | + expect(orpcSpan.parent_span_id).toEqual(expect.any(String)); |
| 93 | + expect(orpcSpan.span_id).toEqual(expect.any(String)); |
| 94 | + expect(orpcSpan.trace_id).toBe(navigationSpan.trace_id); |
| 95 | + expect(orpcSpan.attributes).toMatchObject({ |
| 96 | + 'sentry.op': { value: 'http.server', type: 'string' }, |
| 97 | + 'sentry.origin': { value: 'auto', type: 'string' }, |
| 98 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 99 | + 'sentry.kind': { value: 'server', type: 'string' }, |
| 100 | + 'http.response.status_code': { value: 200, type: 'integer' }, |
| 101 | + 'next.span_name': { value: 'POST /rpc/[[...rest]]/route', type: 'string' }, |
| 102 | + 'next.span_type': { value: 'BaseServer.handleRequest', type: 'string' }, |
| 103 | + 'http.method': { value: 'POST', type: 'string' }, |
| 104 | + 'http.target': { value: '/rpc/planet/find', type: 'string' }, |
| 105 | + 'next.rsc': { value: false, type: 'boolean' }, |
| 106 | + 'http.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 107 | + 'next.route': { value: '/rpc/[[...rest]]', type: 'string' }, |
| 108 | + 'http.status_code': { value: 200, type: 'integer' }, |
108 | 109 | }); |
109 | 110 |
|
110 | | - expect(orpcTx.spans?.map(span => span.description)).toContain('ORPC Middleware'); |
| 111 | + expect(orpcSpans.map(span => span.name)).toContain('ORPC Middleware'); |
111 | 112 | }); |
0 commit comments