Skip to content

Commit

Permalink
Don't init Sentry if SENTRY_DSN isn't defined
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadorequest committed May 28, 2020
1 parent 555b44b commit ec66fef
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/utils/monitoring/sentry.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit ec66fef

Please sign in to comment.