Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support async configuration for SentryModule #16574

Answered by noook
noook asked this question in Q&A
Discussion options

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 {}
You must be logged in to vote

Thanks @Lms24 for your suggestions, I managed to run Sentry with Nest with the late init method as follows:

  1. Update package.json scripts 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",
  1. Create my own SentryModule
import { Global, Module } f...

Replies: 2 comments 1 reply

Comment options

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.

You must be logged in to vote
1 reply
Comment options

Worth noting that we haven't tried this out for NestJS yet. So any report about results would be appreciated :)

Comment options

Thanks @Lms24 for your suggestions, I managed to run Sentry with Nest with the late init method as follows:

  1. Update package.json scripts 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",
  1. 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 {}
  1. Create my SentryService to 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);
 }
}
  1. Register my module (and not @sentry/nestjs) in app.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 {}
You must be logged in to vote
0 replies
Answer selected by Lms24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants
Converted from issue

This discussion was converted from issue #16573 on June 12, 2025 18:00.

AltStyle によって変換されたページ (->オリジナル) /