import { filterAndRedactEvent, SILENCED_ERROR_PATTERNS, } from '@baserow/modules/core/utils/sentryErrors' import { makeFakeTransport } from '@baserow/modules/core/utils/sentryFakeTransport' import { dropRouteParams } from '@baserow/modules/core/utils/sentryTransactions' import { redactPrivateLinks } from '@baserow/modules/core/utils/templatePrivateLink' const redactTransaction = (event) => redactPrivateLinks(dropRouteParams(event)) const withAppHook = (prepare, appHook, redact) => { if (!appHook) { return prepare } return async (event, hint) => { const prepared = prepare(event, hint) if (prepared === null) { return null } const result = await appHook(prepared, hint) // An app hook can add fresh URLs or route parameters to an already redacted event. return result === null ? null : redact(result) } } export const buildSentryConfig = ({ publicConfig, tracingIntegration, userConfig, isDev, }) => { const app = userConfig ?? {} const fakeDsn = publicConfig.sentryDsn === 'fake' const fakeTransport = isDev && fakeDsn const defaults = { dsn: fakeDsn ? 'https://fake@localhost/1' : publicConfig.sentryDsn, release: `baserow-web-frontend@${publicConfig.version}`, environment: publicConfig.sentryEnvironment || 'production', integrations: [tracingIntegration], ignoreErrors: SILENCED_ERROR_PATTERNS, tracesSampleRate: parseFloat(publicConfig.sentryTracesSampleRate) || 0, replaysSessionSampleRate: 0, replaysOnErrorSampleRate: parseFloat(publicConfig.sentryReplaysOnErrorSampleRate) || 0, } const beforeSend = (event, hint) => { const kept = filterAndRedactEvent(event, hint) if (fakeTransport && kept) { console.error('[Sentry captured error]', `${hint?.originalException}`) } return kept } const config = { ...defaults, ...app, beforeSend: withAppHook(beforeSend, app.beforeSend, redactPrivateLinks), beforeSendTransaction: withAppHook( redactTransaction, app.beforeSendTransaction, redactTransaction ), ...(fakeTransport ? { transport: makeFakeTransport } : {}), } for (const key of ['integrations', 'ignoreErrors', 'denyUrls']) { config[key] = [...(defaults[key] ?? []), ...(app[key] ?? [])] } return config }