# NestJS Error Tracking and Performance Monitoring

> Error tracking that helps developers monitor and fix crashes in real time. Iterate continuously. Boost workflow efficiency. Improve user experience.

**URL:** https://sentry.io/for/nestjs/

---

**Documentation:** [https://docs.sentry.io/platforms/javascript/guides/nestjs/](https://docs.sentry.io/platforms/javascript/guides/nestjs/)

## Install

Add `@sentry/nestjs` as a dependency:

```bash
npm install --save @sentry/nestjs
```

You need to create a file named `instrument.ts` that imports and initializes Sentry:

```typescript
import * as Sentry from '@sentry/nestjs';

// Ensure to call this before importing any other modules!
Sentry.init({
  dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
});
```

Import the `instrument.ts` file in `main.ts` before any other modules. This is necessary to ensure that Sentry can automatically instrument all modules in your application:

```typescript
// Import this first!
import './instrument';

// Now import other modules
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}

bootstrap();
```

Afterward, add the `SentryModule` as a root module to your main module:

```typescript
import { Module } from "@nestjs/common";
import { SentryModule } from "@sentry/nestjs/setup";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";

@Module({
  imports: [
    SentryModule.forRoot(),
    // ...other modules
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

In case you are using a global catch-all exception filter (which is either a filter registered with `app.useGlobalFilters()` or a filter registered in your app module providers annotated with an empty `@Catch()` decorator), add a `@SentryExceptionCaptured()` decorator to the `catch()` method of this global error filter. This decorator will report all unexpected errors that are received by your global error filter to Sentry:

```typescript
import { Catch, ExceptionFilter } from '@nestjs/common';
import { SentryExceptionCaptured } from '@sentry/nestjs';

@Catch()
export class YourCatchAllExceptionFilter implements ExceptionFilter {
  @SentryExceptionCaptured()
  catch(exception, host): void {
    // your implementation here
  }
}
```

In case you do not have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. This filter will report all unhandled errors that are not caught by any other error filter to Sentry. Important: The `SentryGlobalFilter` needs to be registered before any other exception filters.

```typescript
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { SentryGlobalFilter } from '@sentry/nestjs/setup';

@Module({
  providers: [
    {
      provide: APP_FILTER,
      useClass: SentryGlobalFilter,
    },
    // ..other providers
  ],
})
export class AppModule {}
```

See the [NestJS docs](https://docs.sentry.io/platforms/javascript/guides/nestjs/) for full setup options.

## Features

### Works With OpenTelemetry

Sentry traces your app out of the box, with rich distributed tracing context across the libraries and frameworks you use. Already running OpenTelemetry? Sentry runs alongside your existing setup without taking it over, and can capture spans from `@opentelemetry/api` when you turn it on.

### NestJS Performance Monitoring

Quickly identify NestJS [performance](https://sentry.io/product/tracing/) issues and view the full end-to-end distributed trace to see the exact, poor-performing API call and surface any related errors.

### NestJS Error Monitoring with Complete Stack Traces

See the NestJS source code at each frame instead of settling for just line and column numbers. Get proper asynchronous context tracking — like thread-local store — in a way that fits Node's concurrency model.

### Fill In The Blanks About NestJS Errors

See what the app was doing when the NestJS error occurred: HTTP requests, database queries, console log statements, etc.

### NestJS Application Profiling

Profiling lets you see what parts of your code are consuming the most resources, like CPU or memory, in your application— so you can optimize them before end user experience is impacted. Test your application performance in any environment, including in production, without writing manual tests or extensive troubleshooting.

---

*Source: [sentry.io/for/nestjs/](https://sentry.io/for/nestjs/)*
