Skip to content

Instantly share code, notes, and snippets.

@elliotblackburn
Created March 7, 2018 11:53
Show Gist options
  • Save elliotblackburn/8dbfb5fb92e60b5dabc72711c78fd2d9 to your computer and use it in GitHub Desktop.
Save elliotblackburn/8dbfb5fb92e60b5dabc72711c78fd2d9 to your computer and use it in GitHub Desktop.
Sentry configuration example
import * as raven from 'raven';
import * as express from 'express';
import * as packageJson from './package.json';
const app: express.Express = express();
const SENTRY_DNS = 'your sentry dns setting will be here';
app.use(configureSentry(SENTRY_DNS));
app.use(sentry.errorHandler());
function prepareSentryData(data: any): any {
// This function is called with all the data that Raven is going to ship
// up to Sentry.
// You may choose to scrub out some specific data from `data.request` here
// as this might contain cookies, access tokens, etc, which you may not wish
// to leak. Sentry does some auto filtering, but that's on Sentry's server side.
// This may not be an issue if you're self-hosting Sentry, it's up to you!
// Modify the stack trace to pick up our sourcemaps
const stacktrace = data.exception && data.exception[0].stacktrace;
if (stacktrace && stacktrace.frames) {
stacktrace.frames.forEach((frame: any) => {
if (frame.filename.startsWith('/')) {
// We use __dirname || process.cwd() here, this only works if this file is in the
// root if your project. If it is not, you should check the Sentry TypeScript docs
// https://docs.sentry.io/clients/node/typescript/
frame.filename =
'app:///' + path.relative(__dirname || process.cwd(), frame.filename);
}
});
}
return data;
}
function configureSentry(
dns: string
): (req: IncomingMessage, res: ServerResponse, next: () => void) => void {
sentry
.config(dns, {
dataCallback: prepareSentryData,
// This version must be a string which is the same as your sentry release.
// We keep our package.json, git tags, and sentry release versions all the
// same to make this easy, so here we just feed from that. You may choose
// to do something different such as reading from an environment variable.
release: (packageJson as any).version,
})
.install();
return sentry.requestHandler();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment