Skip to content

docs(js): Review and update Nest.js Quick Start guide #13497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/platforms/javascript/guides/nestjs/index.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Nest.js
description: "Learn about using Sentry with Nest.js."
description: "Learn how to set up Sentry in your Nest.js app and capture your first errors."
sdk: sentry.javascript.nestjs
fallbackGuide: javascript.node
categories:
Expand All @@ -9,6 +9,4 @@ categories:
- server-node
---

This guide explains how to set up Sentry in your Nest.js application.

<PlatformContent includePath="getting-started-node" />
27 changes: 19 additions & 8 deletions platform-includes/getting-started-config/javascript.nestjs.mdx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
```javascript {tabTitle:ESM} {filename: instrument.mjs}
import * as Sentry from "@sentry/nestjs";
// ___PRODUCT_OPTION_START___ profiling
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { nodeProfilingIntegration } from "@sentry/profiling-node";
// ___PRODUCT_OPTION_END___ profiling

// Ensure to call this before importing any other modules!
Sentry.init({
dsn: "___PUBLIC_DSN___",

// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// ___PRODUCT_OPTION_START___ profiling
integrations: [
// Add our Profiling integration
Expand All @@ -20,14 +20,20 @@ Sentry.init({
// ___PRODUCT_OPTION_END___ profiling
// ___PRODUCT_OPTION_START___ performance

// Add Tracing by setting tracesSampleRate
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
// ___PRODUCT_OPTION_START___ profiling

// Set sampling rate for profiling
// Set profilesSampleRate to 1.0 to profile 100%
// of sampled transactions.
// This is relative to tracesSampleRate
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate
profilesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ profiling
});
Expand All @@ -50,16 +56,21 @@ Sentry.init({
// ___PRODUCT_OPTION_END___ profiling
// ___PRODUCT_OPTION_START___ performance

// Add Tracing by setting tracesSampleRate
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
// ___PRODUCT_OPTION_START___ profiling

// Set sampling rate for profiling
// Set profilesSampleRate to 1.0 to profile 100%
// of sampled transactions.
// This is relative to tracesSampleRate
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate
profilesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ profiling
});
```

1 change: 1 addition & 0 deletions platform-includes/getting-started-node/javascript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Our next recommended steps for you are:
<Expandable permalink={false} title="Are you having problems setting up the SDK?">

- Check out alternative <PlatformLink to="/install">installation methods</PlatformLink>
- Find various topics in <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>
- [Get support](https://sentry.zendesk.com/hc/en-us/)

</Expandable>
24 changes: 13 additions & 11 deletions platform-includes/getting-started-use/javascript.nestjs.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
```javascript {filename: main.ts}
<PlatformContent includePath="getting-started-config" />

You need to require or import the `instrument.js` file before requiring any other modules in your application. This is necessary to ensure that Sentry can automatically instrument all modules in your application:

```javascript {filename: main.ts} {1-2}
// Import this first!
import "./instrument";

Expand Down Expand Up @@ -56,7 +60,7 @@ If you don't have a global catch-all exception filter, add the `SentryGlobalFilt
This filter will report any unhandled errors that aren't caught by other error filters to Sentry.
**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters.

```javascript {3, 9}
```javascript {2-3, 7-10}
import { Module } from "@nestjs/common";
import { APP_FILTER } from "@nestjs/core";
import { SentryGlobalFilter } from "@sentry/nestjs/setup";
Expand All @@ -73,18 +77,18 @@ import { SentryGlobalFilter } from "@sentry/nestjs/setup";
export class AppModule {}
```

<Expandable title="Using Microservices?">
<Expandable title="Are you using Microservices?">

If you are using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters).
If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters).
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.

Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:

```typescript
import { Catch, RpcExceptionFilter, ArgumentsHost } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { RpcException } from '@nestjs/microservices';
import * as Sentry from '@sentry/nestjs';
import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common";
import { Observable, throwError } from "rxjs";
import { RpcException } from "@nestjs/microservices";
import * as Sentry from "@sentry/nestjs";

@Catch(RpcException)
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
Expand All @@ -95,13 +99,11 @@ export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
}
```


</Expandable>


If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`:

```javascript {9}
```javascript {4-9}
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { ExampleException } from './example.exception';
Expand Down
40 changes: 38 additions & 2 deletions platform-includes/getting-started-verify/javascript.nestjs.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
### Issues

First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following route to your application, which will call an undefined function, triggering an error that Sentry will capture:

```javascript
@Get("/debug-sentry")
getError() {
throw new Error("My first Sentry error!");
getError() {
try {
foo();
} catch (e) {
Sentry.captureException(e);
}
}
```

<OnboardingOption optionId="performance">
### Tracing

To test your tracing configuration, update the previous code snippet by starting a performance trace to measure the time it takes for the execution of your code:

```javascript
@Get("/debug-sentry")
getError() {
Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
() => {
setTimeout(() => {
try {
foo();
} catch (e) {
Sentry.captureException(e);
}
}, 99);
},
);
}
```

</OnboardingOption>