Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix count query for unified logs (#46093)

Joshen Limdc211a972cdd
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
index 3481c054fe8..077ace7aa25 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
@@ -59,7 +59,10 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
pathname: '/customers',
} as any)
expect(sql).toContain(`log_attributes['request.method'] IN ('GET')`)
- expect(sql).toContain(`log_attributes['response.status_code'] IN ('401')`)
+ // Status filter wraps the CASE that picks HTTP code or Postgres SQLSTATE
+ // so e.g. '00000' matches postgres success rows.
+ expect(sql).toContain(`log_attributes['parsed.sql_state_code']`)
+ expect(sql).toMatch(/END\) IN \('401'\)/)
expect(sql).toContain(`log_attributes['request.path'] LIKE '%/customers%'`)
})
@@ -128,9 +131,12 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
search: { ...baseSearch, method: ['GET'], status: ['200'] } as any,
facet: 'method',
})
- // Filtered facet is excluded from WHERE; other filters still applied
+ // Filtered facet is excluded from WHERE; other filters still applied.
+ // For facet='method' the SELECT projection doesn't include STATUS_EXPR,
+ // so SQLSTATE/IN ('200') must come from the WHERE clause.
expect(sql).not.toContain(`log_attributes['request.method'] IN ('GET')`)
- expect(sql).toContain(`log_attributes['response.status_code'] IN ('200')`)
+ expect(sql).toContain(`log_attributes['parsed.sql_state_code']`)
+ expect(sql).toMatch(/END\) IN \('200'\)/)
expect(sql).toContain('GROUP BY value')
expect(sql).toContain('LIMIT 20')
})
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
index db880c26fd1..baa5789be6b 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
@@ -128,10 +128,13 @@ const translateFilter = (key: string, value: unknown): SafeLogSqlFragment | null
return arr
? safeSql`${ATTR.method} IN ${inList(arr)}`
: safeSql`${ATTR.method} = ${lit(String(value))}`
- case 'status':
- return arr
- ? safeSql`${ATTR.status} IN ${inList(arr)}`
- : safeSql`${ATTR.status} = ${lit(String(value))}`
+ case 'status': {
+ // Match the displayed status: HTTP response code for gateway rows,
+ // Postgres SQLSTATE for postgres rows. Inline STATUS_EXPR so e.g.
+ // filtering on '00000' picks up postgres success rows.
+ const statuses = arr ?? [value]
+ return safeSql`(${STATUS_EXPR}) IN ${inList(statuses.map((v) => String(v)))}`
+ }
case 'pathname':
return arr
? safeSql`(${joinSqlFragments(
@@ -371,8 +374,8 @@ WHERE ${baseFiltersFor('level')}
)
const facetBranches = joinSqlFragments(
- (['method', 'status', 'pathname'] as const).map((facet) =>
- getFacetCountQuery({ search, facet })
+ (['method', 'status', 'pathname'] as const).map(
+ (facet) => safeSql`(${getFacetCountQuery({ search, facet })})`
),
' UNION ALL '
)
diff --git a/apps/studio/hooks/analytics/useLogsQuery.tsx b/apps/studio/hooks/analytics/useLogsQuery.tsx
index 51e0404ffae..18a761a0222 100644
--- a/apps/studio/hooks/analytics/useLogsQuery.tsx
+++ b/apps/studio/hooks/analytics/useLogsQuery.tsx
@@ -16,6 +16,7 @@ import {
checkForWithClause,
} from '@/components/interfaces/Settings/Logs/Logs.utils'
import { get } from '@/data/fetchers'
+import { logsAllEndpointUrl } from '@/data/logs/logs-endpoint'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
import { DOCS_URL } from '@/lib/constants'
@@ -75,10 +76,7 @@ export const useLogsQuery = (
} = useQuery({
queryKey: ['projects', projectRef, 'logs', params, { otel: useOtel }],
queryFn: async ({ signal }) => {
- const endpoint = useOtel
- ? (`/platform/projects/{ref}/analytics/endpoints/logs.all.otel` as const)
- : (`/platform/projects/{ref}/analytics/endpoints/logs.all` as const)
- const { data, error } = await get(endpoint, {
+ const { data, error } = await get(logsAllEndpointUrl(useOtel), {
params: {
path: { ref: projectRef },
query: params,