Skip to content

feat(express): Add express app example #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions apps/express/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "express-test-application",
"version": "1.0.0",
"main": "dist/main.js",
"directories": {
"lib": "lib"
},
"scripts": {
"build": "tsc",
"start": "yarn build && node dist/app.js",
"clean": "npx rimraf node_modules,pnpm-lock.yaml"
},
"license": "MIT",
"volta": {
"extends": "../../package.json"
},
"dependencies": {
"@sentry/node": "^7.109.0",
"express": "^4.19.2"
},
"devDependencies": {
"@types/express": "^4"
}
}
96 changes: 96 additions & 0 deletions apps/express/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as Sentry from '@sentry/node';
import express from 'express';

declare global {
namespace globalThis {
var transactionIds: string[];
}
}

const app = express();
const port = 3030;

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
includeLocalVariables: true,
debug: true,
tunnel: `http://127.0.0.1:3031/`, // proxy server
tracesSampleRate: 1,
});

app.use(Sentry.Handlers.requestHandler());

app.get('/test-success', function (req, res) {
res.send({ version: 'v1' });
});

app.get('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});

app.get('/test-transaction', async function (req, res) {
Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => {
Sentry.startSpan({ name: 'test-span' }, () => undefined);
});

await Sentry.flush();

res.send({
transactionIds: global.transactionIds || [],
});
});

app.get('/test-error', async function (req, res) {
const exceptionId = Sentry.captureException(new Error('This is an error'));

await Sentry.flush(2000);

res.send({ exceptionId });
});

app.get('/test-local-variables-uncaught', function (req, res) {
const randomVariableToRecord = Math.random();
throw new Error(`Uncaught Local Variable Error - ${JSON.stringify({ randomVariableToRecord })}`);
});

app.get('/test-local-variables-caught', function (req, res) {
const randomVariableToRecord = Math.random();

let exceptionId: string;
try {
throw new Error('Local Variable Error');
} catch (e) {
exceptionId = Sentry.captureException(e);
}

res.send({ exceptionId, randomVariableToRecord });
});

app.use(Sentry.Handlers.errorHandler());

// @ts-ignore
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});

Sentry.addEventProcessor(event => {
global.transactionIds = global.transactionIds || [];

if (event.type === 'transaction') {
const eventId = event.event_id;

if (eventId) {
global.transactionIds.push(eventId);
}
}

return event;
});
7 changes: 7 additions & 0 deletions apps/express/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"outDir": "dist"
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
},
"packageManager": "[email protected]",
"scripts": {
"start:express": "yarn workspace express-test-application run start",
"fix:prettier": "prettier . --write",
"fix:lint": "yarn run eslint --fix",
"lint": "yarn run eslint"
},
"workspaces": [
"apps/express"
],
"devDependencies": {
"@eslint/js": "^9.0.0",
"@types/node": "^20.12.5",
Expand Down
71 changes: 11 additions & 60 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,6 @@ __metadata:
languageName: node
linkType: hard

"@sentry/express-test-application@workspace:apps/express":
version: 0.0.0-use.local
resolution: "@sentry/express-test-application@workspace:apps/express"
dependencies:
"@sentry/node": "npm:^7.109.0"
"@types/express": "npm:^4"
express: "npm:^4.19.2"
ts-node: "npm:10.9.1"
languageName: unknown
linkType: soft

"@sentry/node@npm:^7.109.0":
version: 7.109.0
resolution: "@sentry/node@npm:7.109.0"
Expand Down Expand Up @@ -1270,23 +1259,23 @@ __metadata:
languageName: node
linkType: hard

"event-proxy-server@workspace:utils/event-proxy-server":
version: 0.0.0-use.local
resolution: "event-proxy-server@workspace:utils/event-proxy-server"
dependencies:
"@sentry/types": "npm:7.109.0"
"@sentry/utils": "npm:7.109.0"
ts-node: "npm:^10.4.0"
languageName: unknown
linkType: soft

"exponential-backoff@npm:^3.1.1":
version: 3.1.1
resolution: "exponential-backoff@npm:3.1.1"
checksum: 10c0/160456d2d647e6019640bd07111634d8c353038d9fa40176afb7cd49b0548bdae83b56d05e907c2cce2300b81cae35d800ef92fefb9d0208e190fa3b7d6bb579
languageName: node
linkType: hard

"express-test-application@workspace:apps/express":
version: 0.0.0-use.local
resolution: "express-test-application@workspace:apps/express"
dependencies:
"@sentry/node": "npm:^7.109.0"
"@types/express": "npm:^4"
express: "npm:^4.19.2"
languageName: unknown
linkType: soft

"express@npm:^4.19.2":
version: 4.19.2
resolution: "express@npm:4.19.2"
Expand Down Expand Up @@ -2929,45 +2918,7 @@ __metadata:
languageName: node
linkType: hard

"ts-node@npm:10.9.1":
version: 10.9.1
resolution: "ts-node@npm:10.9.1"
dependencies:
"@cspotcode/source-map-support": "npm:^0.8.0"
"@tsconfig/node10": "npm:^1.0.7"
"@tsconfig/node12": "npm:^1.0.7"
"@tsconfig/node14": "npm:^1.0.0"
"@tsconfig/node16": "npm:^1.0.2"
acorn: "npm:^8.4.1"
acorn-walk: "npm:^8.1.1"
arg: "npm:^4.1.0"
create-require: "npm:^1.1.0"
diff: "npm:^4.0.1"
make-error: "npm:^1.1.1"
v8-compile-cache-lib: "npm:^3.0.1"
yn: "npm:3.1.1"
peerDependencies:
"@swc/core": ">=1.2.50"
"@swc/wasm": ">=1.2.50"
"@types/node": "*"
typescript: ">=2.7"
peerDependenciesMeta:
"@swc/core":
optional: true
"@swc/wasm":
optional: true
bin:
ts-node: dist/bin.js
ts-node-cwd: dist/bin-cwd.js
ts-node-esm: dist/bin-esm.js
ts-node-script: dist/bin-script.js
ts-node-transpile-only: dist/bin-transpile.js
ts-script: dist/bin-script-deprecated.js
checksum: 10c0/95187932fb83f3901e22546bd2feeac7d2feb4f412f42ac3a595f049a23e8dcf70516dffb51866391228ea2dbcfaea039e250fb2bb334d48a86ab2b6aea0ae2d
languageName: node
linkType: hard

"ts-node@npm:10.9.2, ts-node@npm:^10.4.0":
"ts-node@npm:10.9.2":
version: 10.9.2
resolution: "ts-node@npm:10.9.2"
dependencies:
Expand Down