From 5ce38b8b0fc952e4160074e03b1a2aae1f5325b1 Mon Sep 17 00:00:00 2001 From: Tyler Matteo Date: Wed, 30 Oct 2024 13:35:48 -0400 Subject: [PATCH] Update Sentry instrument.js file to use environment variables and disable when environment is not set --- server/src/sentry/instrument.ts | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/server/src/sentry/instrument.ts b/server/src/sentry/instrument.ts index 94b72fed..dc5d9195 100644 --- a/server/src/sentry/instrument.ts +++ b/server/src/sentry/instrument.ts @@ -1,10 +1,34 @@ import * as Sentry from "@sentry/nestjs"; import { nodeProfilingIntegration } from '@sentry/profiling-node'; +import * as dotenv from "dotenv"; +import * as fs from "fs"; + +// Load Sentry environment variables from process.env or development.env file +let envConfig: { [key: string]: string } = {}; +if ( + process.env.NODE_ENV === "production" || + process.env.NODE_ENV === "test" +) { + envConfig = process.env; +} else { + try { + const filePath = "development.env"; + const envValuesFromFile: { [key: string]: string } = {} = dotenv.parse(fs.readFileSync(filePath)) || {}; + + envConfig = { ...process.env, ...envValuesFromFile }; + } catch (e) { + console.log(`Something went wrong loading the environment file: ${e}`); + + // fallback to whatever the environment is + envConfig = process.env; + } +} // Ensure to call this before importing any other modules! Sentry.init({ - dsn: "https://cb886cd8649348e3b402c8fd36ab30bc@o1081909.ingest.us.sentry.io/6410746", - environment: process.env.NODE_ENV === "production" ? "production" : "staging", + dsn: process.env.SENTRY_DSN, + environment: process.env.SENTRY_ENVIRONMENT === undefined ? "local" : process.env.SENTRY_ENVIRONMENT, + enabled: process.env.SENTRY_ENVIRONMENT === undefined ? false : true, integrations: [ // Add our Profiling integration nodeProfilingIntegration(),