|
| 1 | +import type { default as SentryType, Event, Scope } from '@sentry/node'; |
| 2 | +import type { MethodFactoryLevels } from 'loglevelnext'; |
| 3 | + |
| 4 | +import { Transport, type LogMethodOptions, type SendLogOptions } from '@dot/log'; |
| 5 | + |
| 6 | +interface ReportOptions { |
| 7 | + args: any[]; |
| 8 | + methodName: string; |
| 9 | + sentryInstance: Promise<typeof SentryType | undefined>; |
| 10 | +} |
| 11 | + |
| 12 | +const { warn } = console; |
| 13 | +const appEnv: Record<string, string> = { |
| 14 | + default: 'unknown', |
| 15 | + development: 'dev', |
| 16 | + production: 'prod', |
| 17 | + test: 'test' |
| 18 | +}; |
| 19 | +const isLambda = Boolean(process.env.AWS_LAMBDA_FUNCTION_NAME); |
| 20 | + |
| 21 | +const initSentry = async () => { |
| 22 | + if (typeof process === 'undefined') return void 0; |
| 23 | + |
| 24 | + const { |
| 25 | + DISABLE_SENTRY, |
| 26 | + NODE_ENV, |
| 27 | + RALLY_LOG_LEVEL, |
| 28 | + SENTRY_DSN, |
| 29 | + SENTRY_MODULE, |
| 30 | + SENTRY_SAMPLE_RATE |
| 31 | + } = process.env as Record<string, string>; |
| 32 | + const logLevel = RALLY_LOG_LEVEL as MethodFactoryLevels; |
| 33 | + |
| 34 | + if (!SENTRY_DSN || !!DISABLE_SENTRY) { |
| 35 | + if (logLevel === 'debug' || logLevel === 'trace') { |
| 36 | + if (DISABLE_SENTRY) warn(`@rally/log → sentry: disabled by way of the DISABLE_SENTRY envar`); |
| 37 | + } |
| 38 | + return void 0; |
| 39 | + } |
| 40 | + |
| 41 | + const dsn = SENTRY_DSN; |
| 42 | + const enabled = ['prod', 'production'].includes(NODE_ENV); |
| 43 | + const environment = appEnv[NODE_ENV || 'default']; |
| 44 | + const moduleName = SENTRY_MODULE; |
| 45 | + const sampleRate = parseFloat(SENTRY_SAMPLE_RATE || '1'); |
| 46 | + |
| 47 | + const { default: Sentry } = await import('@sentry/node'); |
| 48 | + const beforeSend = ((event: Event) => { |
| 49 | + return { ...event, tags: { module: moduleName, ...event.tags } }; |
| 50 | + }) as any; |
| 51 | + const initMethod = isLambda ? Sentry.initWithoutDefaultIntegrations : Sentry.init; |
| 52 | + |
| 53 | + initMethod({ |
| 54 | + attachStacktrace: true, |
| 55 | + beforeSend, |
| 56 | + dsn, |
| 57 | + enabled, |
| 58 | + environment, |
| 59 | + sampleRate |
| 60 | + }); |
| 61 | + |
| 62 | + return Sentry; |
| 63 | +}; |
| 64 | + |
| 65 | +const report = async (options: ReportOptions) => { |
| 66 | + const { args, methodName, sentryInstance } = options; |
| 67 | + |
| 68 | + const tags = { rallyEnv: process.env.DEPLOY_ENV }; |
| 69 | + const [message, ...rest] = args; |
| 70 | + const [maybeOptions] = rest.slice(-1) as LogMethodOptions[]; |
| 71 | + const event: Event = { extra: { args: rest }, message, tags }; |
| 72 | + const sentry = await sentryInstance; |
| 73 | + |
| 74 | + if (!sentry) return; |
| 75 | + |
| 76 | + if (maybeOptions && maybeOptions.data) { |
| 77 | + event.tags = { ...maybeOptions.data }; |
| 78 | + rest.pop(); |
| 79 | + } |
| 80 | + |
| 81 | + if (methodName === 'warn') { |
| 82 | + sentry.captureEvent({ level: 'warning', ...event }); |
| 83 | + } else if (methodName === 'error') { |
| 84 | + sentry.withScope((scope: Scope) => { |
| 85 | + scope.setExtras(event as any); |
| 86 | + scope.setLevel('error'); |
| 87 | + scope.setTags({ message, ...event.tags }); |
| 88 | + |
| 89 | + // Note: The typing in @sentry/types is incorrect: https://github.com/getsentry/sentry-javascript/issues/5764 |
| 90 | + // We need to assert that this is a truthy value |
| 91 | + if (message) sentry.captureException(message); |
| 92 | + else warn('@sa/log → sentry: message was falsy:', event); |
| 93 | + }); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +export class SentryTransport extends Transport { |
| 98 | + private sentryInstance: Promise<typeof SentryType | undefined> | undefined = void 0; |
| 99 | + |
| 100 | + constructor() { |
| 101 | + super(); |
| 102 | + |
| 103 | + this.sentryInstance = initSentry(); |
| 104 | + } |
| 105 | + |
| 106 | + send({ args, methodName }: SendLogOptions) { |
| 107 | + if (!this.sentryInstance) return; |
| 108 | + report({ args, methodName, sentryInstance: this.sentryInstance }); |
| 109 | + } |
| 110 | +} |
0 commit comments