Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(playground): add express node test (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa authored Oct 4, 2022
1 parent b65416d commit 6206c74
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions playground/express/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const express = require('express');
const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
const { ProfilingIntegration } = require('../../lib');

const app = express();
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

Sentry.init({
dsn: 'https://[email protected]/6625302',
tracesSampleRate: 1,
// @ts-expect-error profilesSampleRate is not yet in the official types
profilesSampleRate: 1,
debug: true,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({
// to trace all requests to the default router
app
// alternatively, you can specify the routes you want to trace:
// router: someRouter,
}),
new ProfilingIntegration()
]
});

// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.tracingHandler());
app.use(Sentry.Handlers.requestHandler());

// All controllers should live here
app.get('/', function rootHandler(_req, res) {
res.end('Hello world!');
});

app.get('/slow', async function rootHandler(_req, res) {
await wait(1000);
res.end('Hello world!');
});

// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());

// Optional fallthrough error handler
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(3000, () => {
console.log('Server listening on port 3000');
});

0 comments on commit 6206c74

Please sign in to comment.