Skip to content

Commit b3fc0ac

Browse files
authored
Merge pull request #2 from getsentry/sig-add-express-app
feat(express): Add express app example
2 parents 9271b41 + 002aa2e commit b3fc0ac

File tree

5 files changed

+142
-60
lines changed

5 files changed

+142
-60
lines changed

apps/express/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "express-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.109.0",
19+
"express": "^4.19.2"
20+
},
21+
"devDependencies": {
22+
"@types/express": "^4"
23+
}
24+
}

apps/express/src/app.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import * as Sentry from '@sentry/node';
2+
import express from 'express';
3+
4+
declare global {
5+
namespace globalThis {
6+
var transactionIds: string[];
7+
}
8+
}
9+
10+
const app = express();
11+
const port = 3030;
12+
13+
Sentry.init({
14+
environment: 'qa', // dynamic sampling bias to keep transactions
15+
dsn: process.env.E2E_TEST_DSN,
16+
includeLocalVariables: true,
17+
debug: true,
18+
tunnel: `http://127.0.0.1:3031/`, // proxy server
19+
tracesSampleRate: 1,
20+
});
21+
22+
app.use(Sentry.Handlers.requestHandler());
23+
24+
app.get('/test-success', function (req, res) {
25+
res.send({ version: 'v1' });
26+
});
27+
28+
app.get('/test-param/:param', function (req, res) {
29+
res.send({ paramWas: req.params.param });
30+
});
31+
32+
app.get('/test-transaction', async function (req, res) {
33+
Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => {
34+
Sentry.startSpan({ name: 'test-span' }, () => undefined);
35+
});
36+
37+
await Sentry.flush();
38+
39+
res.send({
40+
transactionIds: global.transactionIds || [],
41+
});
42+
});
43+
44+
app.get('/test-error', async function (req, res) {
45+
const exceptionId = Sentry.captureException(new Error('This is an error'));
46+
47+
await Sentry.flush(2000);
48+
49+
res.send({ exceptionId });
50+
});
51+
52+
app.get('/test-local-variables-uncaught', function (req, res) {
53+
const randomVariableToRecord = Math.random();
54+
throw new Error(`Uncaught Local Variable Error - ${JSON.stringify({ randomVariableToRecord })}`);
55+
});
56+
57+
app.get('/test-local-variables-caught', function (req, res) {
58+
const randomVariableToRecord = Math.random();
59+
60+
let exceptionId: string;
61+
try {
62+
throw new Error('Local Variable Error');
63+
} catch (e) {
64+
exceptionId = Sentry.captureException(e);
65+
}
66+
67+
res.send({ exceptionId, randomVariableToRecord });
68+
});
69+
70+
app.use(Sentry.Handlers.errorHandler());
71+
72+
// @ts-ignore
73+
app.use(function onError(err, req, res, next) {
74+
// The error id is attached to `res.sentry` to be returned
75+
// and optionally displayed to the user for support.
76+
res.statusCode = 500;
77+
res.end(res.sentry + '\n');
78+
});
79+
80+
app.listen(port, () => {
81+
console.log(`Example app listening on port ${port}`);
82+
});
83+
84+
Sentry.addEventProcessor(event => {
85+
global.transactionIds = global.transactionIds || [];
86+
87+
if (event.type === 'transaction') {
88+
const eventId = event.event_id;
89+
90+
if (eventId) {
91+
global.transactionIds.push(eventId);
92+
}
93+
}
94+
95+
return event;
96+
});

