Skip to content

Commit ab71c59

Browse files
committed
add hapi v7
1 parent 23e1e17 commit ab71c59

16 files changed

+3026
-1
lines changed

apps/hapi/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "hapi-test-application",
3+
"version": "1.0.0",
4+
"main": "dist/main.js",
5+
"directories": {
6+
"lib": "lib"
7+
},
8+
"scripts": {
9+
"build": "tsc",
10+
"start": "yarn build && node dist/app.js",
11+
"clean": "npx rimraf node_modules,pnpm-lock.yaml"
12+
},
13+
"license": "MIT",
14+
"volta": {
15+
"extends": "../../package.json"
16+
},
17+
"dependencies": {
18+
"@hapi/hapi": "21.3.9",
19+
"@koa/router": "12.0.1",
20+
"@sentry/node": "7.113.0",
21+
"dotenv": "^16.4.5"
22+
}
23+
}

apps/hapi/src/app.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
})();

apps/hapi/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src/**/*.ts"],
4+
"compilerOptions": {
5+
"outDir": "dist"
6+
}
7+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"start:proxy-server:fastify": "APP_NAME=fastify yarn workspace event-proxy-server run start",
1515
"start:proxy-server:connect": "APP_NAME=connect yarn workspace event-proxy-server run start",
1616
"start:proxy-server:nestjs": "APP_NAME=nestjs yarn workspace event-proxy-server run start",
17+
"start:proxy-server:hapi": "APP_NAME=hapi yarn workspace event-proxy-server run start",
1718
"start:proxy-server:koa": "APP_NAME=koa yarn workspace event-proxy-server run start",
1819
"start:proxy-server:nextjs-13_2_0": "APP_NAME=nextjs-13_2_0 yarn workspace event-proxy-server run start",
1920
"start:proxy-server:nextjs-14_2_1": "APP_NAME=nextjs-14_2_1 yarn workspace event-proxy-server run start",
@@ -24,6 +25,7 @@
2425
"start:app:fastify": "yarn workspace fastify-test-application run start",
2526
"start:app:connect": "APP_NAME=connect yarn workspace connect-test-application run start",
2627
"start:app:nestjs": "yarn workspace nestjs-test-application run start",
28+
"start:app:hapi": "yarn workspace hapi-test-application run start",
2729
"start:app:koa": "yarn workspace koa-test-application run start",
2830
"start:app:nextjs-13_2_0": "yarn workspace nextjs-13_2_0-test-application run dev",
2931
"start:app:nextjs-14_2_1": "yarn workspace nextjs-14_2_1-test-application run dev",
@@ -34,6 +36,7 @@
3436
"start:fastify": "run-p start:proxy-server:fastify start:app:fastify",
3537
"start:connect": "run-p start:proxy-server:connect start:app:connect",
3638
"start:nestjs": "run-p start:proxy-server:nestjs start:app:nestjs",
39+
"start:hapi": "run-p start:proxy-server:hapi start:app:hapi",
3740
"start:koa": "run-p start:proxy-server:koa start:app:koa",
3841
"start:nextjs-13_2_0": "run-p start:proxy-server:nextjs-13_2_0 start:app:nextjs-13_2_0",
3942
"start:nextjs-14_2_1": "run-p start:proxy-server:nextjs-14_2_1 start:app:nextjs-14_2_1",
@@ -50,6 +53,7 @@
5053
"apps/fastify",
5154
"apps/connect",
5255
"apps/nestjs",
56+
"apps/hapi",
5357
"apps/koa",
5458
"apps/nextjs-13_2_0",
5559
"apps/nextjs-14_2_1",

0 commit comments

Comments
 (0)