Skip to content

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.

  1. Terminal window
    cd apps/api
    pnpm add @sentry/nestjs

  2. 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>

  3. 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
  4. Create an instrument.ts file at the root of the API project

    Section titled “Create an instrument.ts file at the root of the API project”
    src/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,
    })

  5. 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 {}