|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('Span nesting: all spans share the same trace_id', async ({ request }) => { |
| 5 | + const transactionEventPromise = waitForTransaction('nitro-3', event => { |
| 6 | + return event?.transaction === 'GET /api/test-nesting'; |
| 7 | + }); |
| 8 | + |
| 9 | + await request.get('/api/test-nesting'); |
| 10 | + |
| 11 | + const event = await transactionEventPromise; |
| 12 | + const traceId = event.contexts?.trace?.trace_id; |
| 13 | + |
| 14 | + expect(traceId).toMatch(/[a-f0-9]{32}/); |
| 15 | + |
| 16 | + // Every child span must belong to the same trace |
| 17 | + for (const span of event.spans ?? []) { |
| 18 | + expect(span.trace_id).toBe(traceId); |
| 19 | + } |
| 20 | +}); |
| 21 | + |
| 22 | +test('Span nesting: h3 middleware spans are children of the srvx request span', async ({ request }) => { |
| 23 | + const transactionEventPromise = waitForTransaction('nitro-3', event => { |
| 24 | + return event?.transaction === 'GET /api/test-nesting'; |
| 25 | + }); |
| 26 | + |
| 27 | + await request.get('/api/test-nesting'); |
| 28 | + |
| 29 | + const event = await transactionEventPromise; |
| 30 | + |
| 31 | + // Find the srvx request span |
| 32 | + const srvxSpan = event.spans?.find(span => span.origin === 'auto.http.nitro.srvx' && span.op === 'http.server'); |
| 33 | + expect(srvxSpan).toBeDefined(); |
| 34 | + |
| 35 | + // All h3 middleware spans should be children of the srvx span |
| 36 | + const h3Spans = event.spans?.filter(span => span.origin === 'auto.http.nitro.h3'); |
| 37 | + expect(h3Spans?.length).toBeGreaterThanOrEqual(1); |
| 38 | + |
| 39 | + for (const span of h3Spans ?? []) { |
| 40 | + expect(span.parent_span_id).toBe(srvxSpan!.span_id); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +test('Span nesting: manual startSpan calls inside route handler are children of the srvx request span', async ({ |
| 45 | + request, |
| 46 | +}) => { |
| 47 | + const transactionEventPromise = waitForTransaction('nitro-3', event => { |
| 48 | + return event?.transaction === 'GET /api/test-nesting'; |
| 49 | + }); |
| 50 | + |
| 51 | + await request.get('/api/test-nesting'); |
| 52 | + |
| 53 | + const event = await transactionEventPromise; |
| 54 | + |
| 55 | + // Find the srvx request span — this is the parent of all h3 and manual spans |
| 56 | + const srvxSpan = event.spans?.find(span => span.origin === 'auto.http.nitro.srvx' && span.op === 'http.server'); |
| 57 | + expect(srvxSpan).toBeDefined(); |
| 58 | + const srvxSpanId = srvxSpan!.span_id; |
| 59 | + |
| 60 | + // Find the manually created db spans |
| 61 | + const dbSelectSpan = event.spans?.find(span => span.op === 'db' && span.description === 'db.select'); |
| 62 | + const dbInsertSpan = event.spans?.find(span => span.op === 'db' && span.description === 'db.insert'); |
| 63 | + expect(dbSelectSpan).toBeDefined(); |
| 64 | + expect(dbInsertSpan).toBeDefined(); |
| 65 | + |
| 66 | + // FIXME: Once nitro's h3 tracing plugin emits a separate span for route handlers (type: "route"), |
| 67 | + // the db spans should be children of the h3 route handler span, not the srvx span directly. |
| 68 | + // Currently nitro bypasses h3's ~routes for file-based routing, so h3 only emits middleware spans. |
| 69 | + // Both db spans should be children of the srvx request span |
| 70 | + expect(dbSelectSpan!.parent_span_id).toBe(srvxSpanId); |
| 71 | + expect(dbInsertSpan!.parent_span_id).toBe(srvxSpanId); |
| 72 | + |
| 73 | + // Both db spans should be siblings (same parent) |
| 74 | + expect(dbSelectSpan!.parent_span_id).toBe(dbInsertSpan!.parent_span_id); |
| 75 | + |
| 76 | + // The serialize span should be nested inside the db.insert span |
| 77 | + const serializeSpan = event.spans?.find(span => span.op === 'serialize' && span.description === 'db.serialize'); |
| 78 | + expect(serializeSpan).toBeDefined(); |
| 79 | + expect(serializeSpan!.parent_span_id).toBe(dbInsertSpan!.span_id); |
| 80 | +}); |
| 81 | + |
| 82 | +// FIXME: Nitro's file-based routing bypasses h3's ~routes, so h3's tracing plugin never wraps |
| 83 | +// route handlers with type: "route". Once this is fixed upstream or we add our own wrapping, |
| 84 | +// uncomment these tests to verify the h3 route handler span exists and is the parent of manual spans. |
| 85 | +// |
| 86 | +// test('Span nesting: h3 route handler span is a child of the srvx request span', async ({ request }) => { |
| 87 | +// const transactionEventPromise = waitForTransaction('nitro-3', event => { |
| 88 | +// return event?.transaction === 'GET /api/test-nesting'; |
| 89 | +// }); |
| 90 | +// |
| 91 | +// await request.get('/api/test-nesting'); |
| 92 | +// |
| 93 | +// const event = await transactionEventPromise; |
| 94 | +// |
| 95 | +// const srvxSpan = event.spans?.find(span => span.origin === 'auto.http.nitro.srvx' && span.op === 'http.server'); |
| 96 | +// expect(srvxSpan).toBeDefined(); |
| 97 | +// |
| 98 | +// const h3HandlerSpan = event.spans?.find( |
| 99 | +// span => span.origin === 'auto.http.nitro.h3' && span.op === 'http.server', |
| 100 | +// ); |
| 101 | +// expect(h3HandlerSpan).toBeDefined(); |
| 102 | +// expect(h3HandlerSpan!.parent_span_id).toBe(srvxSpan!.span_id); |
| 103 | +// }); |
| 104 | +// |
| 105 | +// test('Span nesting: manual startSpan calls are children of the h3 route handler span', async ({ request }) => { |
| 106 | +// const transactionEventPromise = waitForTransaction('nitro-3', event => { |
| 107 | +// return event?.transaction === 'GET /api/test-nesting'; |
| 108 | +// }); |
| 109 | +// |
| 110 | +// await request.get('/api/test-nesting'); |
| 111 | +// |
| 112 | +// const event = await transactionEventPromise; |
| 113 | +// |
| 114 | +// const h3HandlerSpan = event.spans?.find( |
| 115 | +// span => span.origin === 'auto.http.nitro.h3' && span.op === 'http.server', |
| 116 | +// ); |
| 117 | +// expect(h3HandlerSpan).toBeDefined(); |
| 118 | +// |
| 119 | +// const dbSelectSpan = event.spans?.find(span => span.op === 'db' && span.description === 'db.select'); |
| 120 | +// const dbInsertSpan = event.spans?.find(span => span.op === 'db' && span.description === 'db.insert'); |
| 121 | +// expect(dbSelectSpan!.parent_span_id).toBe(h3HandlerSpan!.span_id); |
| 122 | +// expect(dbInsertSpan!.parent_span_id).toBe(h3HandlerSpan!.span_id); |
| 123 | +// }); |
| 124 | + |
| 125 | +test('Span nesting: middleware spans start before manual spans in the span tree', async ({ request }) => { |
| 126 | + const transactionEventPromise = waitForTransaction('nitro-3', event => { |
| 127 | + return event?.transaction === 'GET /api/test-nesting'; |
| 128 | + }); |
| 129 | + |
| 130 | + await request.get('/api/test-nesting'); |
| 131 | + |
| 132 | + const event = await transactionEventPromise; |
| 133 | + |
| 134 | + // Middleware spans should start before the manual db spans |
| 135 | + const middlewareSpans = event.spans?.filter(span => span.op === 'middleware.nitro') ?? []; |
| 136 | + const dbSpans = event.spans?.filter(span => span.op === 'db') ?? []; |
| 137 | + |
| 138 | + expect(middlewareSpans.length).toBeGreaterThanOrEqual(1); |
| 139 | + expect(dbSpans.length).toBeGreaterThanOrEqual(1); |
| 140 | + |
| 141 | + const earliestMiddlewareStart = Math.min(...middlewareSpans.map(s => s.start_timestamp)); |
| 142 | + const earliestDbStart = Math.min(...dbSpans.map(s => s.start_timestamp)); |
| 143 | + |
| 144 | + // Middleware should start before the db spans |
| 145 | + expect(earliestMiddlewareStart).toBeLessThanOrEqual(earliestDbStart); |
| 146 | +}); |
0 commit comments