Skip to content

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

feat(logs): route unified-logs hooks through executeAnalyticsSql (#46333)

Charis99de23913065
diff --git a/apps/studio/data/logs/unified-log-inspection-query.ts b/apps/studio/data/logs/unified-log-inspection-query.ts
index c7698b77554..303a0ce17a0 100644
--- a/apps/studio/data/logs/unified-log-inspection-query.ts
+++ b/apps/studio/data/logs/unified-log-inspection-query.ts
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import { useFlag } from 'common'
+import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import {
aggregateFunctionLogs,
@@ -198,21 +199,16 @@ WHERE id = ${lit(logId)}
LIMIT 1
`
- const { data, error } = await post('/platform/projects/{ref}/analytics/endpoints/logs.all.otel', {
- params: { path: { ref: projectRef } },
- body: {
- iso_timestamp_start: isoTimestampStart,
- iso_timestamp_end: isoTimestampEnd,
- sql,
- },
+ const rawData = await executeAnalyticsSql({
+ projectRef,
+ endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all.otel',
+ sql,
+ iso_timestamp_start: isoTimestampStart,
+ iso_timestamp_end: isoTimestampEnd,
signal,
})
- if (error) {
- handleError(error)
- }
-
- const otelData = data as { result?: OtelLogRow[] } | undefined
+ const otelData = rawData as { result?: OtelLogRow[] } | undefined
const row = otelData?.result?.[0]
if (!row) {
return { result: [] }
@@ -234,23 +230,20 @@ ORDER BY timestamp ASC
LIMIT 100
`
- const { data: fnData, error: fnError } = await post(
- '/platform/projects/{ref}/analytics/endpoints/logs.all.otel',
- {
- params: { path: { ref: projectRef } },
- body: {
- iso_timestamp_start: isoTimestampStart,
- iso_timestamp_end: isoTimestampEnd,
- sql: fnSql,
- },
+ try {
+ const fnData = await executeAnalyticsSql({
+ projectRef,
+ endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all.otel',
+ sql: fnSql,
+ iso_timestamp_start: isoTimestampStart,
+ iso_timestamp_end: isoTimestampEnd,
signal,
- }
- )
-
- if (!fnError) {
+ })
const fnRows = ((fnData as { result?: OtelLogRow[] } | undefined)?.result ??
[]) as OtelLogRow[]
Object.assign(entry, aggregateFunctionLogs(fnRows))
+ } catch {
+ // function logs are supplementary; silently ignore fetch errors
}
}
}
diff --git a/apps/studio/data/logs/unified-logs-chart-query.ts b/apps/studio/data/logs/unified-logs-chart-query.ts
index 225e2c119fa..ea078151dd3 100644
--- a/apps/studio/data/logs/unified-logs-chart-query.ts
+++ b/apps/studio/data/logs/unified-logs-chart-query.ts
@@ -1,12 +1,12 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query'
import { useFlag } from 'common'
+import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'
import { UNIFIED_LOGS_QUERY_OPTIONS, UnifiedLogsVariables } from './unified-logs-infinite-query'
import { getLogsChartQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { getLogsChartQuery as getLogsChartQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
-import { handleError, post } from '@/data/fetchers'
import { ExecuteSqlError } from '@/data/sql/execute-sql-query'
import { UseCustomQueryOptions } from '@/types'
@@ -42,18 +42,17 @@ export async function getUnifiedLogsChart(
// Get SQL query from utility function (with dynamic bucketing)
const sql = pickLogsQueryBuilder(useOtel, getLogsChartQuery, getLogsChartQueryBq)(search)
- let headers = new Headers(headersInit)
-
const endpoint = logsAllEndpointUrl(useOtel)
- const { data, error } = await post(endpoint, {
- params: { path: { ref: projectRef } },
- body: { sql, iso_timestamp_start: dateStart, iso_timestamp_end: dateEnd },
+ const data = await executeAnalyticsSql({
+ projectRef,
+ endpoint,
+ sql,
+ iso_timestamp_start: dateStart,
+ iso_timestamp_end: dateEnd,
signal,
- headers,
+ headers: headersInit,
})
- if (error) handleError(error)
-
const chartData: Array<{
timestamp: number
success: number
diff --git a/apps/studio/data/logs/unified-logs-count-query.ts b/apps/studio/data/logs/unified-logs-count-query.ts
index 81dfa7b54be..e98dc98617f 100644
--- a/apps/studio/data/logs/unified-logs-count-query.ts
+++ b/apps/studio/data/logs/unified-logs-count-query.ts
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import { useFlag } from 'common'
+import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'
import {
@@ -11,7 +12,6 @@ import {
import { getLogsCountQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { getLogsCountQuery as getLogsCountQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
import { FacetMetadataSchema } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.schema'
-import { handleError, post } from '@/data/fetchers'
import { ExecuteSqlError } from '@/data/sql/execute-sql-query'
import { UseCustomQueryOptions } from '@/types'
@@ -27,14 +27,15 @@ export async function getUnifiedLogsCount(
const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)
const endpoint = logsAllEndpointUrl(useOtel)
- const { data, error } = await post(endpoint, {
- params: { path: { ref: projectRef } },
- body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: isoTimestampEnd, sql },
+ const data = await executeAnalyticsSql({
+ projectRef,
+ endpoint,
+ sql,
+ iso_timestamp_start: isoTimestampStart,
+ iso_timestamp_end: isoTimestampEnd,
signal,
})
- if (error) handleError(error)
-
// Process count results into facets structure
const facets: Record<string, FacetMetadataSchema> = {}
const countsByDimension: Record<string, Map<string, number>> = {}
diff --git a/apps/studio/data/logs/unified-logs-facet-count-query.ts b/apps/studio/data/logs/unified-logs-facet-count-query.ts
index 6a219a539c1..959de605a4c 100644
--- a/apps/studio/data/logs/unified-logs-facet-count-query.ts
+++ b/apps/studio/data/logs/unified-logs-facet-count-query.ts
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import { useFlag } from 'common'
+import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl } from './logs-endpoint'
import { bqIdent, safeSql } from './safe-analytics-sql'
@@ -15,7 +16,6 @@ import {
getUnifiedLogsCTE,
} from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
import { Option } from '@/components/ui/DataTable/DataTable.types'
-import { handleError, post } from '@/data/fetchers'
import { ExecuteSqlError } from '@/data/sql/execute-sql-query'
import { UseCustomQueryOptions } from '@/types'
@@ -43,13 +43,14 @@ SELECT dimension, value, count from ${bqIdent(facet + '_count')};
`
const endpoint = logsAllEndpointUrl(useOtel)
- const { data, error } = await post(endpoint, {
- params: { path: { ref: projectRef } },
- body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: isoTimestampEnd, sql },
+ const data = await executeAnalyticsSql({
+ projectRef,
+ endpoint,
+ sql,
+ iso_timestamp_start: isoTimestampStart,
+ iso_timestamp_end: isoTimestampEnd,
signal,
})
-
- if (error) handleError(error)
return (data.result ?? []) as Option[]
}
diff --git a/apps/studio/data/logs/unified-logs-infinite-query.ts b/apps/studio/data/logs/unified-logs-infinite-query.ts
index c877348502a..23698524f25 100644
--- a/apps/studio/data/logs/unified-logs-infinite-query.ts
+++ b/apps/studio/data/logs/unified-logs-infinite-query.ts
@@ -1,15 +1,17 @@
import { InfiniteData, keepPreviousData, useInfiniteQuery } from '@tanstack/react-query'
import { useFlag } from 'common'
+import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'
+import { analyticsLiteral, safeSql } from './safe-analytics-sql'
import { getUnifiedLogsQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { getUnifiedLogsQuery as getUnifiedLogsQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
import {
PageParam,
QuerySearchParamsType,
} from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'
-import { handleError, post } from '@/data/fetchers'
+import { handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomInfiniteQueryOptions } from '@/types'
const LOGS_PAGE_LIMIT = 50
@@ -83,7 +85,7 @@ export async function getUnifiedLogs(
const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)
const buildQuery = pickLogsQueryBuilder(useOtel, getUnifiedLogsQuery, getUnifiedLogsQueryBq)
- const sql = `${buildQuery(search)} ORDER BY timestamp DESC, id DESC LIMIT ${LOGS_PAGE_LIMIT}`
+ const sql = safeSql`${buildQuery(search)} ORDER BY timestamp DESC, id DESC LIMIT ${analyticsLiteral(LOGS_PAGE_LIMIT)}`
const cursorValue = pageParam?.cursor
const cursorDirection = pageParam?.direction
@@ -105,17 +107,17 @@ export async function getUnifiedLogs(
timestampEnd = isoTimestampEnd
}
- let headers = new Headers(headersInit)
-
const endpoint = logsAllEndpointUrl(useOtel)
- const { data, error } = await post(endpoint, {
- params: { path: { ref: projectRef } },
- body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: timestampEnd, sql },
+ const data = await executeAnalyticsSql({
+ projectRef,
+ endpoint,
+ sql,
+ iso_timestamp_start: isoTimestampStart,
+ iso_timestamp_end: timestampEnd,
signal,
- headers,
+ headers: headersInit,
})
- if (error) handleError(error)
if (data.error) handleError(new Error(data.error as string))
const resultData = data?.result ?? []