|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import connect from 'connect'; |
| 3 | +import dotenv from 'dotenv'; |
| 4 | + |
| 5 | +dotenv.config({ path: './../../.env' }); |
| 6 | + |
| 7 | +declare global { |
| 8 | + namespace globalThis { |
| 9 | + var transactionIds: string[]; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +const app = connect(); |
| 14 | +const port = 3030; |
| 15 | + |
| 16 | +Sentry.init({ |
| 17 | + environment: 'qa', // dynamic sampling bias to keep transactions |
| 18 | + dsn: process.env.SENTRY_DSN, |
| 19 | + includeLocalVariables: true, |
| 20 | + debug: true, |
| 21 | + tunnel: `http://localhost:3031/`, // proxy server |
| 22 | + tracesSampleRate: 1, |
| 23 | +}); |
| 24 | + |
| 25 | +app.use(Sentry.Handlers.requestHandler()); |
| 26 | +app.use(Sentry.Handlers.tracingHandler()); |
| 27 | + |
| 28 | +const stringify = (obj: any) => JSON.stringify(obj, null, 2); |
| 29 | + |
| 30 | +app.use('/test-success', function (req, res) { |
| 31 | + res.end(stringify({ version: 'v1' })); |
| 32 | +}); |
| 33 | + |
| 34 | +app.use('/test-error', async function (req, res) { |
| 35 | + const exceptionId = Sentry.captureException(new Error('This is an error')); |
| 36 | + |
| 37 | + await Sentry.flush(2000); |
| 38 | + |
| 39 | + res.end(stringify({ exceptionId })); |
| 40 | +}); |
| 41 | + |
| 42 | +app.use('/test-param-success/', function (req, res) { |
| 43 | + const param = req.url?.replace('/', ''); |
| 44 | + res.end(stringify({ paramWas: param })); |
| 45 | +}); |
| 46 | + |
| 47 | +app.use('/test-param-error/', async function (req, res) { |
| 48 | + const param = req.url?.replace('/', ''); |
| 49 | + |
| 50 | + const exceptionId = Sentry.captureException(new Error('This is an error')); |
| 51 | + |
| 52 | + await Sentry.flush(2000); |
| 53 | + |
| 54 | + res.end(stringify({ exceptionId, paramWas: param })); |
| 55 | +}); |
| 56 | + |
| 57 | +app.use('/test-success-manual', async function (req, res) { |
| 58 | + Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => { |
| 59 | + Sentry.startSpan({ name: 'test-span' }, () => undefined); |
| 60 | + }); |
| 61 | + |
| 62 | + await Sentry.flush(); |
| 63 | + |
| 64 | + res.end( |
| 65 | + stringify({ |
| 66 | + transactionIds: global.transactionIds || [], |
| 67 | + }), |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +app.use('/test-error-manual', async function (req, res) { |
| 72 | + Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => { |
| 73 | + Sentry.startSpan({ name: 'test-span' }, () => { |
| 74 | + Sentry.captureException(new Error('This is an error')); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + await Sentry.flush(2000); |
| 79 | + |
| 80 | + res.end( |
| 81 | + stringify({ |
| 82 | + transactionIds: global.transactionIds || [], |
| 83 | + }), |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | +app.use('/test-local-variables-uncaught', function (req, res) { |
| 88 | + const randomVariableToRecord = 'LOCAL VARIABLE'; |
| 89 | + throw new Error(`Uncaught Local Variable Error - ${stringify({ randomVariableToRecord })}`); |
| 90 | +}); |
| 91 | + |
| 92 | +app.use('/test-local-variables-caught', function (req, res) { |
| 93 | + const randomVariableToRecord = 'LOCAL VARIABLE'; |
| 94 | + |
| 95 | + let exceptionId: string; |
| 96 | + try { |
| 97 | + throw new Error('Local Variable Error'); |
| 98 | + } catch (e) { |
| 99 | + exceptionId = Sentry.captureException(e); |
| 100 | + } |
| 101 | + |
| 102 | + res.end(stringify({ exceptionId, randomVariableToRecord })); |
| 103 | +}); |
| 104 | + |
| 105 | +app.use(Sentry.Handlers.errorHandler()); |
| 106 | + |
| 107 | +// @ts-ignore |
| 108 | +app.use(function onError(err, req, res, next) { |
| 109 | + // The error id is attached to `res.sentry` to be returned |
| 110 | + // and optionally displayed to the user for support. |
| 111 | + res.statusCode = 500; |
| 112 | + res.end(res.sentry + '\n'); |
| 113 | +}); |
| 114 | + |
| 115 | +app.listen(port, () => { |
| 116 | + console.log(`Example app listening on port ${port}`); |
| 117 | +}); |
| 118 | + |
| 119 | +Sentry.addEventProcessor(event => { |
| 120 | + global.transactionIds = global.transactionIds || []; |
| 121 | + |
| 122 | + if (event.type === 'transaction') { |
| 123 | + const eventId = event.event_id; |
| 124 | + |
| 125 | + if (eventId) { |
| 126 | + global.transactionIds.push(eventId); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return event; |
| 131 | +}); |
0 commit comments