From 39252bd4550ddc7d8f3b4b92dfe563d0d79d36f8 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 24 Apr 2023 13:32:55 +0200 Subject: [PATCH] sentry: identify frontend --- src/hooks.client.ts | 3 +++ src/lib/sentry.ts | 21 +++++++++++++++++++++ src/routes/+layout.svelte | 3 ++- src/routes/+layout.ts | 4 +++- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/lib/sentry.ts diff --git a/src/hooks.client.ts b/src/hooks.client.ts index 093a5d97b..f0ae8a55a 100644 --- a/src/hooks.client.ts +++ b/src/hooks.client.ts @@ -1,6 +1,7 @@ import { handleErrorWithSentry, init } from '@sentry/sveltekit'; import type { NavigationEvent } from '@sveltejs/kit'; import { dev } from '$app/environment'; +import { log } from '$lib'; init({ enabled: !dev, @@ -9,6 +10,8 @@ init({ tracesSampleRate: 1.0 }); +log.info(`sentry init`); + const myErrorHandler = ({ error, event }: { error: any; event: NavigationEvent }) => { console.error('An error occurred on the client side:', error, event); }; diff --git a/src/lib/sentry.ts b/src/lib/sentry.ts new file mode 100644 index 000000000..86b313ff0 --- /dev/null +++ b/src/lib/sentry.ts @@ -0,0 +1,21 @@ +import { setUser } from '@sentry/sveltekit'; +import type { User } from '$lib/api'; +import * as log from '$lib/log'; + +export default () => { + return { + identify: (user: User | undefined) => { + if (user) { + log.info(`sentry identify`); + setUser({ + id: user.id.toString(), + email: user.email, + username: user.name + }); + } else { + log.info(`sentry reset`); + setUser(null); + } + } + }; +}; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index cad03d5e8..0999099ac 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -12,7 +12,7 @@ import { onMount } from 'svelte'; export let data: LayoutData; - const { user, posthog, projects } = data; + const { user, posthog, projects, sentry } = data; const project = derived([page, projects], ([page, projects]) => projects.find((project) => project.id === page.params.projectId) @@ -27,6 +27,7 @@ ); user.subscribe(posthog.identify); + user.subscribe(sentry.identify);
diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index ad0b69fc0..4cdbf3764 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -4,6 +4,7 @@ import { building } from '$app/environment'; import type { Project } from '$lib/projects'; import Api from '$lib/api'; import Posthog from '$lib/posthog'; +import Sentry from '$lib/sentry'; import * as log from '$lib/log'; import { wrapLoadWithSentry } from '@sentry/sveltekit'; @@ -39,6 +40,7 @@ export const load: LayoutLoad = wrapLoadWithSentry(async ({ fetch }) => { projects, user, api: Api({ fetch }), - posthog: Posthog() + posthog: Posthog(), + sentry: Sentry() }; });