Adding Sentry feature
You should add Sentry — or an open source alternative — to monitor errors and performance in your project.
At Lonestone, we use Bugsink which is a self-hosted Sentry alternative, compatible with the Sentry SDK.
-
Install the Sentry SDK in the API project
Section titled “Install the Sentry SDK in the API project”Terminal window cd apps/apipnpm add @sentry/nestjs
-
Add environment variables in the API project (both .env and .env.example)
Section titled “Add environment variables in the API project (both .env and .env.example)”SENTRY_DSN=https://<your-dsn>@sentry.io/<your-project>
-
Load the env in your env.config.ts file. Follow the same pattern as the other env.
Section titled “Load the env in your env.config.ts file. Follow the same pattern as the other env.”src/config/env.config.ts export const configValidationSchema = z.object({// ... other env variables ...SENTRY_DSN: z.string(),})// .....export const config = {// ... other config variables ...sentry: {dsn: configParsed.data.SENTRY_DSN,},} as const -
Create an
Section titled “Create an instrument.ts file at the root of the API project”instrument.tsfile at the root of the API projectsrc/instrument.ts import * as Sentry from '@sentry/nestjs'import { config } from './config/env.config'// Ensure to call this before requiring any other modules!Sentry.init({dsn: config.sentry.dsn,})
-
Load the Sentry module
Section titled “Load the Sentry module”src/app.module.ts /* eslint-disable perfectionist/sort-imports */// Import this first to initialize Sentry!import './instrument'// .. other imports //import { SentryGlobalFilter, SentryModule } from '@sentry/nestjs/setup'@Module({imports: [SentryModule.forRoot(), // add this line// .. other imports //],controllers: [],providers: [// Add this provider to catch all errors{provide: APP_FILTER,useClass: SentryGlobalFilter,},// .. other providers //],})export class AppModule {}