Skip to content

Commit 0937393

Browse files
committed
add connect app v7
1 parent e2ae8b1 commit 0937393

16 files changed

+2954
-2
lines changed

apps/connect/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "connect-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+
"@sentry/node": "7.110.1",
19+
"connect": "3.7.0",
20+
"dotenv": "^16.4.5"
21+
},
22+
"devDependencies": {
23+
"@types/connect": "3.4.38"
24+
}
25+
}

apps/connect/src/app.ts

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

apps/connect/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
@@ -11,18 +11,21 @@
1111
"start:proxy-server": "yarn workspace event-proxy-server run start",
1212
"start:proxy-server:express": "APP_NAME=express yarn workspace event-proxy-server run start",
1313
"start:proxy-server:fastify": "APP_NAME=fastify yarn workspace event-proxy-server run start",
14+
"start:proxy-server:connect": "APP_NAME=connect yarn workspace event-proxy-server run start",
1415
"start:proxy-server:nestjs": "APP_NAME=nestjs yarn workspace event-proxy-server run start",
1516
"start:proxy-server:koa": "APP_NAME=koa yarn workspace event-proxy-server run start",
1617
"start:proxy-server:nextjs-13_2_0": "APP_NAME=nextjs-13_2_0 yarn workspace event-proxy-server run start",
1718
"start:proxy-server:nextjs-14_2_1": "APP_NAME=nextjs-14_2_1 yarn workspace event-proxy-server run start",
1819
"start:app:express": "yarn workspace express-test-application run start",
1920
"start:app:fastify": "yarn workspace fastify-test-application run start",
21+
"start:app:connect": "APP_NAME=connect yarn workspace connect-test-application run start",
2022
"start:app:nestjs": "yarn workspace nestjs-test-application run start",
2123
"start:app:koa": "yarn workspace koa-test-application run start",
2224
"start:app:nextjs-14_2_1": "yarn workspace nextjs-14_2_1-test-application run dev",
2325
"start:app:nextjs-13_2_0": "yarn workspace nextjs-13_2_0-test-application run dev",
2426
"start:express": "run-p start:proxy-server:express start:app:express",
2527
"start:fastify": "run-p start:proxy-server:fastify start:app:fastify",
28+
"start:connect": "run-p start:proxy-server:connect start:app:connect",
2629
"start:nestjs": "run-p start:proxy-server:nestjs start:app:nestjs",
2730
"start:koa": "run-p start:proxy-server:koa start:app:koa",
2831
"start:nextjs-13_2_0": "run-p start:proxy-server:nextjs-13_2_0 start:app:nextjs-13_2_0",
@@ -35,6 +38,7 @@
3538
"utils/event-proxy-server",
3639
"apps/express",
3740
"apps/fastify",
41+
"apps/connect",
3842
"apps/nestjs",
3943
"apps/koa",
4044
"apps/nextjs-13_2_0",

0 commit comments

Comments
 (0)