Skip to content

Commit

Permalink
chore: Add more logging to Sentry init (keyshade-xyz#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuvalBubnovsky authored and Kiranchaudhary537 committed Oct 13, 2024
1 parent b3cf943 commit b2d4a37
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
28 changes: 22 additions & 6 deletions apps/api/src/common/env/env.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ Use the .optional() property if you are okay with a variable being omitted from
*/

const sampleRateSchema = (name: string) =>
z
.string()
.optional()
.transform((val) => (val === undefined ? undefined : parseFloat(val)))
.pipe(z.number().min(0).max(1).optional())
.refine((val) => val === undefined || !isNaN(val), {
message: `${name} must be a number between 0 and 1`
})

const e2eEnvSchema = z.object({
NODE_ENV: z.literal('e2e'),
DATABASE_URL: z.string(),
Expand Down Expand Up @@ -47,11 +57,14 @@ const devSchema = z.object({
GITLAB_CLIENT_SECRET: z.string().optional(),
GITLAB_CALLBACK_URL: z.string().optional(),

SENTRY_DSN: z.string().optional(),
SENTRY_DSN: z
.string()
.url({ message: 'SENTRY_DSN must be a valid URL' })
.optional(),
SENTRY_ORG: z.string().optional(),
SENTRY_PROJECT: z.string().optional(),
SENTRY_TRACES_SAMPLE_RATE: z.string().optional(),
SENTRY_PROFILES_SAMPLE_RATE: z.string().optional(),
SENTRY_TRACES_SAMPLE_RATE: sampleRateSchema('SENTRY_TRACES_SAMPLE_RATE'),
SENTRY_PROFILES_SAMPLE_RATE: sampleRateSchema('SENTRY_PROFILES_SAMPLE_RATE'),
SENTRY_ENV: z.string().optional(),

SMTP_HOST: z.string(),
Expand Down Expand Up @@ -103,11 +116,14 @@ const prodSchema = z.object({
GITLAB_CLIENT_SECRET: z.string().min(1),
GITLAB_CALLBACK_URL: z.string().min(1),

SENTRY_DSN: z.string().min(1),
SENTRY_DSN: z
.string()
.url({ message: 'SENTRY_DSN must be a valid URL' })
.min(1),
SENTRY_ORG: z.string().min(1),
SENTRY_PROJECT: z.string().min(1),
SENTRY_TRACES_SAMPLE_RATE: z.string().min(1),
SENTRY_PROFILES_SAMPLE_RATE: z.string().min(1),
SENTRY_TRACES_SAMPLE_RATE: sampleRateSchema('SENTRY_TRACES_SAMPLE_RATE'),
SENTRY_PROFILES_SAMPLE_RATE: sampleRateSchema('SENTRY_PROFILES_SAMPLE_RATE'),
SENTRY_ENV: z.string().min(1),

SMTP_HOST: z.string().min(1),
Expand Down
30 changes: 22 additions & 8 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NestFactory } from '@nestjs/core'
import { AppModule } from './app/app.module'
import { QueryTransformPipe } from './common/pipes/query.transform.pipe'
import * as Sentry from '@sentry/node'
import { ProfilingIntegration } from '@sentry/profiling-node'
import { nodeProfilingIntegration } from '@sentry/profiling-node'
import { RedisIoAdapter } from './socket/redis.adapter'
import { CustomLoggerService } from './common/logger.service'
import cookieParser from 'cookie-parser'
Expand All @@ -28,20 +28,34 @@ async function initializeSentry() {
logger.warn(
'Missing one or more Sentry environment variables. Skipping initialization...'
)
} else {
return
}

try {
Sentry.init({
dsn: process.env.SENTRY_DSN,
enabled: sentryEnv !== 'test' && sentryEnv !== 'e2e',
environment: sentryEnv,
tracesSampleRate:
parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE) || 1.0,
profilesSampleRate:
parseFloat(process.env.SENTRY_PROFILES_SAMPLE_RATE) || 1.0,
integrations: [new ProfilingIntegration()],
tracesSampleRate: process.env.SENTRY_TRACES_SAMPLE_RATE,
profilesSampleRate: process.env.SENTRY_PROFILES_SAMPLE_RATE,
integrations: [nodeProfilingIntegration()],
debug: sentryEnv.startsWith('dev')
})

logger.log('Sentry initialized')
logger.log('Sentry initialized with the following configuration:')
logger.log(`Sentry Organization: ${process.env.SENTRY_ORG}`)
logger.log(`Sentry Project: ${process.env.SENTRY_PROJECT}`)
logger.log(`Sentry Environment: ${sentryEnv}`)
logger.log(
`Sentry Traces Sample Rate: ${process.env.SENTRY_TRACES_SAMPLE_RATE}`
)
logger.log(
`Sentry Profiles Sample Rate: ${process.env.SENTRY_PROFILES_SAMPLE_RATE}`
)
logger.log(`Sentry Debug Mode: ${sentryEnv.startsWith('dev')}`)
} catch (error) {
logger.error(`Failed to initialize Sentry: ${error.message}`)
Sentry.captureException(error)
}
}

Expand Down

0 comments on commit b2d4a37

Please sign in to comment.