Skip to content

Commit ec66fef

Browse files
committed
Don't init Sentry if SENTRY_DSN isn't defined
1 parent 555b44b commit ec66fef

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/utils/monitoring/sentry.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
import { NowRequest } from '@now/node/dist';
22
import * as Sentry from '@sentry/node';
3-
import get from 'lodash.get';
43
import { isBrowser } from '@unly/utils';
4+
import get from 'lodash.get';
55

6-
/**
7-
* Initialize Sentry and export it.
8-
*
9-
* Helper to avoid duplicating the init() call in every /pages/api file.
10-
* Also used in pages/_app for the client side, which automatically applies it for all frontend pages.
11-
*/
12-
Sentry.init({
13-
dsn: process.env.SENTRY_DSN,
14-
enabled: process.env.NODE_ENV !== 'test',
15-
environment: process.env.APP_STAGE,
16-
release: process.env.APP_VERSION_RELEASE,
17-
});
6+
// 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)
7+
if (process.env.SENTRY_DSN) {
8+
/**
9+
* Initialize Sentry and export it.
10+
*
11+
* Helper to avoid duplicating the init() call in every /pages/api file.
12+
* Also used in pages/_app for the client side, which automatically applies it for all frontend pages.
13+
*/
14+
Sentry.init({
15+
dsn: process.env.SENTRY_DSN,
16+
enabled: process.env.NODE_ENV !== 'test',
17+
environment: process.env.APP_STAGE,
18+
release: process.env.APP_VERSION_RELEASE,
19+
});
1820

19-
if (!process.env.SENTRY_DSN && process.env.NODE_ENV !== 'test') {
20-
// eslint-disable-next-line no-console
21-
console.error('Sentry DSN not defined');
22-
}
21+
if (!process.env.SENTRY_DSN && process.env.NODE_ENV !== 'test') {
22+
// eslint-disable-next-line no-console
23+
console.error('Sentry DSN not defined');
24+
}
2325

24-
// Scope configured by default, subsequent calls to "configureScope" will add additional data
25-
Sentry.configureScope((scope) => { // See https://www.npmjs.com/package/@sentry/node
26-
scope.setTag('appVersion', process.env.APP_VERSION);
27-
scope.setTag('nodejs', process.version);
28-
scope.setTag('nodejsAWS', process.env.AWS_EXECUTION_ENV || null); // Optional - Available on production environment only
29-
scope.setTag('memory', process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || null); // Optional - Available on production environment only
30-
scope.setTag('runtimeEngine', isBrowser() ? 'browser' : 'server');
31-
scope.setTag('buildTime', process.env.BUILD_TIME);
32-
});
26+
// Scope configured by default, subsequent calls to "configureScope" will add additional data
27+
Sentry.configureScope((scope) => { // See https://www.npmjs.com/package/@sentry/node
28+
scope.setTag('appVersion', process.env.APP_VERSION);
29+
scope.setTag('nodejs', process.version);
30+
scope.setTag('nodejsAWS', process.env.AWS_EXECUTION_ENV || null); // Optional - Available on production environment only
31+
scope.setTag('memory', process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || null); // Optional - Available on production environment only
32+
scope.setTag('runtimeEngine', isBrowser() ? 'browser' : 'server');
33+
scope.setTag('buildTime', process.env.BUILD_TIME);
34+
});
35+
}
3336

3437
/**
3538
* Configure the Sentry scope by extracting useful tags and context from the given request.

0 commit comments

Comments
 (0)