|
| 1 | +import * as Sentry from '@sentry/react' |
| 2 | +import { redact } from '~/lib/security/token-redaction' |
| 3 | + |
| 4 | +const dsn = import.meta.env.VITE_SENTRY_DSN as string | undefined |
| 5 | + |
| 6 | +if (dsn) { |
| 7 | + Sentry.init({ |
| 8 | + dsn, |
| 9 | + environment: import.meta.env.MODE, |
| 10 | + debug: import.meta.env.DEV, |
| 11 | + // v8-style integrations |
| 12 | + integrations: [ |
| 13 | + Sentry.browserTracingIntegration(), |
| 14 | + Sentry.captureConsoleIntegration({ levels: ['error', 'warn'] }), |
| 15 | + // Added but sampling below keeps Replay off unless errors occur |
| 16 | + Sentry.replayIntegration(), |
| 17 | + ], |
| 18 | + // Keep light by default; override via VITE_* env if needed |
| 19 | + tracesSampleRate: Number(import.meta.env.VITE_SENTRY_TRACES_SAMPLE_RATE ?? '0.1'), |
| 20 | + replaysSessionSampleRate: Number( |
| 21 | + import.meta.env.VITE_SENTRY_REPLAYS_SESSION_SAMPLE_RATE ?? '0.0', |
| 22 | + ), |
| 23 | + replaysOnErrorSampleRate: Number( |
| 24 | + import.meta.env.VITE_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE ?? '1.0', |
| 25 | + ), |
| 26 | + tracePropagationTargets: ((): (string | RegExp)[] => { |
| 27 | + const targets: (string | RegExp)[] = [ |
| 28 | + /^https?:\/\/localhost(?::\d+)?/, |
| 29 | + /^https?:\/\/(.*\.)?ex0\.dev/i, |
| 30 | + ] |
| 31 | + |
| 32 | + const baseUrl = import.meta.env.VITE_BASE_URL |
| 33 | + if (baseUrl) targets.push(baseUrl) |
| 34 | + if (typeof window !== 'undefined') targets.push(window.location.origin) |
| 35 | + |
| 36 | + return targets |
| 37 | + })(), |
| 38 | + beforeBreadcrumb(breadcrumb) { |
| 39 | + try { |
| 40 | + if (breadcrumb?.message) breadcrumb.message = redact(String(breadcrumb.message)) |
| 41 | + if (breadcrumb?.data) { |
| 42 | + breadcrumb.data = JSON.parse( |
| 43 | + JSON.stringify(breadcrumb.data, (k, v) => |
| 44 | + typeof v === 'string' ? redact(v) : v, |
| 45 | + ), |
| 46 | + ) |
| 47 | + } |
| 48 | + } catch { |
| 49 | + // no-op |
| 50 | + } |
| 51 | + return breadcrumb |
| 52 | + }, |
| 53 | + beforeSend(event) { |
| 54 | + try { |
| 55 | + if (event.request?.url) event.request.url = redact(event.request.url) |
| 56 | + if (event.request?.headers) { |
| 57 | + for (const [k, v] of Object.entries(event.request.headers)) { |
| 58 | + if (typeof v === 'string') (event.request.headers as any)[k] = redact(v) |
| 59 | + } |
| 60 | + } |
| 61 | + // never send cookies from the browser |
| 62 | + if (event.request) (event.request as any).cookies = undefined |
| 63 | + |
| 64 | + if (event.exception?.values) { |
| 65 | + for (const ex of event.exception.values) { |
| 66 | + if (ex.value) ex.value = redact(ex.value) |
| 67 | + } |
| 68 | + } |
| 69 | + } catch { |
| 70 | + // no-op |
| 71 | + } |
| 72 | + return event |
| 73 | + }, |
| 74 | + }) |
| 75 | +} else if (import.meta.env.DEV) { |
| 76 | + console.warn('[sentry] VITE_SENTRY_DSN missing; client telemetry disabled') |
| 77 | +} |
0 commit comments