|
| 1 | +import { afterAll, describe, expect } from 'vitest'; |
| 2 | +import { isOrchestrionEnabled } from '../../../utils'; |
| 3 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
| 4 | + |
| 5 | +describe('koa auto-instrumentation', () => { |
| 6 | + afterAll(async () => { |
| 7 | + cleanupChildProcesses(); |
| 8 | + }); |
| 9 | + |
| 10 | + // `createEsmAndCjsTests` auto-runs this suite with orchestrion on CI. The |
| 11 | + // orchestrion path keeps span ops/attributes identical to the OTel path; only |
| 12 | + // the origin differs to signal the injection mechanism, so we branch on |
| 13 | + // `isOrchestrionEnabled()`. |
| 14 | + const origin = isOrchestrionEnabled() ? 'auto.http.orchestrion.koa' : 'auto.http.otel.koa'; |
| 15 | + |
| 16 | + const EXPECTED_ERROR_EVENT = { |
| 17 | + exception: { |
| 18 | + values: [ |
| 19 | + { |
| 20 | + type: 'Error', |
| 21 | + value: 'Sentry Test Error', |
| 22 | + }, |
| 23 | + ], |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { |
| 28 | + test('should auto-instrument `koa` router and middleware layers.', async () => { |
| 29 | + const runner = createRunner() |
| 30 | + .expect({ |
| 31 | + transaction: { |
| 32 | + transaction: 'GET /', |
| 33 | + spans: expect.arrayContaining([ |
| 34 | + // Router layer span (from `@koa/router`), carrying the matched route. |
| 35 | + expect.objectContaining({ |
| 36 | + description: '/', |
| 37 | + op: 'router.koa', |
| 38 | + origin, |
| 39 | + data: expect.objectContaining({ |
| 40 | + 'http.route': '/', |
| 41 | + 'koa.type': 'router', |
| 42 | + 'koa.name': '/', |
| 43 | + 'sentry.op': 'router.koa', |
| 44 | + 'sentry.origin': origin, |
| 45 | + }), |
| 46 | + }), |
| 47 | + // Plain middleware span. |
| 48 | + expect.objectContaining({ |
| 49 | + description: 'simpleMiddleware', |
| 50 | + op: 'middleware.koa', |
| 51 | + origin, |
| 52 | + data: expect.objectContaining({ |
| 53 | + 'koa.type': 'middleware', |
| 54 | + 'koa.name': 'simpleMiddleware', |
| 55 | + 'code.function.name': 'simpleMiddleware', |
| 56 | + 'sentry.op': 'middleware.koa', |
| 57 | + 'sentry.origin': origin, |
| 58 | + }), |
| 59 | + }), |
| 60 | + ]), |
| 61 | + }, |
| 62 | + }) |
| 63 | + .start(); |
| 64 | + runner.makeRequest('get', '/'); |
| 65 | + await runner.completed(); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should assign a parameterized transaction name.', async () => { |
| 69 | + const runner = createRunner() |
| 70 | + .expect({ |
| 71 | + transaction: { |
| 72 | + transaction: 'GET /test-param/:id', |
| 73 | + spans: expect.arrayContaining([ |
| 74 | + expect.objectContaining({ |
| 75 | + description: '/test-param/:id', |
| 76 | + op: 'router.koa', |
| 77 | + origin, |
| 78 | + data: expect.objectContaining({ |
| 79 | + 'http.route': '/test-param/:id', |
| 80 | + 'koa.type': 'router', |
| 81 | + 'koa.name': '/test-param/:id', |
| 82 | + 'sentry.op': 'router.koa', |
| 83 | + 'sentry.origin': origin, |
| 84 | + }), |
| 85 | + }), |
| 86 | + ]), |
| 87 | + }, |
| 88 | + }) |
| 89 | + .start(); |
| 90 | + runner.makeRequest('get', '/test-param/123'); |
| 91 | + await runner.completed(); |
| 92 | + }); |
| 93 | + |
| 94 | + test('should capture errors thrown in routes via the koa error handler.', async () => { |
| 95 | + const runner = createRunner() |
| 96 | + .unordered() |
| 97 | + .expect({ transaction: { transaction: 'GET /error' } }) |
| 98 | + .expect({ event: EXPECTED_ERROR_EVENT }) |
| 99 | + .start(); |
| 100 | + runner.makeRequest('get', '/error', { expectError: true }); |
| 101 | + await runner.completed(); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments