|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import { isBrowser } from '@unly/utils'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Initialize Sentry and export it. |
| 6 | + * |
| 7 | + * Helper to avoid duplicating the init() call in every /pages/api file. |
| 8 | + * Also used in pages/_app for the client side, which automatically applies it for all frontend pages. |
| 9 | + * |
| 10 | + * Doesn't initialise Sentry if SENTRY_DSN isn't defined |
| 11 | + * |
| 12 | + * @see https://www.npmjs.com/package/@sentry/node |
| 13 | + */ |
| 14 | +if (process.env.SENTRY_DSN) { |
| 15 | + Sentry.init({ |
| 16 | + dsn: process.env.SENTRY_DSN, |
| 17 | + enabled: process.env.NODE_ENV !== 'test', |
| 18 | + environment: process.env.NEXT_PUBLIC_APP_STAGE, |
| 19 | + release: process.env.NEXT_PUBLIC_APP_VERSION_RELEASE, |
| 20 | + }); |
| 21 | + |
| 22 | + if (!process.env.SENTRY_DSN && process.env.NODE_ENV !== 'test') { |
| 23 | + // eslint-disable-next-line no-console |
| 24 | + console.error('Sentry DSN not defined'); |
| 25 | + } |
| 26 | + |
| 27 | + // Scope configured by default, subsequent calls to "configureScope" will add additional data |
| 28 | + Sentry.configureScope((scope) => { // See https://www.npmjs.com/package/@sentry/node |
| 29 | + scope.setTag('customerRef', process.env.NEXT_PUBLIC_CUSTOMER_REF); |
| 30 | + scope.setTag('appStage', process.env.NEXT_PUBLIC_APP_STAGE); |
| 31 | + scope.setTag('appName', process.env.NEXT_PUBLIC_APP_NAME); |
| 32 | + scope.setTag('appBaseUrl', process.env.NEXT_PUBLIC_APP_BASE_URL); |
| 33 | + scope.setTag('appVersion', process.env.NEXT_PUBLIC_APP_VERSION); |
| 34 | + scope.setTag('appNameVersion', process.env.NEXT_PUBLIC_APP_NAME_VERSION); |
| 35 | + scope.setTag('appBuildTime', process.env.NEXT_PUBLIC_APP_BUILD_TIME); |
| 36 | + scope.setTag('buildTimeISO', (new Date(process.env.NEXT_PUBLIC_APP_BUILD_TIME || null)).toISOString()); |
| 37 | + scope.setTag('appBuildId', process.env.NEXT_PUBLIC_APP_BUILD_ID); |
| 38 | + scope.setTag('nodejs', process.version); |
| 39 | + scope.setTag('nodejsAWS', process.env.AWS_EXECUTION_ENV || null); // Optional - Available on production environment only |
| 40 | + scope.setTag('memory', process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || null); // Optional - Available on production environment only |
| 41 | + scope.setTag('runtimeEngine', isBrowser() ? 'browser' : 'server'); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +export default Sentry; |
0 commit comments