From ec66fefe6aa64ffeac991f80fbce3fda51d8fb02 Mon Sep 17 00:00:00 2001
From: Dhenain Ambroise <ambroise.dhenain@gmail.com>
Date: Thu, 28 May 2020 19:31:39 +0200
Subject: [PATCH] Don't init Sentry if SENTRY_DSN isn't defined

---
 src/utils/monitoring/sentry.ts | 55 ++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 26 deletions(-)

diff --git a/src/utils/monitoring/sentry.ts b/src/utils/monitoring/sentry.ts
index 343ff9272..ee6b5bb2c 100644
--- a/src/utils/monitoring/sentry.ts
+++ b/src/utils/monitoring/sentry.ts
@@ -1,35 +1,38 @@
 import { NowRequest } from '@now/node/dist';
 import * as Sentry from '@sentry/node';
-import get from 'lodash.get';
 import { isBrowser } from '@unly/utils';
+import get from 'lodash.get';
 
-/**
- * Initialize Sentry and export it.
- *
- * Helper to avoid duplicating the init() call in every /pages/api file.
- * Also used in pages/_app for the client side, which automatically applies it for all frontend pages.
- */
-Sentry.init({
-  dsn: process.env.SENTRY_DSN,
-  enabled: process.env.NODE_ENV !== 'test',
-  environment: process.env.APP_STAGE,
-  release: process.env.APP_VERSION_RELEASE,
-});
+// Don't initialise Sentry if SENTRY_DSN isn't defined (won't crash the app, usage of the Sentry lib is resilient to this and doesn't cause any issue)
+if (process.env.SENTRY_DSN) {
+  /**
+   * Initialize Sentry and export it.
+   *
+   * Helper to avoid duplicating the init() call in every /pages/api file.
+   * Also used in pages/_app for the client side, which automatically applies it for all frontend pages.
+   */
+  Sentry.init({
+    dsn: process.env.SENTRY_DSN,
+    enabled: process.env.NODE_ENV !== 'test',
+    environment: process.env.APP_STAGE,
+    release: process.env.APP_VERSION_RELEASE,
+  });
 
-if (!process.env.SENTRY_DSN && process.env.NODE_ENV !== 'test') {
-  // eslint-disable-next-line no-console
-  console.error('Sentry DSN not defined');
-}
+  if (!process.env.SENTRY_DSN && process.env.NODE_ENV !== 'test') {
+    // eslint-disable-next-line no-console
+    console.error('Sentry DSN not defined');
+  }
 
-// Scope configured by default, subsequent calls to "configureScope" will add additional data
-Sentry.configureScope((scope) => { // See https://www.npmjs.com/package/@sentry/node
-  scope.setTag('appVersion', process.env.APP_VERSION);
-  scope.setTag('nodejs', process.version);
-  scope.setTag('nodejsAWS', process.env.AWS_EXECUTION_ENV || null); // Optional - Available on production environment only
-  scope.setTag('memory', process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || null); // Optional - Available on production environment only
-  scope.setTag('runtimeEngine', isBrowser() ? 'browser' : 'server');
-  scope.setTag('buildTime', process.env.BUILD_TIME);
-});
+  // Scope configured by default, subsequent calls to "configureScope" will add additional data
+  Sentry.configureScope((scope) => { // See https://www.npmjs.com/package/@sentry/node
+    scope.setTag('appVersion', process.env.APP_VERSION);
+    scope.setTag('nodejs', process.version);
+    scope.setTag('nodejsAWS', process.env.AWS_EXECUTION_ENV || null); // Optional - Available on production environment only
+    scope.setTag('memory', process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || null); // Optional - Available on production environment only
+    scope.setTag('runtimeEngine', isBrowser() ? 'browser' : 'server');
+    scope.setTag('buildTime', process.env.BUILD_TIME);
+  });
+}
 
 /**
  * Configure the Sentry scope by extracting useful tags and context from the given request.