Files
supabase/apps/studio/components/ui/ErrorBoundary/globalErrorBoundaryHandler.test.tsx
Charis 975689dcfc feat(studio): send pageview and global crash metrics on staging and preview (#54331)
## Problem

We need simple counts of every pageview and every global crash for SLO
monitoring. These counts need to go to observability ingest, not to
Sentry, etc., so they can be part of our Grafana dashboards pipeline.

## Solution

- `usePageviewMetric` sends one `pageview` event on first load
(hydration) and on every client-side navigation that changes the
pathname. Changes to only the query string or hash do not count.
- A `global_crash` event is sent once when the global crash screen is
shown. That covers both the root `ErrorBoundary` and the root route
`errorComponent`. Chunk-load errors that are recovered by an automatic
reload are not counted, which matches what is reported to Sentry.
- Both are gated by a new `IS_STAGING_OR_PREVIEW` constant. It is false
on production, local development, self-hosted and Nimbus.
- The request is a fire-and-forget `fetch` with `keepalive: true` and
`credentials: 'omit'`. It uses a `text/plain` body, so there is no CORS
preflight. Errors are swallowed.
- The endpoint origin is added to `connect-src` for all platform
environments, because production will need it later. No other CSP
directive changes.

## Review instructions

1. Open the Studio preview deployment for this PR with the network panel
open.
2. Load any page. See one `POST /api/metrics` request that returns 204.
3. Navigate to another page. See one more request.
4. Change only a query parameter (for example a table filter). See no
new request.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-12-01 15:35:05 +01:00

98 lines
2.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { errorBoundaryHandler, reportGlobalCrashMetric } from './globalErrorBoundaryHandler'
import { sendFrontendMetric } from '@/lib/frontend-metrics'
const mocks = vi.hoisted(() => ({ isEnabled: true }))
vi.mock('@/lib/constants', () => ({
get IS_STAGING_OR_PREVIEW() {
return mocks.isEnabled
},
}))
vi.mock('@/lib/frontend-metrics', () => ({ sendFrontendMetric: vi.fn() }))
vi.mock('@sentry/react', () => ({
withScope: (callback: (scope: { setTag: () => void }) => void) => callback({ setTag: vi.fn() }),
captureException: vi.fn(() => 'event-id'),
}))
const info = { componentStack: '' }
describe('errorBoundaryHandler', () => {
const reload = vi.fn()
const originalLocation = window.location
beforeEach(() => {
mocks.isEnabled = true
window.sessionStorage.clear()
vi.mocked(sendFrontendMetric).mockReset()
reload.mockClear()
vi.spyOn(console, 'error').mockImplementation(() => {})
Object.defineProperty(window, 'location', {
configurable: true,
value: { ...originalLocation, reload },
})
})
afterEach(() => {
Object.defineProperty(window, 'location', { configurable: true, value: originalLocation })
vi.restoreAllMocks()
})
it('sends one global_crash metric for a caught crash', () => {
errorBoundaryHandler(new Error('boom'), info)
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
expect(sendFrontendMetric).toHaveBeenCalledWith('global_crash')
})
it('sends nothing when the gate is off', () => {
mocks.isEnabled = false
errorBoundaryHandler(new Error('boom'), info)
expect(sendFrontendMetric).not.toHaveBeenCalled()
})
it('sends nothing for a chunk load error recovered by an automatic reload', () => {
errorBoundaryHandler(new TypeError('Importing a module script failed.'), info)
expect(reload).toHaveBeenCalledTimes(1)
expect(sendFrontendMetric).not.toHaveBeenCalled()
})
it('counts a chunk load error that recurs inside the reload guard window', () => {
errorBoundaryHandler(new TypeError('Importing a module script failed.'), info)
errorBoundaryHandler(new TypeError('Importing a module script failed.'), info)
expect(reload).toHaveBeenCalledTimes(1)
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
})
})
describe('reportGlobalCrashMetric', () => {
beforeEach(() => {
mocks.isEnabled = true
vi.mocked(sendFrontendMetric).mockReset()
})
it('counts the same error object once', () => {
const error = new Error('boom')
reportGlobalCrashMetric(error)
reportGlobalCrashMetric(error)
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
})
it('does not throw when sending fails', () => {
vi.mocked(sendFrontendMetric).mockImplementation(() => {
throw new Error('send failed')
})
expect(() => reportGlobalCrashMetric(new Error('boom'))).not.toThrow()
})
})