|
| 1 | +import * as Sentry from '@sentry/elysia'; |
| 2 | +import { Elysia } from 'elysia'; |
| 3 | + |
| 4 | +Sentry.init({ |
| 5 | + environment: 'qa', // dynamic sampling bias to keep transactions |
| 6 | + dsn: process.env.E2E_TEST_DSN, |
| 7 | + tunnel: `http://localhost:3031/`, // proxy server |
| 8 | + tracesSampleRate: 1, |
| 9 | + tracePropagationTargets: ['http://localhost:3030', '/external-allowed'], |
| 10 | +}); |
| 11 | + |
| 12 | +const app = Sentry.withElysia(new Elysia()); |
| 13 | + |
| 14 | +// Simple success route |
| 15 | +app.get('/test-success', () => ({ version: 'v1' })); |
| 16 | + |
| 17 | +// Parameterized route |
| 18 | +app.get('/test-param/:param', ({ params }) => ({ paramWas: params.param })); |
| 19 | + |
| 20 | +// Multiple params |
| 21 | +app.get('/test-multi-param/:param1/:param2', ({ params }) => ({ |
| 22 | + param1: params.param1, |
| 23 | + param2: params.param2, |
| 24 | +})); |
| 25 | + |
| 26 | +// Route that throws an error (will be caught by onError) |
| 27 | +app.get('/test-exception/:id', ({ params }) => { |
| 28 | + throw new Error(`This is an exception with id ${params.id}`); |
| 29 | +}); |
| 30 | + |
| 31 | +// Route with a custom span |
| 32 | +app.get('/test-transaction', () => { |
| 33 | + Sentry.startSpan({ name: 'test-span' }, () => { |
| 34 | + Sentry.startSpan({ name: 'child-span' }, () => {}); |
| 35 | + }); |
| 36 | + return { status: 'ok' }; |
| 37 | +}); |
| 38 | + |
| 39 | +// Route with specific middleware via .guard or .use |
| 40 | +app.group('/with-middleware', app => |
| 41 | + app |
| 42 | + .onBeforeHandle(() => { |
| 43 | + // This is a route-specific middleware |
| 44 | + }) |
| 45 | + .get('/test', () => ({ middleware: true })), |
| 46 | +); |
| 47 | + |
| 48 | +// Error with specific status code |
| 49 | +app.post('/test-post-error', () => { |
| 50 | + throw new Error('Post error'); |
| 51 | +}); |
| 52 | + |
| 53 | +// Route that returns a non-500 error |
| 54 | +app.get('/test-4xx', ({ set }) => { |
| 55 | + set.status = 400; |
| 56 | + return { error: 'Bad Request' }; |
| 57 | +}); |
| 58 | + |
| 59 | +// Error that reaches the error handler with status still set to 200 (unusual, should still be captured) |
| 60 | +app.get('/test-error-with-200-status', ({ set }) => { |
| 61 | + set.status = 200; |
| 62 | + throw new Error('Error with 200 status'); |
| 63 | +}); |
| 64 | + |
| 65 | +// POST route that echoes body |
| 66 | +app.post('/test-post', ({ body }) => ({ status: 'ok', body })); |
| 67 | + |
| 68 | +// Route that returns inbound headers (for propagation tests) |
| 69 | +app.get('/test-inbound-headers/:id', ({ params, request }) => { |
| 70 | + const headers = Object.fromEntries(request.headers.entries()); |
| 71 | + return { headers, id: params.id }; |
| 72 | +}); |
| 73 | + |
| 74 | +// Outgoing fetch propagation |
| 75 | +app.get('/test-outgoing-fetch/:id', async ({ params }) => { |
| 76 | + const id = params.id; |
| 77 | + const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`); |
| 78 | + const data = await response.json(); |
| 79 | + return data; |
| 80 | +}); |
| 81 | + |
| 82 | +// Outgoing fetch to external (allowed by tracePropagationTargets) |
| 83 | +app.get('/test-outgoing-fetch-external-allowed', async () => { |
| 84 | + const response = await fetch(`http://localhost:3040/external-allowed`); |
| 85 | + const data = await response.json(); |
| 86 | + return data; |
| 87 | +}); |
| 88 | + |
| 89 | +// Outgoing fetch to external (disallowed by tracePropagationTargets) |
| 90 | +app.get('/test-outgoing-fetch-external-disallowed', async () => { |
| 91 | + const response = await fetch(`http://localhost:3040/external-disallowed`); |
| 92 | + const data = await response.json(); |
| 93 | + return data; |
| 94 | +}); |
| 95 | + |
| 96 | +// Route that throws a string (not an Error object) |
| 97 | +app.get('/test-string-error', () => { |
| 98 | + // eslint-disable-next-line no-throw-literal |
| 99 | + throw 'String error message'; |
| 100 | +}); |
| 101 | + |
| 102 | +// Route for concurrent isolation tests — returns scope data in response |
| 103 | +app.get('/test-isolation/:userId', async ({ params }) => { |
| 104 | + Sentry.setUser({ id: params.userId }); |
| 105 | + Sentry.setTag('user_id', params.userId); |
| 106 | + |
| 107 | + // Simulate async work to increase overlap between concurrent requests |
| 108 | + await new Promise(resolve => setTimeout(resolve, 200)); |
| 109 | + |
| 110 | + return { |
| 111 | + userId: params.userId, |
| 112 | + isolationScopeUserId: Sentry.getIsolationScope().getUser()?.id, |
| 113 | + isolationScopeTag: Sentry.getIsolationScope().getScopeData().tags?.user_id, |
| 114 | + }; |
| 115 | +}); |
| 116 | + |
| 117 | +// Flush route for waiting on events |
| 118 | +app.get('/flush', async () => { |
| 119 | + await Sentry.flush(); |
| 120 | + return { ok: true }; |
| 121 | +}); |
| 122 | + |
| 123 | +app.listen(3030, () => { |
| 124 | + console.log('Elysia app listening on port 3030'); |
| 125 | +}); |
| 126 | + |
| 127 | +// Second app for external propagation tests |
| 128 | +const app2 = new Elysia(); |
| 129 | + |
| 130 | +app2.get('/external-allowed', ({ request }) => { |
| 131 | + const headers = Object.fromEntries(request.headers.entries()); |
| 132 | + return { headers, route: '/external-allowed' }; |
| 133 | +}); |
| 134 | + |
| 135 | +app2.get('/external-disallowed', ({ request }) => { |
| 136 | + const headers = Object.fromEntries(request.headers.entries()); |
| 137 | + return { headers, route: '/external-disallowed' }; |
| 138 | +}); |
| 139 | + |
| 140 | +app2.listen(3040, () => { |
| 141 | + console.log('External app listening on port 3040'); |
| 142 | +}); |
0 commit comments