apps/express/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
@@ -8,10 +8,14 @@
88
},
99
"packageManager": "[email protected]",
1010
"scripts": {
11+
"start:express": "yarn workspace express-test-application run start",
1112
"fix:prettier": "prettier . --write",
1213
"fix:lint": "yarn run eslint --fix",
1314
"lint": "yarn run eslint"
1415
},
16+
"workspaces": [
17+
"apps/express"
18+
],
1519
"devDependencies": {
1620
"@eslint/js": "^9.0.0",
1721
"@types/node": "^20.12.5",

yarn.lock

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,6 @@ __metadata:
221221
languageName: node
222222
linkType: hard
223223

224-
"@sentry/express-test-application@workspace:apps/express":
225-
version: 0.0.0-use.local
226-
resolution: "@sentry/express-test-application@workspace:apps/express"
227-
dependencies:
228-
"@sentry/node": "npm:^7.109.0"
229-
"@types/express": "npm:^4"
230-
express: "npm:^4.19.2"
231-
ts-node: "npm:10.9.1"
232-
languageName: unknown
233-
linkType: soft
234-
235224
"@sentry/node@npm:^7.109.0":
236225
version: 7.109.0
237226
resolution: "@sentry/node@npm:7.109.0"
@@ -1270,23 +1259,23 @@ __metadata:
12701259
languageName: node
12711260
linkType: hard
12721261

1273-
"event-proxy-server@workspace:utils/event-proxy-server":
1274-
version: 0.0.0-use.local
1275-
resolution: "event-proxy-server@workspace:utils/event-proxy-server"
1276-
dependencies:
1277-
"@sentry/types": "npm:7.109.0"
1278-
"@sentry/utils": "npm:7.109.0"
1279-
ts-node: "npm:^10.4.0"
1280-
languageName: unknown
1281-
linkType: soft
1282-
12831262
"exponential-backoff@npm:^3.1.1":
12841263
version: 3.1.1
12851264
resolution: "exponential-backoff@npm:3.1.1"
12861265
checksum: 10c0/160456d2d647e6019640bd07111634d8c353038d9fa40176afb7cd49b0548bdae83b56d05e907c2cce2300b81cae35d800ef92fefb9d0208e190fa3b7d6bb579
12871266
languageName: node
12881267
linkType: hard
12891268

1269+
"express-test-application@workspace:apps/express":
1270+
version: 0.0.0-use.local
1271+
resolution: "express-test-application@workspace:apps/express"
1272+
dependencies:
1273+
"@sentry/node": "npm:^7.109.0"
1274+
"@types/express": "npm:^4"
1275+
express: "npm:^4.19.2"
1276+
languageName: unknown
1277+
linkType: soft
1278+
12901279
"express@npm:^4.19.2":
12911280
version: 4.19.2
12921281
resolution: "express@npm:4.19.2"
@@ -2929,45 +2918,7 @@ __metadata:
29292918
languageName: node
29302919
linkType: hard
29312920

2932-
"ts-node@npm:10.9.1":
2933-
version: 10.9.1
2934-
resolution: "ts-node@npm:10.9.1"
2935-
dependencies:
2936-
"@cspotcode/source-map-support": "npm:^0.8.0"
2937-
"@tsconfig/node10": "npm:^1.0.7"
2938-
"@tsconfig/node12": "npm:^1.0.7"
2939-
"@tsconfig/node14": "npm:^1.0.0"
2940-
"@tsconfig/node16": "npm:^1.0.2"
2941-
acorn: "npm:^8.4.1"
2942-
acorn-walk: "npm:^8.1.1"
2943-
arg: "npm:^4.1.0"
2944-
create-require: "npm:^1.1.0"
2945-
diff: "npm:^4.0.1"
2946-
make-error: "npm:^1.1.1"
2947-
v8-compile-cache-lib: "npm:^3.0.1"
2948-
yn: "npm:3.1.1"
2949-
peerDependencies:
2950-
"@swc/core": ">=1.2.50"
2951-
"@swc/wasm": ">=1.2.50"
2952-
"@types/node": "*"
2953-
typescript: ">=2.7"
2954-
peerDependenciesMeta:
2955-
"@swc/core":
2956-
optional: true
2957-
"@swc/wasm":
2958-
optional: true
2959-
bin:
2960-
ts-node: dist/bin.js
2961-
ts-node-cwd: dist/bin-cwd.js
2962-
ts-node-esm: dist/bin-esm.js
2963-
ts-node-script: dist/bin-script.js
2964-
ts-node-transpile-only: dist/bin-transpile.js
2965-
ts-script: dist/bin-script-deprecated.js
2966-
checksum: 10c0/95187932fb83f3901e22546bd2feeac7d2feb4f412f42ac3a595f049a23e8dcf70516dffb51866391228ea2dbcfaea039e250fb2bb334d48a86ab2b6aea0ae2d
2967-
languageName: node
2968-
linkType: hard
2969-
2970-
"ts-node@npm:10.9.2, ts-node@npm:^10.4.0":
2921+
"ts-node@npm:10.9.2":
29712922
version: 10.9.2
29722923
resolution: "ts-node@npm:10.9.2"
29732924
dependencies:

0 commit comments

Comments
 (0)