Skip to content

Commit bbc481d

Browse files
committed
fix: clean up
fix some snippets and text, stick with CJS, and link to Sentry to cover ESM related topics
1 parent b137b4a commit bbc481d

File tree

1 file changed

+59
-64
lines changed

1 file changed

+59
-64
lines changed

content/recipes/sentry.md

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,58 @@ First, install the required dependencies:
1010
```bash
1111
$ npm install --save @sentry/nestjs @sentry/profiling-node
1212
```
13+
> info **Hint** we support `yarn` and `pnpm` as well. @sentry/profiling-node is optional, but recommended for performance profiling.
1314
1415

1516
#### Basic Setup
1617

1718
To get started with Sentry, you'll need to create an initialization file (e.g., `sentry.init.ts`) that should be imported before any other modules in your application:
1819

1920
```typescript
20-
import as Sentry from '@sentry/nestjs';
21-
import { nodeProfilingIntegration } from '@sentry/profiling-node';
21+
@@filename(instrument)
22+
const Sentry = require("@sentry/nestjs");
23+
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
24+
25+
// Ensure to call this before requiring any other modules!
2226
Sentry.init({
23-
dsn: 'your-sentry-dsn',
24-
integrations: [
25-
nodeProfilingIntegration(),
26-
],
27-
// Set sampling rate for tracing (adjust in production)
28-
tracesSampleRate: 1.0,
29-
// Set sampling rate for profiling
30-
profilesSampleRate: 1.0,
27+
dsn: SENTRY_DSN,
28+
integrations: [
29+
// Add our Profiling integration
30+
nodeProfilingIntegration(),
31+
],
32+
33+
// Add Tracing by setting tracesSampleRate
34+
// We recommend adjusting this value in production
35+
tracesSampleRate: 1.0,
36+
37+
// Set sampling rate for profiling
38+
// This is relative to tracesSampleRate
39+
profilesSampleRate: 1.0,
3140
});
3241

42+
3343
```
3444

3545

3646
Update your `main.ts` file to import the Sentry initialization before other imports:
3747

3848

3949
```typescript
40-
// Import Sentry initialization first
41-
import './sentry.init';
42-
import { NestFactory } from '@nestjs/core';
43-
import { AppModule } from './app.module';
50+
@@filename(main)
51+
// Import this first!
52+
import "./instrument";
53+
54+
// Now import other modules
55+
import { NestFactory } from "@nestjs/core";
56+
import { AppModule } from "./app.module";
57+
4458
async function bootstrap() {
45-
const app = await NestFactory.create(AppModule);
46-
await app.listen(3000);
59+
const app = await NestFactory.create(AppModule);
60+
await app.listen(3000);
4761
}
62+
4863
bootstrap();
64+
4965
```
5066

5167

@@ -55,55 +71,34 @@ Add the SentryModule to your application's root module:
5571

5672

5773
```typescript
58-
import { Module } from '@nestjs/common';
59-
import { SentryModule } from '@sentry/nestjs/setup';
60-
import { AppController } from './app.controller';
61-
import { AppService } from './app.service';
74+
@@filename(app.module)
75+
import { Module } from "@nestjs/common";
76+
import { SentryModule } from "@sentry/nestjs/setup";
77+
import { AppController } from "./app.controller";
78+
import { AppService } from "./app.service";
79+
6280
@Module({
63-
imports: [
64-
SentryModule.forRoot(),
65-
// other modules...
66-
],
67-
controllers: [AppController],
68-
providers: [AppService],
81+
imports: [
82+
SentryModule.forRoot(),
83+
// ...other modules
84+
],
85+
controllers: [AppController],
86+
providers: [AppService],
6987
})
7088
export class AppModule {}
7189
```
7290

91+
> info **Hint** *Running with ESM*: If you run your application with ESM, you'll need to import the Sentry Initialization file before importing any other modules. Read about [running Sentry with ESM](https://docs.sentry.io/platforms/javascript/guides/nestjs/install/esm/). If you're not sure about how you're running your application, see [Installation Methods](https://docs.sentry.io/platforms/javascript/guides/nestjs/install/) for more information.
7392
74-
#### Exception Handling
75-
76-
To ensure Sentry captures unhandled exceptions, you can add the SentryGlobalFilter to your application module:
7793

78-
```typescript
79-
import { Module } from '@nestjs/common';
80-
import { APP_FILTER } from '@nestjs/core';
81-
import { SentryGlobalFilter } from '@sentry/nestjs/setup';
82-
@Module({
83-
providers: [
84-
{
85-
provide: APP_FILTER,
86-
useClass: SentryGlobalFilter,
87-
},
88-
// other providers...
89-
],
90-
})
91-
export class AppModule {}
92-
```
94+
#### Add Readable Stack Traces to Errors
9395

96+
Depending on how you've set up your project, the stack traces in your Sentry errors probably won't look like your actual code.
9497

95-
For custom exception filters, you can use the `@SentryExceptionCaptured()` decorator:
98+
To fix this, upload your source maps to Sentry. The easiest way to do this is by using the Sentry Wizard:
9699

97-
```typescript
98-
import { Catch, ExceptionFilter } from '@nestjs/common';
99-
import { SentryExceptionCaptured } from '@sentry/nestjs';
100-
@Catch()
101-
export class AllExceptionsFilter implements ExceptionFilter {
102-
@SentryExceptionCaptured()
103-
catch(exception: unknown, host: ArgumentsHost) {
104-
// Your exception handling logic
105-
}
106-
}
100+
```bash
101+
npx @sentry/wizard@latest -i sourcemaps
107102
```
108103

109104

@@ -112,18 +107,18 @@ catch(exception: unknown, host: ArgumentsHost) {
112107
To verify your Sentry integration is working, you can add a test endpoint that throws an error:
113108

114109
```typescript
115-
import { Controller, Get } from '@nestjs/common';
116-
@Controller()
117-
export class AppController {
118-
@Get('debug-sentry')
119-
testSentry() {
120-
throw new Error('Test Sentry Integration!');
110+
@Get("/debug-sentry")
111+
getError() {
112+
throw new Error("My first Sentry error!");
121113
}
122-
}
123-
```
124114

115+
```
125116

126117
Visit `/debug-sentry` in your application, and you should see the error appear in your Sentry dashboard.
127118

128-
> info **Hint** For complete documentation about Sentry's NestJS integration, including advanced configuration options and features, visit the [official Sentry documentation](https://docs.sentry.io/platforms/javascript/guides/nestjs/).
129119

120+
### Summary
121+
122+
For complete documentation about Sentry's NestJS SDK, including advanced configuration options and features, visit the [official Sentry documentation](https://docs.sentry.io/platforms/javascript/guides/nestjs/).
123+
124+
While software bugs are Sentry's thing, we still write them. If you come across any problems while installing our SDK, please open a [GitHub Issue](https://github.com/getsentry/sentry-javascript/issues) or reach out on [Discord](https://discord.com/invite/sentry).

0 commit comments

Comments
 (0)