Home > Article > Web Front-end > How to Implement Sentry in a NestJS Project
Sentry is an essential tool for monitoring errors and performance in applications. It automatically captures and reports exceptions, in addition to allowing performance tracking and code profiling. In this article, we'll explore how to configure and use Sentry in a NestJS project.
1. Installing Sentry
First, you need to add the necessary dependencies to your NestJS project. Run the following command:
npm install @sentry/nestjs @sentry/profiling-node
2. Configuring Sentry
After installing the dependencies, configure Sentry in your project. Create a file for Sentry settings, for example, sentry.config.ts, and add the following code:
import * as Sentry from '@sentry/nestjs'; import { nodeProfilingIntegration } from '@sentry/profiling-node'; Sentry.init({ dsn: process.env.SENTRY_DSN, // Adicione sua DSN do Sentry aqui integrations: [nodeProfilingIntegration()], tracesSampleRate: 1.0, // Captura 100% das transações para monitoramento de performance profilesSampleRate: 1.0, // Define a taxa de amostragem para profiling de código });
*3. Integrating Sentry into Application Launch
*
Now, you need to integrate Sentry into your application startup. In the main startup file (main.ts), configure Sentry to catch and report exceptions as shown below:
import '../src/config/sentry'; // Importa a configuração do Sentry import { BaseExceptionFilter, HttpAdapterHost, NestFactory, } from '@nestjs/core'; import { AppModule } from './app.module'; import * as Sentry from '@sentry/nestjs'; async function bootstrap() { const app = await NestFactory.create(AppModule, { rawBody: true }); // Configurando o Sentry para capturar e relatar exceções const { httpAdapter } = app.get(HttpAdapterHost); Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter)); await app.listen(3000); } bootstrap();
Conclusion
With these settings, Sentry is now integrated into your NestJS project, monitoring errors, exceptions and application performance. This can be crucial for identifying and resolving issues quickly, ensuring a more stable and predictable user experience.
Remember to adjust sampling rates according to the environment and application criticality to avoid unnecessary overhead.
The above is the detailed content of How to Implement Sentry in a NestJS Project. For more information, please follow other related articles on the PHP Chinese website!