The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(studio): derive unified auth log severity from log_attributes[status] (#46854)
Jordi Enric39ed3add2f3e
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.tsindex a8bea182e51..5d9d9f8380f 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts@@ -70,6 +70,16 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {expect(sql).toMatch(/END\) NOT IN \('401'\)/)})+ it('reads the HTTP status from log_attributes[status] for auth rows', () => {+ // Auth-service logs expose their status under `status`, not the gateway's+ // `response.status_code`, so without this their 4xx/5xx classify as+ // success and the severity filter returns nothing.+ const sql = getUnifiedLogsQuery(withFilters('log_type:eq:auth'))+ expect(sql).toContain(+ `if(source = 'auth_logs', log_attributes['status'], log_attributes['response.status_code'])`+ )+ })+it('flips LIKE to NOT LIKE when the pathname/host operator is `<>`', () => {const sql = getUnifiedLogsQuery(withFilters('pathname:neq:/health', 'host:neq:cdn.foo'))expect(sql).toContain(`log_attributes['request.path'] NOT LIKE '%/health%'`)@@ -171,6 +181,13 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {} as any)expect(sql).toContain('toStartOfDay(timestamp)')})++ it('buckets auth rows by their log_attributes[status] so 4xx/5xx are not counted as success', () => {+ const sql = getLogsChartQuery(baseSearch)+ expect(sql).toContain(+ `if(source = 'auth_logs', log_attributes['status'], log_attributes['response.status_code'])`+ )+ })})describe('getFacetCountQuery', () => {diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.tsindex 01839820d90..39ab733e62e 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts@@ -36,6 +36,14 @@ const ATTR = {path: safeSql`log_attributes['request.path']`,} as const+// The HTTP status code lives under different OTEL attribute keys per service:+// gateway rows (edge / postgrest / storage / edge function) expose it as+// `response.status_code`, while auth-service rows expose it as `status`. This+// normalizes the two so both the displayed status and the derived severity are+// correct for auth logs (which would otherwise have an empty status and fall+// back to their `severity_text` of INFO, classifying every 4xx/5xx as success).+const HTTP_STATUS_EXPR: SafeLogSqlFragment = safeSql`if(source = 'auth_logs', log_attributes['status'], ${ATTR.status})`+/*** Predicate that matches rows belonging to a given log_type. Mirrors the* shape of the original BigQuery unified-logs CTEs: edge gateway traffic@@ -72,25 +80,26 @@ const LOG_TYPE_EXPR: SafeLogSqlFragment = safeSql`CASEELSE sourceEND`-// Status code is sourced from the HTTP response for gateway-style rows and-// from the Postgres `parsed.sql_state_code` (e.g. `42P01`) for postgres rows.+// Status code is sourced from the HTTP response for gateway-style rows, the+// auth-service `status` attribute for auth rows, and the Postgres+// `parsed.sql_state_code` (e.g. `42P01`) for postgres rows.const STATUS_EXPR: SafeLogSqlFragment = safeSql`CASEWHEN source = 'postgres_logs' THEN toString(log_attributes['parsed.sql_state_code'])- ELSE toString(${ATTR.status})+ ELSE toString((${HTTP_STATUS_EXPR}))END`// SQL expression for derived `level`. Used inline (not as alias reference)// because the OTEL endpoint can't resolve aliases inside countIf when the// alias is not in GROUP BY.//-// HTTP status is checked first so gateway rows (which always carry an+// HTTP status is checked first so gateway and auth rows (which carry a// `severity_text` of `INFO` regardless of response code) bucket as// success/warning/error by status. Postgres-style severity is the// fallback for rows without a status code.const LEVEL_EXPR: SafeLogSqlFragment = safeSql`CASE- WHEN ${ATTR.status} != '' AND toInt32OrZero(${ATTR.status}) >= 500 THEN 'error'- WHEN ${ATTR.status} != '' AND toInt32OrZero(${ATTR.status}) BETWEEN 400 AND 499 THEN 'warning'- WHEN ${ATTR.status} != '' AND toInt32OrZero(${ATTR.status}) BETWEEN 200 AND 299 THEN 'success'+ WHEN (${HTTP_STATUS_EXPR}) != '' AND toInt32OrZero((${HTTP_STATUS_EXPR})) >= 500 THEN 'error'+ WHEN (${HTTP_STATUS_EXPR}) != '' AND toInt32OrZero((${HTTP_STATUS_EXPR})) BETWEEN 400 AND 499 THEN 'warning'+ WHEN (${HTTP_STATUS_EXPR}) != '' AND toInt32OrZero((${HTTP_STATUS_EXPR})) BETWEEN 200 AND 299 THEN 'success'WHEN severity_text IN ('ERROR','FATAL','CRITICAL','ALERT','EMERGENCY') THEN 'error'WHEN severity_text IN ('WARN','WARNING') THEN 'warning'WHEN severity_text IN ('TRACE','DEBUG','INFO','LOG','NOTICE') THEN 'success'