-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
-
Problem Statement
Is it possible to document whether it is feasible to initialize Sentry with dynamic configuration ? My configuration depends on environment variable and dynamically fetched configurations from remote sources.
I understand the instrument.ts file is meant to be called as early as possible to log application initialization errors, but I have healthchecks on my container that will tell me if the app started or not.
Solution Brainstorm
I would expect something similar to this
@Global() @Module({ imports: [EnvModule], providers: [ { provide: SENTRY_CLIENT, useFactory: (envService: EnvService, supabaseClient: SupabaseClient): SentryClient => { return Sentry.init({ // usage of envService and supabase client }} }, inject: [EnvService, SupabaseClient], }, ], exports: [SENTRY_CLIENT], }) export class SentryModule {}
Beta Was this translation helpful? Give feedback.
All reactions
Thanks @Lms24 for your suggestions, I managed to run Sentry with Nest with the late init method as follows:
- Update
package.jsonscripts to require the@sentry/node/preload.js
- "start": "nest start", - "start:dev": "nest start --watch", - "start:debug": "nest start --debug --watch", - "start:prod": "node dist/main", + "start": "nest start -e \"node --import @sentry/node/preload\"", + "start:dev": "nest start --watch -e \"node --import @sentry/node/preload\"", + "start:debug": "nest start --debug --watch -e \"node --import @sentry/node/preload\"", + "start:prod": "node --import @sentry/node/preload dist/main",
- Create my own
SentryModule
import { Global, Module } f...
Replies: 2 comments 1 reply
-
Hey @noook, I converted this to a discussion since I don't think this is a bug or a feature request to track as an issue.
You can probably use the late initialization installation method. Gotta check though if it is re-exported from @sentry/nestjs. Otherwise you might need to use @sentry/node for this step but call Sentry.init from @sentry/nestjs.
Beta Was this translation helpful? Give feedback.
All reactions
-
Worth noting that we haven't tried this out for NestJS yet. So any report about results would be appreciated :)
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks @Lms24 for your suggestions, I managed to run Sentry with Nest with the late init method as follows:
- Update
package.jsonscripts to require the@sentry/node/preload.js
- "start": "nest start", - "start:dev": "nest start --watch", - "start:debug": "nest start --debug --watch", - "start:prod": "node dist/main", + "start": "nest start -e \"node --import @sentry/node/preload\"", + "start:dev": "nest start --watch -e \"node --import @sentry/node/preload\"", + "start:debug": "nest start --debug --watch -e \"node --import @sentry/node/preload\"", + "start:prod": "node --import @sentry/node/preload dist/main",
- Create my own
SentryModule
import { Global, Module } from '@nestjs/common'; import { SentryGlobalFilter, SentryModule as SentryNestModule } from '@sentry/nestjs/setup'; import { SentryService } from './sentry.service'; import { APP_FILTER } from '@nestjs/core'; import { SentryExceptionFilter } from '../filters/http-exception.filter'; @Global() @Module({ imports: [SentryNestModule.forRoot()], providers: [ SentryService, { provide: APP_FILTER, useClass: SentryGlobalFilter, }, { provide: APP_FILTER, useClass: SentryExceptionFilter, }, ], exports: [SentryService], }) export class SentryModule {}
- Create my
SentryServiceto configure it:
import { Injectable, OnModuleInit } from '@nestjs/common'; import { EnvService } from '../env/env.service'; import * as Sentry from '@sentry/nestjs'; import { nodeProfilingIntegration } from '@sentry/profiling-node'; @Injectable() export class SentryService implements OnModuleInit { constructor(private envService: EnvService) {} onModuleInit() { const environment = this.envService.get('NODE_ENV'); const dsn = this.envService.get('SENTRY_DSN'); Sentry.init({ dsn, environment, tracesSampleRate: 1.0, integrations: [nodeProfilingIntegration()], }); } captureException(error: any) { Sentry.captureException(error); } captureMessage(message: string) { Sentry.captureMessage(message); } }
- Register my module (and not
@sentry/nestjs) inapp.module.ts
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { EnvModule } from './core/env/env.module'; import { envSchema } from './core/env/env'; import { SentryModule } from './core/sentry/sentry.module'; @Module({ imports: [ SentryModule, ConfigModule.forRoot({ validate: (env) => envSchema.parse(env), isGlobal: true, }), EnvModule, ], controllers: [], providers: [], }) export class AppModule {}
Beta Was this translation helpful? Give feedback.