mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
Adds an optional `scope` to the unified logs search: fixed filters every query applies on top of the user's, which the user can't remove. Nothing sets a scope in this PR, so the Logs page is unchanged. #50957 uses one to narrow the function Logs tab to one edge function. A scope uses the same columns and operators as the `filter` URL param, so any view can narrow logs to one resource without new query code. Columns the query builder doesn't know match a `log_attributes` key, so one edge function's logs are: ```ts { filters: [ { column: 'log_type', operator: '=', value: 'edge function' }, { column: 'log_type', operator: '=', value: 'edge function runtime' }, { column: 'function_id', operator: '=', value: functionId }, ], } ``` - New `edge function runtime` log type, read from the `function_logs` source. Its level comes from the console level (`error`/`fatal` count as errors, `warn`/`warning` as warnings), and its details stay in metadata. - **Every scan**: the scope's filters apply to rows, counts, the chart and single-facet queries. Facet scans never drop them, unlike the user's own filter on the facet's column. Filters on the same column and operator combine like the URL's (`=` values match any), every group must match, and a scope replaces the default postgres + edge log types. - **Fails closed**: a scope condition that can't be built matches no rows, instead of being dropped like a bad user filter. - **ClickHouse only**: scoped searches always run on ClickHouse, whatever `otelUnifiedLogs` says, including downloads and the log detail panel. The BigQuery queries are unchanged from master and throw on a scope rather than return rows from outside it. - `getLogTypeVisibility` offers only the log types a scope fixes. Otherwise it keeps the new log type out of the filters, since the project-wide Logs page already shows runtime logs inside each invocation's detail panel. - `toQuerySearchParams` and `createUnifiedLogsSearch` attach the scope. - `UnifiedLogs` works out which log types its queries may use once, instead of in each of its queries, filters and facets. ### How to test 1. Open the project Logs page with the unified logs preview on. The log type filter shouldn't offer "Edge Function runtime", and default results should match master. 2. The scoped behavior is easiest to try on #50957. ### Validation - Query builder tests: the scope on every count scan, the chart and facet queries, requests for out-of-scope log types, different operators on one column, the fail-closed case, BigQuery refusing a scope, and unscoped queries staying the same. - Studio typecheck, lint ratchet, Prettier and knip pass on this layer. ### Stack Merge in order: 1. #50953 — reusable logs table building blocks 2. #50954 — function-scoped unified logs queries 3. #50955 — slimmer function detail layout, header on Overview 4. #50957 — Invocations merged into a function-scoped Logs tab 5. #50958 — Overview errors in the shared logs table 6. #52281 — remove the legacy Overview --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Jordi Enric <jordi.err@gmail.com>
110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
import { act } from '@testing-library/react'
|
|
import { platformComponents } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { z } from 'zod'
|
|
|
|
import { retrieveUnifiedLogs, useGetUnifiedLogsMutation } from './get-unified-logs'
|
|
import { createUnifiedLogsSearch } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.utils'
|
|
import { customRenderHook } from '@/tests/lib/custom-render'
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
|
|
type AnalyticsResponse = platformComponents['schemas']['AnalyticsResponse_Output']
|
|
const requestSchema = z.object({ sql: z.string() })
|
|
const scopedSearch = createUnifiedLogsSearch(
|
|
{},
|
|
{
|
|
filters: [{ column: 'function_id', operator: '=', value: 'fn-123' }],
|
|
}
|
|
)
|
|
|
|
const mockExport = (useOtel: boolean) => {
|
|
const queries: string[] = []
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: useOtel
|
|
? '/platform/projects/:ref/analytics/endpoints/logs.all.otel'
|
|
: '/platform/projects/:ref/analytics/endpoints/logs.all',
|
|
response: async ({ request }) => {
|
|
queries.push(requestSchema.parse(await request.json()).sql)
|
|
return HttpResponse.json<AnalyticsResponse>({ result: [] })
|
|
},
|
|
})
|
|
return queries
|
|
}
|
|
|
|
describe('unified log exports', () => {
|
|
it('keeps direct scoped exports on ClickHouse even when the caller requests BigQuery', async () => {
|
|
const queries = mockExport(true)
|
|
await expect(
|
|
retrieveUnifiedLogs({
|
|
projectRef: 'default',
|
|
search: scopedSearch,
|
|
limit: 10,
|
|
useOtel: false,
|
|
})
|
|
).resolves.toEqual([])
|
|
expect(queries).toHaveLength(1)
|
|
expect(queries[0]).toContain("log_attributes['function_id'] IN ('fn-123')")
|
|
})
|
|
|
|
it('keeps scoped mutation exports on ClickHouse despite an explicit legacy override', async () => {
|
|
const queries = mockExport(true)
|
|
const { result } = customRenderHook(() => useGetUnifiedLogsMutation())
|
|
await act(async () => {
|
|
await expect(
|
|
result.current.mutateAsync({
|
|
projectRef: 'default',
|
|
search: scopedSearch,
|
|
limit: 10,
|
|
useOtel: false,
|
|
})
|
|
).resolves.toEqual([])
|
|
})
|
|
expect(queries).toHaveLength(1)
|
|
expect(queries[0]).toContain("log_attributes['function_id'] IN ('fn-123')")
|
|
})
|
|
|
|
it('reports a failed scoped export without falling back to an unscoped backend', async () => {
|
|
const queries: string[] = []
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
|
|
response: async ({ request }) => {
|
|
queries.push(requestSchema.parse(await request.json()).sql)
|
|
return HttpResponse.json<APIErrorBody>(
|
|
{ message: 'Analytics unavailable' },
|
|
{ status: 503 }
|
|
)
|
|
},
|
|
})
|
|
await expect(
|
|
retrieveUnifiedLogs({
|
|
projectRef: 'default',
|
|
search: scopedSearch,
|
|
limit: 10,
|
|
useOtel: false,
|
|
})
|
|
).rejects.toMatchObject({ message: 'Analytics unavailable' })
|
|
expect(queries).toHaveLength(1)
|
|
expect(queries[0]).toContain("log_attributes['function_id'] IN ('fn-123')")
|
|
})
|
|
|
|
it.each([
|
|
{ backend: 'BigQuery', useOtel: false },
|
|
{ backend: 'ClickHouse', useOtel: true },
|
|
])('exports project-wide logs through the selected $backend backend', async ({ useOtel }) => {
|
|
const queries = mockExport(useOtel)
|
|
await expect(
|
|
retrieveUnifiedLogs({
|
|
projectRef: 'default',
|
|
search: createUnifiedLogsSearch(),
|
|
limit: 10,
|
|
useOtel,
|
|
})
|
|
).resolves.toEqual([])
|
|
expect(queries).toHaveLength(1)
|
|
expect(queries[0]).not.toContain("log_attributes['function_id']")
|
|
})
|
|
})
|