Files
supabase/apps/studio/lib/sentry-client-options.ts
Drew McLellan 923bbc16fd feat(studio): sample perf-relevant routes at a higher Sentry trace rate (#54205)
## Problem

Studio traces run at a flat `tracesSampleRate` of 0.02. That's too few
samples to trust per-surface perf numbers for the dashboard performance
work, and raising the global rate would increase Sentry quota use
everywhere.

Part of
[FE-3828](https://linear.app/supabase/issue/FE-3828/raise-sentry-sample-rate-for-perf-work-stand-up-a-studio-owned-perf).

## Solution

Replace the flat rate with a `tracesSampler`
(`lib/sentry-traces-sampler.ts`):

- Project home, editor, SQL, auth and database routes are sampled at 0.2
(10x).
- Everything else stays at 0.02.
- Both rates are named constants.
- The route pattern matches the TanStack route ID
(`/project/$ref/editor`) and the resolved path, with or without the
`/dashboard` base path. A pageload's transaction name is the raw path
when it is sampled.
- A sampling decision made upstream (for example a propagated
`sentry-trace` header) is honored on navigations.
- **Each pageload starts a new trace.** Studio serves one prerendered
`_shell.html` to every visitor, and it carries `sentry-trace` and
`baggage` meta tags frozen at build time. The pageload span continues
the trace from them, so every full load shared one trace ID and one
frozen `sample_rand`. A build whose frozen value was above the route
rate could never sample a pageload, whatever the sampler returned.
`initSentryTanStackClient` now removes those two tags before
`Sentry.init`, so each pageload gets its own trace and is sampled at the
route rate.

This is temporary while the baseline is collected. To revert, delete
`sentry-traces-sampler.ts` and restore `tracesSampleRate: 0.02` in
`buildSentryClientOptions`. To only dial the boost down, lower
`PERF_ROUTE_TRACES_SAMPLE_RATE`. The steps are also in a comment at the
top of the sampler file.

The server-side rate is unchanged.

Not in this PR: the Sentry Performance view (initial load, per-surface
navigation, request timings), which is built in the Sentry UI and
tracked on the ticket.

## Review instructions

1. Read `apps/studio/lib/sentry-traces-sampler.ts` and check the route
list, rates and pageload handling.
2. Run `pnpm --filter studio exec vitest run
lib/sentry-traces-sampler.test.ts lib/sentry-client-options.test.ts
sentry.tanstack.test.ts`.
3. On a preview, check in the Sentry transaction envelopes that a
navigation to the editor, SQL, auth or database routes records
`sample_rate` 0.2, and storage records 0.02. Do a full page load of the
project home or table editor and confirm pageload transactions are now
recorded for it, with a different trace ID per load.
2026-12-03 16:35:44 +00:00

284 lines
11 KiB
TypeScript

// Sentry client-side configuration. `sentry.tanstack.ts` calls `Sentry.init`
// with these options from `getRouter()` (router.tsx).
import * as Sentry from '@sentry/react'
import { hasConsented } from 'common'
import { IS_PLATFORM } from 'common/constants/environment'
import {
BROWSER_NOISE_IGNORE_ERRORS,
BROWSER_TRANSLATION_SAMPLE_RATE,
filterSentryEvent,
isBrowserTranslationDomError,
isPageTranslated,
isSentryErrorBoundaryCrash,
} from 'common/sentry'
import { MIRRORED_BREADCRUMBS } from '@/lib/breadcrumbs'
import { applyStudioErrorPolicy } from '@/lib/errors/sentry-event-policy'
import { sanitizeArrayOfObjects, sanitizeUrlHashParams } from '@/lib/sanitize'
import { studioTracesSampler } from '@/lib/sentry-traces-sampler'
type Integration = Parameters<typeof Sentry.addIntegration>[0]
// This is a workaround to ignore hCaptcha related errors.
function isHCaptchaRelatedError(event: Sentry.Event): boolean {
const errors = event.exception?.values ?? []
for (const error of errors) {
if (
error.value?.includes('is not a function') &&
error.stacktrace?.frames?.some((f) => f.filename === 'api.js')
) {
return true
}
}
return false
}
// Filter browser wallet extension errors (e.g., Gate.io wallet)
// These errors come from injected wallet scripts and are not actionable
// Examples: SUPABASE-APP-AFC, SUPABASE-APP-92A
export function isBrowserWalletExtensionError(event: Sentry.Event): boolean {
const frames = event.exception?.values?.flatMap((e) => e.stacktrace?.frames || []) || []
return frames.some((frame) => {
const filename = frame.filename || frame.abs_path || ''
return filename.includes('gt-window-provider') || filename.includes('wallet-provider')
})
}
// Filter user-aborted operations (intentional cancellations)
// These are expected when users cancel requests or navigate away
// Examples: SUPABASE-APP-BG6, SUPABASE-APP-BG7
export function isUserAbortedOperation(error: unknown, event: Sentry.Event): boolean {
const errorMessage = error instanceof Error ? error.message : ''
const eventMessage = event.message || ''
const message = errorMessage || eventMessage
return (
message.includes('operation was aborted') ||
message.includes('signal is aborted') ||
message.includes('manually canceled') ||
message.includes('AbortError')
)
}
// Filter cancellation promise rejections (e.g., from query cancellation)
// These occur when operations are intentionally cancelled by the user
// Example: SUPABASE-APP-353 (~466k events)
export function isCancellationRejection(event: Sentry.Event): boolean {
const serialized = event.extra?.__serialized__ as Record<string, unknown> | undefined
return serialized?.type === 'cancelation'
}
// Filter challenge/captcha expired errors (user timeout)
// These happen when users don't complete captcha in time - expected behavior
// Example: SUPABASE-APP-ACC
export function isChallengeExpiredError(error: unknown, event: Sentry.Event): boolean {
const errorMessage = error instanceof Error ? error.message : ''
const eventMessage = event.message || ''
const message = errorMessage || eventMessage
return message.includes('challenge-expired')
}
export interface SentryClientOptionsParams {
/** Build-specific integrations (e.g. TanStack Router browser tracing). */
extraIntegrations?: Integration[]
/**
* Release identifier for the client.
*
* The SDK SILENTLY DROPS session envelopes when the client has no release
* (`Client.sendSession` early-returns), so a build without a release sends
* no Release Health traffic at all — errors and traces still flow.
*
* The build runs no Sentry bundler plugin to inject one, so
* sentry.tanstack.ts passes the commit SHA.
*/
release?: string
}
/**
* Applies the `sample_rate` tag set by `reportStudioError`: events tagged with a rate below 1 are
* dropped with probability `1 - rate`. Events without the tag are never dropped.
*/
export function shouldDropForSampleRate(
event: Pick<Sentry.Event, 'tags'>,
random: () => number = Math.random
): boolean {
const rate = Number(event.tags?.sample_rate)
if (event.tags?.sample_rate === undefined || !Number.isFinite(rate) || rate >= 1) return false
return random() >= rate
}
/**
* Tags browser-translation DOM errors (insert/remove node crashes) so they are sampled at
* `BROWSER_TRANSLATION_SAMPLE_RATE` by `shouldDropForSampleRate` and easy to filter in Sentry.
* `page_translated` is best-effort and only looked up for matching events. Keeps a `sample_rate`
* that is already set, such as an explicit `1`.
*/
export function tagBrowserTranslationError(event: Sentry.Event, pageTranslated?: boolean): void {
if (!isBrowserTranslationDomError(event)) return
event.tags = {
...event.tags,
// After the spread: an explicit `sample_rate: undefined` must not replace the default
sample_rate: event.tags?.sample_rate ?? BROWSER_TRANSLATION_SAMPLE_RATE,
browser_translation: true,
page_translated: pageTranslated ?? isPageTranslated(),
}
}
export function buildSentryClientOptions({
extraIntegrations = [],
release,
}: SentryClientOptionsParams = {}): Sentry.BrowserOptions {
return {
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
...(process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT && {
environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
}),
...(release && { release }),
// Setting this option to true will print useful information to the console while you're setting up Sentry.
debug: false,
// Enable performance monitoring. Temporarily samples perf-relevant routes higher (FE-3828);
// see lib/sentry-traces-sampler.ts for the rates and how to revert to the flat 0.02.
tracesSampler: studioTracesSampler,
// Leave the error `sampleRate` unset (1.0). Error sampling is per tag and fault in `ERROR_ROUTING` and
// applied in `beforeSend`; a global rate would multiply with it and also drop `alwaysCapture`.
// Function form so Sentry's default integrations (browserSession,
// globalHandlers, breadcrumbs, dedupe, …) are explicitly preserved — this
// is the documented way to extend the defaults, and it can never be
// misread as replacing them.
integrations: (defaultIntegrations) => [...defaultIntegrations, ...extraIntegrations],
// Only capture errors originating from our own code.
// This is a whitelist on the source URL in stack frames — it drops errors from
// browser extensions, injected scripts, third-party widgets, etc. (FE-2094)
allowUrls: [/https?:\/\/(.*\.)?supabase\.(com|co|green|io)/],
beforeBreadcrumb(breadcrumb, _hint) {
const cleanedBreadcrumb = { ...breadcrumb }
if (cleanedBreadcrumb.category === 'navigation') {
if (typeof cleanedBreadcrumb.data?.from === 'string') {
cleanedBreadcrumb.data.from = sanitizeUrlHashParams(cleanedBreadcrumb.data.from)
}
if (typeof cleanedBreadcrumb.data?.to === 'string') {
cleanedBreadcrumb.data.to = sanitizeUrlHashParams(cleanedBreadcrumb.data.to)
}
}
MIRRORED_BREADCRUMBS.pushBack(cleanedBreadcrumb)
return cleanedBreadcrumb
},
beforeSend(rawEvent, hint) {
const event = applyStudioErrorPolicy(rawEvent, hint.originalException)
if (!event) return null
// Must run before `filterSentryEvent`, which skips its 1% sampling for tagged events.
tagBrowserTranslationError(event)
if (!filterSentryEvent(event, { isPlatform: IS_PLATFORM, hasConsent: hasConsented() })) {
return null
}
const isErrorBoundaryCrash = isSentryErrorBoundaryCrash(event)
if (isHCaptchaRelatedError(event)) {
return null
}
// Drop events where every exception has no stack trace — these are not debuggable.
// Exempt error boundary crashes: even without stack frames, a page crash is always worth reporting.
// Also exempt browser-translation errors: they only need counting, and dropping them here
// would skip the sample.
const exceptions = event.exception?.values ?? []
if (
!isErrorBoundaryCrash &&
event.tags?.browser_translation !== true &&
exceptions.length > 0 &&
exceptions.every((ex) => !ex.stacktrace?.frames?.length)
) {
return null
}
// Filter out errors like 'e._5BLbSXV[t] is not a function' or anything matching '[t] is not a function'
if (
hint.originalException instanceof Error &&
hint.originalException.message.includes('[t] is not a function')
) {
return null
}
if (isBrowserWalletExtensionError(event)) {
return null
}
if (isUserAbortedOperation(hint.originalException, event)) {
return null
}
if (isCancellationRejection(event)) {
return null
}
if (isChallengeExpiredError(hint.originalException, event)) {
return null
}
if (shouldDropForSampleRate(event)) {
return null
}
if (event.breadcrumbs) {
event.breadcrumbs = sanitizeArrayOfObjects(event.breadcrumbs) as Sentry.Breadcrumb[]
}
return event
},
ignoreErrors: [
// === Monaco Editor ===
'ResizeObserver',
's.getModifierState is not a function',
/^Uncaught NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope'/,
// === Browser extension errors ===
// Gate.io wallet
'shouldSetTallyForCurrentProvider is not a function',
// SAP browser extensions (SAP GUI, SAP Companion)
'sap is not defined',
// Non-Error objects thrown as exceptions (e.g., Event objects)
'[object Event]',
// === Third-party SDK errors ===
// stripe-js: https://github.com/stripe/stripe-js/issues/26
'Failed to load Stripe.js',
// hCaptcha
"undefined is not an object (evaluating 'n.chat.setReady')",
"undefined is not an object (evaluating 'i.chat.setReady')",
// === User input errors (not bugs) ===
// sql-formatter lexer on invalid SQL input
/^Parse error: Unexpected ".+" at line \d+ column \d+$/,
...BROWSER_NOISE_IGNORE_ERRORS,
// === React hydration mismatches caused by extensions modifying DOM ===
// Note: we only suppress the generic browser messages, NOT "Hydration failed because..."
// which can indicate real SSR/client mismatches in our own code.
/text content does not match/i,
/There was an error while hydrating/i,
// === Web crawler / bot errors ===
'instantSearchSDKJSBridgeClearHighlight',
// === Third-party library race conditions ===
// cmdk: useSyncExternalStore subscribe called before store context is available
"Cannot read properties of undefined (reading 'subscribe')",
"undefined is not an object (evaluating 't.subscribe')",
// === Misc known noise ===
'r.default.setDefaultLevel is not a function',
// Clipboard permission denied
'The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.',
// Facebook pixel
'fb_xd_fragment',
],
}
}