Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Move the sentry user generation logic into the telemetry component (#27152)

Ivan Vasilov9b72cd8cf626
diff --git a/apps/studio/components/ui/PageTelemetry.tsx b/apps/studio/components/ui/PageTelemetry.tsx
index ccfdeafea15..fd86596271e 100644
--- a/apps/studio/components/ui/PageTelemetry.tsx
+++ b/apps/studio/components/ui/PageTelemetry.tsx
@@ -1,10 +1,12 @@
-import { useIsLoggedIn, useParams, useTelemetryProps } from 'common'
+import * as Sentry from '@sentry/nextjs'
+import { useIsLoggedIn, useParams, useTelemetryProps, useUser } from 'common'
import { useRouter } from 'next/router'
import { PropsWithChildren, useEffect } from 'react'
import { useSelectedOrganization } from 'hooks'
import { post } from 'lib/common/fetch'
import { API_URL, IS_PLATFORM, LOCAL_STORAGE_KEYS } from 'lib/constants'
+import { getAnonId } from 'lib/telemetry'
import { useAppStateSnapshot } from 'state/app-state'
const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {
@@ -45,6 +47,26 @@ const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {
}
}, [router.isReady, snap.isOptedInTelemetry])
+ const user = useUser()
+ useEffect(() => {
+ // don't set the sentry user id if the user hasn't logged in (so that Sentry errors show null user id instead of anonymous id)
+ if (!user?.id) {
+ return
+ }
+
+ const setSentryId = async () => {
+ let sentryUserId = localStorage.getItem(LOCAL_STORAGE_KEYS.SENTRY_USER_ID)
+ if (!sentryUserId) {
+ sentryUserId = await getAnonId(user?.id)
+ localStorage.setItem(LOCAL_STORAGE_KEYS.SENTRY_USER_ID, sentryUserId)
+ }
+ Sentry.setUser({ id: sentryUserId })
+ }
+
+ // if an error happens, continue without setting a sentry id
+ setSentryId().catch((e) => console.error(e))
+ }, [user?.id])
+
/**
* send page_view event
*
diff --git a/apps/studio/pages/_app.tsx b/apps/studio/pages/_app.tsx
index 39921b960a1..ca5b2a8e7ee 100644
--- a/apps/studio/pages/_app.tsx
+++ b/apps/studio/pages/_app.tsx
@@ -23,7 +23,7 @@ import { SessionContextProvider } from '@supabase/auth-helpers-react'
import { createClient } from '@supabase/supabase-js'
import { Hydrate, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
-import { ThemeProvider, useThemeSandbox, useUser } from 'common'
+import { ThemeProvider, useThemeSandbox } from 'common'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import relativeTime from 'dayjs/plugin/relativeTime'
@@ -52,7 +52,6 @@ import { useRootQueryClient } from 'data/query-client'
import { AuthProvider } from 'lib/auth'
import { BASE_PATH, IS_PLATFORM, LOCAL_STORAGE_KEYS } from 'lib/constants'
import { ProfileProvider } from 'lib/profile'
-import { getAnonId } from 'lib/telemetry'
import { useAppStateSnapshot } from 'state/app-state'
import HCaptchaLoadedStore from 'stores/hcaptcha-loaded-store'
import { AppPropsWithLayout } from 'types'
@@ -148,27 +147,6 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
- const user = useUser()
- useEffect(() => {
- // don't set the sentry user id if the user hasn't logged in (so that Sentry errors show null user id instead of anonymous id)
- if (!user?.id) {
- return
- }
-
- const setSentryId = async () => {
- let sentryUserId = localStorage.getItem(LOCAL_STORAGE_KEYS.SENTRY_USER_ID)
-
- if (!sentryUserId) {
- sentryUserId = await getAnonId(user?.id)
- localStorage.setItem(LOCAL_STORAGE_KEYS.SENTRY_USER_ID, sentryUserId)
- }
- Sentry.setUser({ id: sentryUserId })
- }
-
- // if an error happens, continue without setting a sentry id
- setSentryId().catch((e) => console.error(e))
- }, [user?.id])
-
useThemeSandbox()
const isTestEnv = process.env.NEXT_PUBLIC_NODE_ENV === 'test'