mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
kb: add sentry to the kb search (#53126)
## Problem The search in KB currently does not have Sentry ## Solution This PR adds Sentry to the KB search ## Review instructions - I tried to match the style in the docs while keeping everything simple, but does it look alright? - I haven't added the env variables into Vercel yet. I wanted to get this PR in front of eyes of others first. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] 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)
This commit is contained in:
1 parent
5b6443862b
commit
e7093263eb
5 files changed
+127
-37
No files matched your search
@@ -3,6 +3,11 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
const rpc = vi.fn()
|
||||
vi.mock('@supabase/supabase-js', () => ({ createClient: () => ({ rpc }) }))
|
||||
|
||||
const init = vi.fn()
|
||||
const captureException = vi.fn()
|
||||
const flush = vi.fn(async () => true)
|
||||
vi.mock('@sentry/node', () => ({ init, captureException, flush }))
|
||||
|
||||
const { GET } = await import('./index.js')
|
||||
|
||||
const row = {
|
||||
@@ -18,6 +23,8 @@ const search = (params: string) => GET(new Request(`https://supabase.com/kb/api/
|
||||
describe('GET /api/search', () => {
|
||||
beforeEach(() => {
|
||||
rpc.mockReset()
|
||||
captureException.mockReset()
|
||||
flush.mockClear()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
@@ -50,10 +57,38 @@ describe('GET /api/search', () => {
|
||||
expect(rpc).toHaveBeenCalledWith('search_kb', { query_text: 'x', match_limit: 10 })
|
||||
})
|
||||
|
||||
it('returns 500 when the query fails', async () => {
|
||||
it('returns 500 and reports to Sentry when the query fails', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
rpc.mockResolvedValue({ data: null, error: new Error('boom') })
|
||||
rpc.mockResolvedValue({ data: null, error: { message: 'boom' } })
|
||||
|
||||
expect((await search('q=x')).status).toBe(500)
|
||||
expect(captureException).toHaveBeenCalledWith(
|
||||
new Error('boom'),
|
||||
expect.objectContaining({
|
||||
tags: { route: 'kb-search' },
|
||||
extra: expect.objectContaining({ query: 'x', limit: 10 }),
|
||||
})
|
||||
)
|
||||
expect(flush).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns 500 and reports to Sentry when the request throws', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const thrown = new Error('network down')
|
||||
rpc.mockRejectedValue(thrown)
|
||||
|
||||
expect((await search('q=x')).status).toBe(500)
|
||||
expect(captureException).toHaveBeenCalledWith(
|
||||
thrown,
|
||||
expect.objectContaining({ tags: { route: 'kb-search' } })
|
||||
)
|
||||
expect(flush).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not report successful searches', async () => {
|
||||
rpc.mockResolvedValue({ data: [], error: null })
|
||||
await search('q=x')
|
||||
expect(captureException).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('includes markdown content, or null when it cannot be fetched', async () => {
|
||||
@@ -70,3 +105,23 @@ describe('GET /api/search', () => {
|
||||
expect(results.map((r: { content: string | null }) => r.content)).toEqual(['# Heroku', null])
|
||||
})
|
||||
})
|
||||
|
||||
describe('Sentry setup', () => {
|
||||
beforeEach(() => {
|
||||
init.mockClear()
|
||||
vi.unstubAllEnvs()
|
||||
vi.resetModules()
|
||||
})
|
||||
|
||||
it('skips Sentry under vercel dev', async () => {
|
||||
vi.stubEnv('VERCEL_ENV', 'development')
|
||||
await import('./index.js')
|
||||
expect(init).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('sets up Sentry on deployments, labeled with the Vercel environment', async () => {
|
||||
vi.stubEnv('VERCEL_ENV', 'preview')
|
||||
await import('./index.js')
|
||||
expect(init).toHaveBeenCalledWith(expect.objectContaining({ environment: 'preview' }))
|
||||
})
|
||||
})
|
||||
+62
-35
@@ -1,8 +1,19 @@
|
||||
import * as Sentry from '@sentry/node'
|
||||
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
|
||||
|
||||
import { corsHeaders } from './_cors.js'
|
||||
import type { Database, KbSearchResult } from './_types.js'
|
||||
|
||||
// `vercel dev` sets VERCEL_ENV to 'development'. Elsewhere, Sentry stays disabled without a DSN (tests).
|
||||
const IS_DEV = process.env.VERCEL_ENV === 'development'
|
||||
|
||||
if (!IS_DEV) {
|
||||
Sentry.init({
|
||||
dsn: process.env.SENTRY_DSN,
|
||||
environment: process.env.VERCEL_ENV,
|
||||
})
|
||||
}
|
||||
|
||||
let client: SupabaseClient<Database> | undefined
|
||||
function supabase() {
|
||||
client ??= createClient<Database>(
|
||||
@@ -33,45 +44,61 @@ export function OPTIONS() {
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const query = searchParams.get('q')?.trim()
|
||||
const limit = Number(searchParams.get('limit')) || 10
|
||||
const includeContent = searchParams.get('content') === 'true'
|
||||
if (!query)
|
||||
return Response.json({ error: 'Missing q parameter' }, { status: 400, headers: corsHeaders })
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const query = searchParams.get('q')?.trim()
|
||||
const limit = Number(searchParams.get('limit')) || 10
|
||||
const includeContent = searchParams.get('content') === 'true'
|
||||
if (!query)
|
||||
return Response.json({ error: 'Missing q parameter' }, { status: 400, headers: corsHeaders })
|
||||
|
||||
const { data, error } = await supabase().rpc('search_kb', {
|
||||
query_text: query,
|
||||
match_limit: limit,
|
||||
})
|
||||
if (error) {
|
||||
console.error('Error running kb search:', error)
|
||||
const { data, error } = await supabase().rpc('search_kb', {
|
||||
query_text: query,
|
||||
match_limit: limit,
|
||||
})
|
||||
if (error) {
|
||||
console.error('Error running kb search:', error)
|
||||
Sentry.captureException(new Error(error.message), {
|
||||
tags: { route: 'kb-search' },
|
||||
extra: { query, limit, error },
|
||||
})
|
||||
// the function can be frozen as soon as it responds, so send the event first
|
||||
await Sentry.flush(2000)
|
||||
return Response.json(
|
||||
{ error: 'There was an error processing your request' },
|
||||
{ status: 500, headers: corsHeaders }
|
||||
)
|
||||
}
|
||||
|
||||
let results: KbSearchResult[] = data.map((row) => ({
|
||||
title: row.page_title,
|
||||
// slugs keep the case of the content file, same as the pages and their markdown exports
|
||||
href: `https://supabase.com/${row.slug}`,
|
||||
heading: row.heading,
|
||||
headingPath: row.heading_path,
|
||||
excerpt: row.excerpt,
|
||||
score: row.score,
|
||||
}))
|
||||
|
||||
if (includeContent) {
|
||||
results = await Promise.all(
|
||||
results.map(async (result) => ({ ...result, content: await fetchContent(result.href) }))
|
||||
)
|
||||
}
|
||||
|
||||
return Response.json(results, {
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=300',
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error handling kb search request:', error)
|
||||
Sentry.captureException(error, { tags: { route: 'kb-search' } })
|
||||
await Sentry.flush(2000)
|
||||
return Response.json(
|
||||
{ error: 'There was an error processing your request' },
|
||||
{ status: 500, headers: corsHeaders }
|
||||
)
|
||||
}
|
||||
|
||||
let results: KbSearchResult[] = data.map((row) => ({
|
||||
title: row.page_title,
|
||||
// slugs keep the case of the content file, same as the pages and their markdown exports
|
||||
href: `https://supabase.com/${row.slug}`,
|
||||
heading: row.heading,
|
||||
headingPath: row.heading_path,
|
||||
excerpt: row.excerpt,
|
||||
score: row.score,
|
||||
}))
|
||||
|
||||
if (includeContent) {
|
||||
results = await Promise.all(
|
||||
results.map(async (result) => ({ ...result, content: await fetchContent(result.href) }))
|
||||
)
|
||||
}
|
||||
|
||||
return Response.json(results, {
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=300',
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -24,6 +24,7 @@
|
||||
"@octokit/auth-app": "^7.0.0",
|
||||
"@octokit/core": "^7.0.6",
|
||||
"@octokit/plugin-retry": "^8.1.0",
|
||||
"@sentry/node": "catalog:",
|
||||
"@supabase/supabase-js": "catalog:",
|
||||
"astro": "^7.2.6",
|
||||
"common": "workspace:*",
|
||||
|
||||
Generated
+6
@@ -12,6 +12,9 @@ catalogs:
|
||||
'@sentry/nextjs':
|
||||
specifier: ^10.75.2
|
||||
version: 10.76.2
|
||||
'@sentry/node':
|
||||
specifier: ^10.75.2
|
||||
version: 10.76.2
|
||||
'@sentry/tanstackstart-react':
|
||||
specifier: ^10.75.2
|
||||
version: 10.76.2
|
||||
@@ -775,6 +778,9 @@ importers:
|
||||
'@octokit/plugin-retry':
|
||||
specifier: ^8.1.0
|
||||
version: 8.1.0(@octokit/core@7.0.6)
|
||||
'@sentry/node':
|
||||
specifier: 'catalog:'
|
||||
version: 10.76.2(@opentelemetry/core@2.12.0(@opentelemetry/api@1.9.1))(supports-color@8.1.1)
|
||||
'@supabase/supabase-js':
|
||||
specifier: 'catalog:'
|
||||
version: 2.117.3(@opentelemetry/api@1.9.1)
|
||||
|
||||
@@ -13,6 +13,7 @@ enableGlobalVirtualStore: false
|
||||
catalog:
|
||||
'@monaco-editor/react': 4.8.0-rc.3
|
||||
'@sentry/nextjs': ^10.75.2
|
||||
'@sentry/node': ^10.75.2
|
||||
'@sentry/tanstackstart-react': ^10.75.2
|
||||
'@supabase/auth-js': 2.117.3
|
||||
'@supabase/postgrest-js': 2.117.3
|
||||
|
||||
Reference in new issue
Block a user