|
| 1 | +import { env } from '$env/dynamic/public'; |
| 2 | +import * as Sentry from '@sentry/sveltekit'; |
| 3 | +import { isCloud } from './system'; |
| 4 | + |
| 5 | +const timestamp = Date.now(); |
| 6 | + |
| 7 | +export function getSessionId(userId: string) { |
| 8 | + return `${userId}-${timestamp}`; |
| 9 | +} |
| 10 | + |
| 11 | +export function identify(userId: string) { |
| 12 | + if (isCloud) { |
| 13 | + Sentry.setContext('Imagine Request Context', { |
| 14 | + session_id: getSessionId(userId) |
| 15 | + }); |
| 16 | + Sentry.setUser({ |
| 17 | + id: userId |
| 18 | + }); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export function setupSentry({ withSessionReplay }: { withSessionReplay: boolean }) { |
| 23 | + const dsn = env.PUBLIC_SENTRY_DSN; |
| 24 | + const environment = env.PUBLIC_SENTRY_ENVIRONMENT; |
| 25 | + |
| 26 | + if (!dsn) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (dsn && !environment) { |
| 31 | + throw new Error( |
| 32 | + "PUBLIC_SENTRY_ENVIRONMENT is required when PUBLIC_SENTRY_DSN is set. For local development, set it to your name, e.g. 'torsten'." |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const variables = { |
| 37 | + enabled: true, |
| 38 | + includePII: env.PUBLIC_SENTRY_INCLUDE_PII === 'true', |
| 39 | + tracesSampleRate: parseFloat(env.PUBLIC_SENTRY_TRACES_SAMPLE_RATE), |
| 40 | + replaysSessionSampleRate: parseFloat(env.PUBLIC_SENTRY_REPLAY_SAMPLE_RATE), |
| 41 | + debug: env.PUBLIC_SENTRY_DEBUG === 'true' |
| 42 | + }; |
| 43 | + |
| 44 | + const integrations = []; |
| 45 | + if (withSessionReplay) { |
| 46 | + integrations.push( |
| 47 | + Sentry.replayIntegration({ |
| 48 | + maskAllText: variables.includePII ? false : true, |
| 49 | + maskAllInputs: variables.includePII ? false : true |
| 50 | + }) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + Sentry.init({ |
| 55 | + enabled: variables.enabled, |
| 56 | + dsn, |
| 57 | + tracesSampleRate: variables.tracesSampleRate, |
| 58 | + replaysSessionSampleRate: variables.replaysSessionSampleRate, |
| 59 | + replaysOnErrorSampleRate: 1, |
| 60 | + integrations, |
| 61 | + debug: variables.debug, |
| 62 | + sendDefaultPii: true |
| 63 | + }); |
| 64 | +} |
0 commit comments