|
| 1 | +const Sentry = require('@sentry/node'); |
| 2 | +import Hapi from '@hapi/hapi'; |
| 3 | + |
| 4 | +const server = Hapi.server({ |
| 5 | + port: 3030, |
| 6 | + host: 'localhost', |
| 7 | +}); |
| 8 | + |
| 9 | +declare global { |
| 10 | + namespace globalThis { |
| 11 | + var transactionIds: string[]; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +Sentry.init({ |
| 16 | + environment: 'qa', // dynamic sampling bias to keep transactions |
| 17 | + dsn: process.env.SENTRY_DSN, |
| 18 | + includeLocalVariables: true, |
| 19 | + integrations: [Sentry.hapiIntegration({ server })], |
| 20 | + debug: true, |
| 21 | + tunnel: `http://localhost:3031/`, // proxy server |
| 22 | + tracesSampleRate: 1, |
| 23 | +}); |
| 24 | + |
| 25 | +const init = async () => { |
| 26 | + server.route({ |
| 27 | + method: 'GET', |
| 28 | + path: '/test-success', |
| 29 | + handler: function (request, h) { |
| 30 | + console.log('test console'); |
| 31 | + return { version: 'v1' }; |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + server.route({ |
| 36 | + method: 'GET', |
| 37 | + path: '/test-error', |
| 38 | + handler: async function (request, h) { |
| 39 | + const exceptionId = Sentry.captureException(new Error('This is an error')); |
| 40 | + |
| 41 | + await Sentry.flush(2000); |
| 42 | + |
| 43 | + return { exceptionId }; |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + server.route({ |
| 48 | + method: 'GET', |
| 49 | + path: '/test-failure-uncaught', |
| 50 | + handler: async function (request, h) { |
| 51 | + throw new Error('This is an error'); |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + server.route({ |
| 56 | + method: 'GET', |
| 57 | + path: '/test-param-success/{param}', |
| 58 | + handler: function (request, h) { |
| 59 | + return { paramWas: request.params.param }; |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + server.route({ |
| 64 | + method: 'GET', |
| 65 | + path: '/test-param-error/{param}', |
| 66 | + handler: async function (request, h) { |
| 67 | + const exceptionId = Sentry.captureException(new Error('This is an error')); |
| 68 | + |
| 69 | + await Sentry.flush(2000); |
| 70 | + |
| 71 | + return { exceptionId, paramWas: request.params.param }; |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + server.route({ |
| 76 | + method: 'GET', |
| 77 | + path: '/test-success-manual', |
| 78 | + handler: async function (request, h) { |
| 79 | + Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => { |
| 80 | + Sentry.startSpan({ name: 'test-span' }, () => undefined); |
| 81 | + }); |
| 82 | + |
| 83 | + await Sentry.flush(); |
| 84 | + |
| 85 | + return { |
| 86 | + transactionIds: global.transactionIds || [], |
| 87 | + }; |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + server.route({ |
| 92 | + method: 'GET', |
| 93 | + path: '/test-error-manual', |
| 94 | + handler: async function (request, h) { |
| 95 | + Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => { |
| 96 | + Sentry.startSpan({ name: 'test-span' }, () => { |
| 97 | + Sentry.captureException(new Error('This is an error')); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + await Sentry.flush(2000); |
| 102 | + |
| 103 | + return { |
| 104 | + transactionIds: global.transactionIds || [], |
| 105 | + }; |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + server.route({ |
| 110 | + method: 'GET', |
| 111 | + path: '/test-local-variables-uncaught', |
| 112 | + handler: function (request, h) { |
| 113 | + const randomVariableToRecord = 'LOCAL VARIABLE'; |
| 114 | + throw new Error( |
| 115 | + `Uncaught Local Variable Error - ${JSON.stringify({ randomVariableToRecord })}`, |
| 116 | + ); |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + server.route({ |
| 121 | + method: 'GET', |
| 122 | + path: '/test-local-variables-caught', |
| 123 | + handler: function (request, h) { |
| 124 | + const randomVariableToRecord = 'LOCAL VARIABLE'; |
| 125 | + |
| 126 | + let exceptionId: string; |
| 127 | + try { |
| 128 | + throw new Error('Local Variable Error'); |
| 129 | + } catch (e) { |
| 130 | + exceptionId = Sentry.captureException(e); |
| 131 | + } |
| 132 | + |
| 133 | + return { exceptionId }; |
| 134 | + }, |
| 135 | + }); |
| 136 | +}; |
| 137 | + |
| 138 | +(async () => { |
| 139 | + init(); |
| 140 | + await server.start(); |
| 141 | + console.log('Server running on %s', server.info.uri); |
| 142 | +})(); |
0 commit comments