|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForStreamedSpan, getSpanOp, collectStreamedSpansUntilSegment } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('Sends successful span', async ({ baseURL }) => { |
| 5 | + const pageloadSegmentEventPromise = collectStreamedSpansUntilSegment( |
| 6 | + 'node-hapi', |
| 7 | + segment => getSpanOp(segment) === 'http.server' && segment.name === 'GET /test-success', |
| 8 | + ); |
| 9 | + |
| 10 | + await fetch(`${baseURL}/test-success`); |
| 11 | + |
| 12 | + const segmentEventSpans = await pageloadSegmentEventPromise; |
| 13 | + const segmentEvent = segmentEventSpans.find( |
| 14 | + segment => segment.is_segment && getSpanOp(segment) === 'http.server' && segment.name === 'GET /test-success', |
| 15 | + )!; |
| 16 | + |
| 17 | + expect(segmentEvent).toEqual( |
| 18 | + expect.objectContaining({ |
| 19 | + span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 20 | + status: 'ok', |
| 21 | + trace_id: expect.stringMatching(/[a-f0-9]{32}/), |
| 22 | + attributes: expect.objectContaining({ |
| 23 | + 'sentry.segment.name.source': { value: 'route', type: 'string' }, |
| 24 | + 'sentry.origin': { value: 'auto.http.http_server', type: 'string' }, |
| 25 | + 'sentry.op': { value: 'http.server', type: 'string' }, |
| 26 | + 'sentry.sample_rate': { value: 1, type: 'integer' }, |
| 27 | + 'sentry.kind': { value: 'server', type: 'string' }, |
| 28 | + 'http.response.status_code': { value: 200, type: 'integer' }, |
| 29 | + 'url.full': { value: 'http://localhost:3030/test-success', type: 'string' }, |
| 30 | + 'url.path': { value: '/test-success', type: 'string' }, |
| 31 | + 'server.address': { value: 'localhost', type: 'string' }, |
| 32 | + 'http.request.method': { value: 'GET', type: 'string' }, |
| 33 | + 'url.scheme': { value: 'http', type: 'string' }, |
| 34 | + 'user_agent.original': { value: 'node', type: 'string' }, |
| 35 | + 'client.address': { value: '::1', type: 'string' }, |
| 36 | + 'client.port': { value: expect.any(Number), type: 'integer' }, |
| 37 | + 'network.transport': { value: 'tcp', type: 'string' }, |
| 38 | + 'network.local.address': { value: expect.any(String), type: 'string' }, |
| 39 | + 'network.local.port': { value: expect.any(Number), type: 'integer' }, |
| 40 | + 'network.peer.address': { value: expect.any(String), type: 'string' }, |
| 41 | + 'network.peer.port': { value: expect.any(Number), type: 'integer' }, |
| 42 | + 'network.protocol.name': { value: 'http', type: 'string' }, |
| 43 | + 'network.protocol.version': { value: '1.1', type: 'string' }, |
| 44 | + 'server.port': { value: 3030, type: 'integer' }, |
| 45 | + 'http.response.status_text': { value: 'OK', type: 'string' }, |
| 46 | + 'http.route': { value: '/test-success', type: 'string' }, |
| 47 | + 'http.request.header.accept': { value: '*/*', type: 'string' }, |
| 48 | + 'http.request.header.accept_encoding': { value: 'gzip, deflate', type: 'string' }, |
| 49 | + 'http.request.header.accept_language': { value: '*', type: 'string' }, |
| 50 | + 'http.request.header.connection': { value: 'keep-alive', type: 'string' }, |
| 51 | + 'http.request.header.host': { value: expect.any(String), type: 'string' }, |
| 52 | + 'http.request.header.sec_fetch_mode': { value: 'cors', type: 'string' }, |
| 53 | + 'http.request.header.user_agent': { value: 'node', type: 'string' }, |
| 54 | + }), |
| 55 | + }), |
| 56 | + ); |
| 57 | + |
| 58 | + expect(segmentEvent).toEqual( |
| 59 | + expect.objectContaining({ |
| 60 | + name: 'GET /test-success', |
| 61 | + is_segment: true, |
| 62 | + attributes: expect.objectContaining({ 'sentry.segment.name.source': { value: 'route', type: 'string' } }), |
| 63 | + }), |
| 64 | + ); |
| 65 | + |
| 66 | + const spans = segmentEventSpans.filter( |
| 67 | + span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segmentEvent.span_id, |
| 68 | + ); |
| 69 | + |
| 70 | + spans.forEach(span => { |
| 71 | + expect(Object.keys(span.attributes).some(key => key.startsWith('http.request.header.'))).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + expect(spans).toEqual([ |
| 75 | + expect.objectContaining({ |
| 76 | + name: '/test-success', |
| 77 | + parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 78 | + span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 79 | + start_timestamp: expect.any(Number), |
| 80 | + status: 'ok', |
| 81 | + end_timestamp: expect.any(Number), |
| 82 | + trace_id: expect.stringMatching(/[a-f0-9]{32}/), |
| 83 | + attributes: expect.objectContaining({ |
| 84 | + 'hapi.type': { value: 'router', type: 'string' }, |
| 85 | + 'http.request.method': { value: 'GET', type: 'string' }, |
| 86 | + 'http.route': { value: '/test-success', type: 'string' }, |
| 87 | + 'sentry.op': { value: 'router', type: 'string' }, |
| 88 | + 'sentry.origin': { value: 'auto.http.hapi', type: 'string' }, |
| 89 | + }), |
| 90 | + }), |
| 91 | + expect.objectContaining({ |
| 92 | + name: 'ext - onPreResponse', |
| 93 | + parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 94 | + span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 95 | + start_timestamp: expect.any(Number), |
| 96 | + status: 'ok', |
| 97 | + end_timestamp: expect.any(Number), |
| 98 | + trace_id: expect.stringMatching(/[a-f0-9]{32}/), |
| 99 | + attributes: expect.objectContaining({ |
| 100 | + 'hapi.type': { value: 'server.ext', type: 'string' }, |
| 101 | + 'sentry.op': { value: 'middleware', type: 'string' }, |
| 102 | + 'sentry.origin': { value: 'auto.http.hapi', type: 'string' }, |
| 103 | + 'server.ext.type': { value: 'onPreResponse', type: 'string' }, |
| 104 | + }), |
| 105 | + }), |
| 106 | + ]); |
| 107 | +}); |
| 108 | + |
| 109 | +test('Sends parameterized spans to Sentry', async ({ baseURL }) => { |
| 110 | + const pageloadSegmentEventPromise = waitForStreamedSpan( |
| 111 | + 'node-hapi', |
| 112 | + segment => segment.is_segment && getSpanOp(segment) === 'http.server' && segment.name === 'GET /test-param/{param}', |
| 113 | + ); |
| 114 | + |
| 115 | + await fetch(`${baseURL}/test-param/123`); |
| 116 | + |
| 117 | + const segmentEvent = await pageloadSegmentEventPromise; |
| 118 | + |
| 119 | + expect(getSpanOp(segmentEvent)).toBe('http.server'); |
| 120 | + expect(segmentEvent.attributes?.['http.route']?.value).toBe('/test-param/{param}'); |
| 121 | + expect(segmentEvent.name).toBe('GET /test-param/{param}'); |
| 122 | +}); |
| 123 | + |
| 124 | +test('Isolates requests', async ({ baseURL }) => { |
| 125 | + const segment1Promise = waitForStreamedSpan( |
| 126 | + 'node-hapi', |
| 127 | + segment => |
| 128 | + segment.is_segment && |
| 129 | + getSpanOp(segment) === 'http.server' && |
| 130 | + segment.attributes?.['url.path']?.value === '/test-param/888', |
| 131 | + ); |
| 132 | + const segment2Promise = waitForStreamedSpan( |
| 133 | + 'node-hapi', |
| 134 | + segment => |
| 135 | + segment.is_segment && |
| 136 | + getSpanOp(segment) === 'http.server' && |
| 137 | + segment.attributes?.['url.path']?.value === '/test-param/999', |
| 138 | + ); |
| 139 | + |
| 140 | + await Promise.all([fetch(`${baseURL}/test-param/888`), fetch(`${baseURL}/test-param/999`)]); |
| 141 | + |
| 142 | + const segment1 = await segment1Promise; |
| 143 | + const segment2 = await segment2Promise; |
| 144 | + |
| 145 | + expect(segment1.attributes['param-888']?.value).toBe('yes'); |
| 146 | + expect(segment1.attributes['param-999']).toBeUndefined(); |
| 147 | + expect(segment2.attributes['param-999']?.value).toBe('yes'); |
| 148 | + expect(segment2.attributes['param-888']).toBeUndefined(); |
| 149 | +}); |
0 commit comments