Files
supabase/apps/studio/components/interfaces/Auth/Overview/OverviewErrors.constants.ts
Jordi Enric 548abae3e9 fix(auth): open matching logs from Overview API errors FE-5191 (#53854)
## Problem

On Auth Overview, following a failed sign-in from the Auth API Errors
table opens a legacy text search. That search can miss the requests
behind the count or include requests with a different method or status.

## Fix

Open unified logs for the current project with the gateway source, HTTP
method, path, and status from the selected row. Use the start and end
timestamps of the query that produced the count.

Closes [FE-5191](https://linear.app/supabase/issue/FE-5191).

## How to test

1. Open Authentication → Overview on a project with Auth API errors.
2. Follow the logs link on a failed sign-in row such as `POST
/auth/v1/token`.
3. Check the project, API Gateway source, method, path, and HTTP status
in Logs. The time range should match the 24-hour window used to fetch
the row, even if the page has been open for a while.

## Compatibility and verification

Explicit legacy Logs preferences still open the legacy gateway view. In
unified logs, existing Path equals and Path does not equal filters now
compare the complete path on both backends. Use Contains or Does not
contain for substring matching.

All 211 focused tests passed with retries disabled, along with Studio
typecheck and formatting. Restoring the old substring predicates made
the exact-path tests fail on both backends.

Verified the click-through on the Studio staging preview for
`c9186d34c0`. A `POST /auth/v1/signup` row with status 401 opened
unified logs on the same project with the four expected filters. The
time window was exactly 24 hours and the visible results matched the
row. The component regression covers `POST /auth/v1/token` on both
analytics backends.
2026-11-23 17:00:55 +01:00

136 lines
4.0 KiB
TypeScript

import dayjs from 'dayjs'
import { z } from 'zod'
import { pickLogsQueryBuilder } from '@/data/logs/logs-endpoint'
import { safeSql } from '@/data/logs/safe-analytics-sql'
import { fetchLogs } from '@/data/reports/report.utils'
export type ResponseErrorRow = {
method: string
path: string
status_code: number
count: number
}
export type AuthErrorCodeRow = {
error_code: string
count: number
}
export const getDateRange = () => {
return {
start: dayjs().subtract(24, 'hour').toISOString(),
end: dayjs().toISOString(),
}
}
// Top API response errors for /auth/v1 endpoints (path/method/status)
export const AUTH_TOP_RESPONSE_ERRORS_SQL = safeSql`
select
request.method as method,
request.path as path,
response.status_code as status_code,
count(*) as count
from edge_logs
cross join unnest(metadata) as m
cross join unnest(m.request) as request
cross join unnest(m.response) as response
where path like '%auth/v1%'
and response.status_code between 400 and 599
group by method, path, status_code
order by count desc
limit 10
`
// Top Auth service error codes from x_sb_error_code header for /auth/v1 endpoints
export const AUTH_TOP_ERROR_CODES_SQL = safeSql`
select
h.x_sb_error_code as error_code,
count(*) as count
from edge_logs
cross join unnest(metadata) as m
cross join unnest(m.request) as request
cross join unnest(m.response) as response
cross join unnest(response.headers) as h
where path like '%auth/v1%'
and response.status_code between 400 and 599
and h.x_sb_error_code is not null
group by error_code
order by count desc
limit 10
`
export const AUTH_TOP_RESPONSE_ERRORS_SQL_OTEL = safeSql`
select
log_attributes['request.method'] as method,
log_attributes['request.path'] as path,
toInt32OrZero(log_attributes['response.status_code']) as status_code,
count() as count
from logs
where source = 'edge_logs'
and path like '%auth/v1%'
and status_code between 400 and 599
group by method, path, status_code
order by count desc
limit 10
`
export const AUTH_TOP_ERROR_CODES_SQL_OTEL = safeSql`
select
log_attributes['response.headers.x_sb_error_code'] as error_code,
count() as count
from logs
where source = 'edge_logs'
and log_attributes['request.path'] like '%auth/v1%'
and toInt32OrZero(log_attributes['response.status_code']) between 400 and 599
and error_code != ''
group by error_code
order by count desc
limit 10
`
const countSchema = z.union([z.number(), z.string().min(1)]).pipe(z.coerce.number().finite())
const responseErrorSchema = z.object({
method: z.string(),
path: z.string(),
status_code: countSchema,
count: countSchema,
})
const authErrorCodeSchema = z.object({ error_code: z.string(), count: countSchema })
export const parseResponseErrors = (rows: unknown[]): ResponseErrorRow[] =>
rows.flatMap((row) => {
const result = responseErrorSchema.safeParse(row)
return result.success ? [result.data] : []
})
export const parseAuthErrorCodes = (rows: unknown[]): AuthErrorCodeRow[] =>
rows.flatMap((row) => {
const result = authErrorCodeSchema.safeParse(row)
return result.success ? [result.data] : []
})
export const fetchTopResponseErrors = async (projectRef: string, useOtel = false) => {
const { start, end } = getDateRange()
const sql = pickLogsQueryBuilder(
useOtel,
AUTH_TOP_RESPONSE_ERRORS_SQL_OTEL,
AUTH_TOP_RESPONSE_ERRORS_SQL
)
const data = await fetchLogs(projectRef, sql, start, end, useOtel)
if (data?.error) {
throw new Error(typeof data.error === 'string' ? data.error : data.error.message)
}
return { ...data, start, end }
}
export const fetchTopAuthErrorCodes = async (projectRef: string, useOtel = false) => {
const { start, end } = getDateRange()
const sql = pickLogsQueryBuilder(useOtel, AUTH_TOP_ERROR_CODES_SQL_OTEL, AUTH_TOP_ERROR_CODES_SQL)
const data = await fetchLogs(projectRef, sql, start, end, useOtel)
if (data?.error) {
throw new Error(typeof data.error === 'string' ? data.error : data.error.message)
}
return data
}