Sentry
This page explains how to set up Sentry debugging tools
Last updated
import * as Sentry from '@sentry/nestjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import constants from './constants';
import { httpIntegration } from '@sentry/nestjs';
Sentry.init({
dsn: constants.SENTRY_DNS,
environment: constants.SENTRY_ENVIRONMENT,
includeLocalVariables: true,
integrations: [nodeProfilingIntegration(), httpIntegration()],
tracesSampleRate: 0.1,
profilesSampleRate: 0.1,
beforeSend(event, hint) {
const error = hint?.originalException;
if (error && typeof error === 'object' && 'status' in error) {
const statusCode = (error as any).status;
if (statusCode >= 400 && statusCode < 500) {
// Do not send 4xx errors to Sentry
return null;
}
}
return event;
},
});import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import * as Sentry from '@sentry/node';
@Catch()
export class SentryFilter extends BaseExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const request = ctx.getRequest<Request>();
Sentry.withScope(scope => {
const headers = { ...request.headers };
delete headers['authorization']; // Remove Authorization token
scope.setExtras({
method: request.method,
url: request.url,
headers: headers,
user: request['user']
? {
email: request['user']?.email,
roles: request['user']?.roles,
teamId: request['user']?.teamId,
}
: null,
});
Sentry.captureException(exception);
});
super.catch(exception, host);
}
}providers: [
{
provide: APP_FILTER,
useClass: SentryGlobalFilter,
},
],@Get("/debug-sentry")
getError() {
throw new Error("My first Sentry error!");
}