Skip to content

Commit

Permalink
feat: Add more logging to Sentry init
Browse files Browse the repository at this point in the history
Added input range validation for Sentry env vars
  • Loading branch information
YuvalBubnovsky committed Sep 29, 2024
1 parent 4aad8a2 commit b598651
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
39 changes: 33 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,12 @@ Use the .optional() property if you are okay with a variable being omitted from
*/

const validateSampleRate = (val: string | undefined) => {
if (val === undefined) return true
const num = parseFloat(val)
return !isNaN(num) && num >= 0 && num <= 1
}

const e2eEnvSchema = z.object({
NODE_ENV: z.literal('e2e'),
DATABASE_URL: z.string(),
Expand Down Expand Up @@ -47,11 +53,23 @@ 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: z.string().optional().refine(validateSampleRate, {
message:
'SENTRY_TRACES_SAMPLE_RATE must be a string representing a number between 0 and 1'
}),
SENTRY_PROFILES_SAMPLE_RATE: z
.string()
.optional()
.refine(validateSampleRate, {
message:
'SENTRY_PROFILES_SAMPLE_RATE must be a string representing a number between 0 and 1'
}),
SENTRY_ENV: z.string().optional(),

SMTP_HOST: z.string(),
Expand Down Expand Up @@ -103,11 +121,20 @@ 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: z.string().min(1).refine(validateSampleRate, {
message:
'SENTRY_TRACES_SAMPLE_RATE must be a string representing a number between 0 and 1'
}),
SENTRY_PROFILES_SAMPLE_RATE: z.string().min(1).refine(validateSampleRate, {
message:
'SENTRY_PROFILES_SAMPLE_RATE must be a string representing a number between 0 and 1'
}),
SENTRY_ENV: z.string().min(1),

SMTP_HOST: z.string().min(1),
Expand Down
31 changes: 23 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,35 @@ async function initializeSentry() {
logger.warn(
'Missing one or more Sentry environment variables. Skipping initialization...'
)
} else {
return
}

try {
const tracesSampleRate =
parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE) || 1.0
const profilesSampleRate =
parseFloat(process.env.SENTRY_PROFILES_SAMPLE_RATE) || 1.0

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,
profilesSampleRate,
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: ${tracesSampleRate}`)
logger.log(`Sentry Profiles Sample Rate: ${profilesSampleRate}`)
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 b598651

Please sign in to comment.