|
| 1 | +import { afterAll, describe, expect } from 'vitest'; |
| 2 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
| 3 | + |
| 4 | +describe('hono tracing', () => { |
| 5 | + afterAll(() => { |
| 6 | + cleanupChildProcesses(); |
| 7 | + }); |
| 8 | + |
| 9 | + createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { |
| 10 | + describe.each(['/sync', '/async'] as const)('when using %s route', route => { |
| 11 | + describe.each(['get', 'post', 'put', 'delete', 'patch'] as const)('when using %s method', method => { |
| 12 | + describe.each(['/', '/all', '/on'])('when using %s path', path => { |
| 13 | + test('should handle transaction', async () => { |
| 14 | + const runner = createRunner() |
| 15 | + .expect({ |
| 16 | + transaction: { |
| 17 | + transaction: `${method.toUpperCase()} ${route}${path === '/' ? '' : path}`, |
| 18 | + spans: expect.arrayContaining([ |
| 19 | + expect.objectContaining({ |
| 20 | + data: expect.objectContaining({ |
| 21 | + 'hono.name': 'sentryRequestMiddleware', |
| 22 | + 'hono.type': 'middleware', |
| 23 | + }), |
| 24 | + description: 'sentryRequestMiddleware', |
| 25 | + op: 'middleware.hono', |
| 26 | + origin: 'auto.http.otel.hono', |
| 27 | + }), |
| 28 | + expect.objectContaining({ |
| 29 | + data: expect.objectContaining({ |
| 30 | + 'hono.name': 'sentryErrorMiddleware', |
| 31 | + 'hono.type': 'middleware', |
| 32 | + }), |
| 33 | + description: 'sentryErrorMiddleware', |
| 34 | + op: 'middleware.hono', |
| 35 | + origin: 'auto.http.otel.hono', |
| 36 | + }), |
| 37 | + expect.objectContaining({ |
| 38 | + data: expect.objectContaining({ |
| 39 | + 'hono.name': 'global', |
| 40 | + 'hono.type': 'middleware', |
| 41 | + }), |
| 42 | + description: 'global', |
| 43 | + op: 'middleware.hono', |
| 44 | + origin: 'auto.http.otel.hono', |
| 45 | + }), |
| 46 | + expect.objectContaining({ |
| 47 | + data: expect.objectContaining({ |
| 48 | + 'hono.name': 'base', |
| 49 | + 'hono.type': 'middleware', |
| 50 | + }), |
| 51 | + description: 'base', |
| 52 | + op: 'middleware.hono', |
| 53 | + origin: 'auto.http.otel.hono', |
| 54 | + }), |
| 55 | + expect.objectContaining({ |
| 56 | + data: expect.objectContaining({ |
| 57 | + 'hono.name': `${route}${path === '/' ? '' : path}`, |
| 58 | + 'hono.type': 'request_handler', |
| 59 | + }), |
| 60 | + description: `${route}${path === '/' ? '' : path}`, |
| 61 | + op: 'request_handler.hono', |
| 62 | + origin: 'auto.http.otel.hono', |
| 63 | + }), |
| 64 | + ]), |
| 65 | + }, |
| 66 | + }) |
| 67 | + .start(); |
| 68 | + runner.makeRequest(method, `${route}${path === '/' ? '' : path}`); |
| 69 | + await runner.completed(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should handle transaction with anonymous middleware', async () => { |
| 73 | + const runner = createRunner() |
| 74 | + .expect({ |
| 75 | + transaction: { |
| 76 | + transaction: `${method.toUpperCase()} ${route}${path === '/' ? '' : path}/middleware`, |
| 77 | + spans: expect.arrayContaining([ |
| 78 | + expect.objectContaining({ |
| 79 | + data: expect.objectContaining({ |
| 80 | + 'hono.name': 'sentryRequestMiddleware', |
| 81 | + 'hono.type': 'middleware', |
| 82 | + }), |
| 83 | + description: 'sentryRequestMiddleware', |
| 84 | + op: 'middleware.hono', |
| 85 | + origin: 'auto.http.otel.hono', |
| 86 | + }), |
| 87 | + expect.objectContaining({ |
| 88 | + data: expect.objectContaining({ |
| 89 | + 'hono.name': 'sentryErrorMiddleware', |
| 90 | + 'hono.type': 'middleware', |
| 91 | + }), |
| 92 | + description: 'sentryErrorMiddleware', |
| 93 | + op: 'middleware.hono', |
| 94 | + origin: 'auto.http.otel.hono', |
| 95 | + }), |
| 96 | + expect.objectContaining({ |
| 97 | + data: expect.objectContaining({ |
| 98 | + 'hono.name': 'global', |
| 99 | + 'hono.type': 'middleware', |
| 100 | + }), |
| 101 | + description: 'global', |
| 102 | + op: 'middleware.hono', |
| 103 | + origin: 'auto.http.otel.hono', |
| 104 | + }), |
| 105 | + expect.objectContaining({ |
| 106 | + data: expect.objectContaining({ |
| 107 | + 'hono.name': 'base', |
| 108 | + 'hono.type': 'middleware', |
| 109 | + }), |
| 110 | + description: 'base', |
| 111 | + op: 'middleware.hono', |
| 112 | + origin: 'auto.http.otel.hono', |
| 113 | + }), |
| 114 | + expect.objectContaining({ |
| 115 | + data: expect.objectContaining({ |
| 116 | + 'hono.name': 'anonymous', |
| 117 | + 'hono.type': 'middleware', |
| 118 | + }), |
| 119 | + description: 'anonymous', |
| 120 | + op: 'middleware.hono', |
| 121 | + origin: 'auto.http.otel.hono', |
| 122 | + }), |
| 123 | + expect.objectContaining({ |
| 124 | + data: expect.objectContaining({ |
| 125 | + 'hono.name': `${route}${path === '/' ? '' : path}/middleware`, |
| 126 | + 'hono.type': 'request_handler', |
| 127 | + }), |
| 128 | + description: `${route}${path === '/' ? '' : path}/middleware`, |
| 129 | + op: 'request_handler.hono', |
| 130 | + origin: 'auto.http.otel.hono', |
| 131 | + }), |
| 132 | + ]), |
| 133 | + }, |
| 134 | + }) |
| 135 | + .start(); |
| 136 | + runner.makeRequest(method, `${route}${path === '/' ? '' : path}/middleware`); |
| 137 | + await runner.completed(); |
| 138 | + }); |
| 139 | + |
| 140 | + test('should handle returned errors for %s path', async () => { |
| 141 | + const runner = createRunner() |
| 142 | + .ignore('transaction') |
| 143 | + .expect({ |
| 144 | + event: { |
| 145 | + exception: { |
| 146 | + values: [ |
| 147 | + { |
| 148 | + type: 'Error', |
| 149 | + value: 'response 500', |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + }, |
| 154 | + }) |
| 155 | + .start(); |
| 156 | + runner.makeRequest(method, `${route}${path === '/' ? '' : path}/500`, { expectError: true }); |
| 157 | + await runner.completed(); |
| 158 | + }); |
| 159 | + |
| 160 | + test.each(['/401', '/402', '/403', '/does-not-exist'])( |
| 161 | + 'should ignores error %s path by default', |
| 162 | + async (subPath: string) => { |
| 163 | + const runner = createRunner() |
| 164 | + .expect({ |
| 165 | + transaction: { |
| 166 | + transaction: `${method.toUpperCase()} ${route}`, |
| 167 | + }, |
| 168 | + }) |
| 169 | + .start(); |
| 170 | + runner.makeRequest(method, `${route}${path === '/' ? '' : path}${subPath}`, { expectError: true }); |
| 171 | + runner.makeRequest(method, route); |
| 172 | + await runner.completed(); |
| 173 | + }, |
| 174 | + ); |
| 175 | + }); |
| 176 | + }); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments