The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat(logs): migrate unified logs queries to OTEL endpoint DEBUG-71 (#45642)
Jordi Enric19e0b36650dc
diff --git a/apps/studio/components/interfaces/App/FeaturePreview/FeaturePreviewContext.tsx b/apps/studio/components/interfaces/App/FeaturePreview/FeaturePreviewContext.tsxindex ea7ac88e49e..25c8feacf9d 100644--- a/apps/studio/components/interfaces/App/FeaturePreview/FeaturePreviewContext.tsx+++ b/apps/studio/components/interfaces/App/FeaturePreview/FeaturePreviewContext.tsx@@ -14,6 +14,7 @@ import {import { useFeaturePreviews } from './useFeaturePreviews'import { useIsEnterpriseOrSupabaseOrg } from './useIsEnterpriseOrSupabaseOrg'import { useStaticEffectEvent } from '@/hooks/useStaticEffectEvent'+import { IS_STAGING_OR_LOCAL } from '@/lib/constants'import { EMPTY_OBJ } from '@/lib/void'type FeaturePreviewContextType = {@@ -28,7 +29,7 @@ const FeaturePreviewContext = createContext<FeaturePreviewContextType>({export const useFeaturePreviewContext = () => useContext(FeaturePreviewContext)-export const FeaturePreviewContextProvider = ({ children }: PropsWithChildren<{}>) => {+export const FeaturePreviewContextProvider = ({ children }: PropsWithChildren) => {const { hasLoaded } = useContext(FeatureFlagContext)const featurePreviews = useFeaturePreviews()@@ -93,7 +94,7 @@ export const useUnifiedLogsPreview = () => {useIsEnterpriseOrSupabaseOrg()const isLoading = !flagsHaveLoaded || isOrgLoading- const isEligible = unifiedLogsEnabled && isEnterpriseOrSupabaseOrg+ const isEligible = unifiedLogsEnabled && (IS_STAGING_OR_LOCAL || isEnterpriseOrSupabaseOrg)const isEnabled = unifiedLogsEnabled && flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS]const enable = () => onUpdateFlag(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS, true)diff --git a/apps/studio/components/interfaces/App/FeaturePreview/useFeaturePreviews.ts b/apps/studio/components/interfaces/App/FeaturePreview/useFeaturePreviews.tsindex b438fc92ecc..7c3924b14fa 100644--- a/apps/studio/components/interfaces/App/FeaturePreview/useFeaturePreviews.ts+++ b/apps/studio/components/interfaces/App/FeaturePreview/useFeaturePreviews.ts@@ -1,6 +1,7 @@import { LOCAL_STORAGE_KEYS, useFlag } from 'common'import { useIsEnterpriseOrSupabaseOrg } from './useIsEnterpriseOrSupabaseOrg'+import { IS_STAGING_OR_LOCAL } from '@/lib/constants'export type FeaturePreview = {key: string@@ -37,7 +38,7 @@ export const useFeaturePreviews = (): FeaturePreview[] => {key: LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS,name: 'New Logs interface',discussionsUrl: 'https://github.com/orgs/supabase/discussions/37234',- enabled: isUnifiedLogsPreviewAvailable && isEnterpriseOrSupabaseOrg,+ enabled: isUnifiedLogsPreviewAvailable && (IS_STAGING_OR_LOCAL || isEnterpriseOrSupabaseOrg),isNew: false,isPlatformOnly: true,isDefaultOptIn: false,diff --git a/apps/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx b/apps/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsxindex b1d57775ac1..bf64d392410 100644--- a/apps/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx+++ b/apps/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx@@ -18,10 +18,12 @@ import {DropdownMenuContent,DropdownMenuItem,DropdownMenuTrigger,+ Label_Shadcn_,Popover_Shadcn_,PopoverContent_Shadcn_,PopoverTrigger_Shadcn_,SidePanel,+ Switch,Tooltip,TooltipContent,TooltipTrigger,@@ -36,7 +38,7 @@ import { DatePickerValue, LogsDatePicker } from './Logs.DatePickers'import { LogsWarning, LogTemplate } from './Logs.types'import Table from '@/components/to-be-cleaned/Table'import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'-import { DOCS_URL } from '@/lib/constants'+import { DOCS_URL, IS_STAGING_OR_LOCAL } from '@/lib/constants'export interface LogsQueryPanelProps {templates?: LogTemplate[]@@ -45,6 +47,8 @@ export interface LogsQueryPanelProps {onSelectTemplate: (template: LogTemplate) => voidonSelectSource: (source: string) => voidonDateChange: (value: DatePickerValue) => void+ useOtel?: boolean+ onUseOtelChange?: (value: boolean) => void}function DropdownMenuItemContent({ name, desc }: { name: ReactNode; desc?: string }) {@@ -63,9 +67,14 @@ const LogsQueryPanel = ({onSelectTemplate,onSelectSource,onDateChange,+ useOtel = false,+ onUseOtelChange,}: LogsQueryPanelProps) => {const [showReference, setShowReference] = useState(false)const { logsTemplates } = useIsFeatureEnabled(['logs:templates'])+ // Staff-only debugging affordance: only show on staging/local, never to+ // enterprise customers running against production.+ const otelToggleEnabled = IS_STAGING_OR_LOCAL && !!onUseOtelChangeconst {projectAuthAll: authEnabled,@@ -152,6 +161,30 @@ const LogsQueryPanel = ({helpers={EXPLORER_DATEPICKER_HELPERS}/>+ {otelToggleEnabled && (+ <Tooltip>+ <TooltipTrigger asChild>+ <div className="flex items-center gap-2">+ <Switch+ id="logs-explorer-otel-toggle"+ checked={useOtel}+ onCheckedChange={(checked) => onUseOtelChange?.(checked)}+ />+ <Label_Shadcn_+ htmlFor="logs-explorer-otel-toggle"+ className="text-xs text-foreground-light cursor-pointer"+ >+ OTEL endpoint+ </Label_Shadcn_>+ </div>+ </TooltipTrigger>+ <TooltipContent side="bottom" className="max-w-xs">+ Run this query against the new ClickHouse-backed OTEL endpoint instead of+ BigQuery. Use to validate ClickHouse SQL before relying on it.+ </TooltipContent>+ </Tooltip>+ )}+<divdata-testid="log-explorer-warnings"className={`transition-all duration-300 h-full ${diff --git a/apps/studio/components/interfaces/UnifiedLogs/ServiceFlow/components/blocks/PostgresFlowDetail.tsx b/apps/studio/components/interfaces/UnifiedLogs/ServiceFlow/components/blocks/PostgresFlowDetail.tsxindex 357215c95db..1895f904349 100644--- a/apps/studio/components/interfaces/UnifiedLogs/ServiceFlow/components/blocks/PostgresFlowDetail.tsx+++ b/apps/studio/components/interfaces/UnifiedLogs/ServiceFlow/components/blocks/PostgresFlowDetail.tsx@@ -3,6 +3,7 @@ import { Cable, Clock, Database } from 'lucide-react'import { memo } from 'react'import { ColumnSchema } from '../../../UnifiedLogs.schema'+import { getRowTimestampMs } from '../../../UnifiedLogs.utils'import { postgresDetailsFields, postgresPrimaryFields } from '../../config/serviceFlowFields'import { BlockFieldConfig } from '../../types'import { DetailRow } from '../shared/DetailRow'@@ -61,11 +62,7 @@ export const PostgresFlowDetail = memo(function PostgresFlowDetail({filterFields,table,}: PostgresFlowDetailProps) {- const timestampMs = data?.timestamp- ? data.timestamp / 1000- : data?.date- ? data.date.getTime()- : null+ const timestampMs = getRowTimestampMs(data)const formattedTime = timestampMs ? new Date(timestampMs).toLocaleString() : nullconst severity: string | undefined =@@ -75,7 +72,7 @@ export const PostgresFlowDetail = memo(function PostgresFlowDetail({return (<div className="[&>*:nth-child(even)]:bg-surface-100/50"><DetailSectionHeader- title="Requested started"+ title="Request started"icon={Clock}summary={formattedTime ?? undefined}/>diff --git a/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsx b/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsxindex d86e1649be2..a36f3004e62 100644--- a/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsx+++ b/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsx@@ -27,6 +27,7 @@ import { ServiceFlowPanelControls } from './ServiceFlow/components/ServiceFlowPaimport { DetailSectionHeader } from './ServiceFlow/components/shared/DetailSection'import { ColumnSchema } from './UnifiedLogs.schema'import { QuerySearchParamsType } from './UnifiedLogs.types'+import { getRowTimestampMs } from './UnifiedLogs.utils'import { useDataTable } from '@/components/ui/DataTable/providers/DataTableProvider'import {ServiceFlowType,@@ -89,11 +90,7 @@ export function ServiceFlowPanel({if (!selectedRowKey || !selectedRow) return null- const timestampMs = selectedRow.timestamp- ? selectedRow.timestamp / 1000- : selectedRow.date- ? selectedRow.date.getTime()- : null+ const timestampMs = getRowTimestampMs(selectedRow)const formattedTime = timestampMs ? new Date(timestampMs).toLocaleString() : null// Prepare JSON data for Raw JSON tabdiff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.tsnew file mode 100644index 00000000000..96feefeac72--- /dev/null+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts@@ -0,0 +1,608 @@+// Legacy BigQuery unified-logs queries. Kept side-by-side with+// UnifiedLogs.queries.ts (the OTEL/ClickHouse version) so the+// `otelUnifiedLogs` feature flag can route traffic between the two paths+// during the migration. This file should be deleted once the flag is+// removed.++import { literal } from '@supabase/pg-meta/src/pg-format'+import dayjs from 'dayjs'++import { DEFAULT_LOG_TYPES } from './UnifiedLogs.constants'+import { QuerySearchParamsType, SearchParamsType } from './UnifiedLogs.types'++// Pagination and control parameters+const PAGINATION_PARAMS = ['sort', 'start', 'size', 'uuid', 'cursor', 'direction', 'live'] as const++// Special filter parameters that need custom handling+const SPECIAL_FILTER_PARAMS = ['date'] as const++// Combined list of all parameters to exclude from standard filtering+const EXCLUDED_QUERY_PARAMS = [...PAGINATION_PARAMS, ...SPECIAL_FILTER_PARAMS] as const++// Strips `literal()`'s surrounding quotes so the value can be concatenated+// inside a `LIKE '%...%'` clause without re-escaping; quotes and back-+// slashes are already escaped by literal().+const likeValue = (value: unknown): string =>+ literal(String(value)).replace(/^E?'/, '').replace(/'$/, '')++/**+ * Builds query conditions from search parameters and returns WHERE clause+ * @param search SearchParamsType object containing query parameters+ * @returns Object with whereConditions array and formatted WHERE clause+ */+const buildQueryConditions = (search: QuerySearchParamsType) => {+ const whereConditions: string[] = []++ // Process all search parameters for filtering+ Object.entries(search).forEach(([key, value]) => {+ // Skip pagination/control parameters+ if ((EXCLUDED_QUERY_PARAMS as readonly string[]).includes(key)) {+ return+ }++ // Handle array filters (IN clause)+ if (Array.isArray(value) && value.length > 0) {+ whereConditions.push(`${key} IN (${value.map((v) => literal(String(v))).join(',')})`)+ return+ }++ // Handle scalar values+ if (value !== null && value !== undefined) {+ if (['host', 'pathname'].includes(key)) {+ whereConditions.push(`${key} LIKE '%${likeValue(value)}%'`)+ } else {+ whereConditions.push(`${key} = ${literal(String(value))}`)+ }+ }+ })++ // Create final WHERE clause+ const finalWhere = whereConditions.length > 0 ? `WHERE ${whereConditions.join(' AND ')}` : ''++ return { whereConditions, finalWhere }+}++/**+ * Calculates how much the chart start datetime should be offset given the current datetime filter params+ * and determines the appropriate bucketing level (minute, hour, day)+ * Ported from the older implementation (apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts)+ */+const calculateChartBucketing = (search: SearchParamsType | Record<string, unknown>): string => {+ // Extract start and end times from the date array if available+ const dateRange = (search.date as Array<Date | string | number | null | undefined>) || []++ // Handle timestamps that could be in various formats+ const convertToMillis = (timestamp: Date | string | number | null | undefined) => {+ if (!timestamp) return null+ // If timestamp is a Date object+ if (timestamp instanceof Date) return timestamp.getTime()++ // If timestamp is a string that needs parsing+ if (typeof timestamp === 'string') return dayjs(timestamp).valueOf()++ // If timestamp is already a number (unix timestamp)+ // Check if microseconds (16 digits) and convert to milliseconds+ if (typeof timestamp === 'number') {+ const str = timestamp.toString()+ if (str.length >= 16) return Math.floor(timestamp / 1000)+ return timestamp+ }++ return null+ }++ let startMillis = convertToMillis(dateRange[0])+ let endMillis = convertToMillis(dateRange[1])++ // Default values if not set+ if (!startMillis) startMillis = dayjs().subtract(1, 'hour').valueOf()+ if (!endMillis) endMillis = dayjs().valueOf()++ const startTime = dayjs(startMillis)+ const endTime = dayjs(endMillis)++ let truncationLevel = 'MINUTE'++ const hourDiff = endTime.diff(startTime, 'hour')+ const dayDiff = endTime.diff(startTime, 'day')++ // Adjust bucketing based on time range+ if (dayDiff >= 2) {+ truncationLevel = 'DAY'+ } else if (hourDiff >= 12) {+ truncationLevel = 'HOUR'+ } else {+ truncationLevel = 'MINUTE'+ }++ return truncationLevel+}++/**+ * Edge logs query fragment+ *+ * excludes `/rest/` in the path+ */+const getEdgeLogsQuery = () => {+ return `+ select+ id,+ null as source_id,+ el.timestamp as timestamp,+ 'edge' as log_type,+ CAST(edge_logs_response.status_code AS STRING) as status,+ CASE+ WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN edge_logs_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ edge_logs_request.path as pathname,+ null as event_message,+ edge_logs_request.method as method,+ null as log_count,+ null as logs+ from edge_logs as el+ cross join unnest(metadata) as edge_logs_metadata+ cross join unnest(edge_logs_metadata.request) as edge_logs_request+ cross join unnest(edge_logs_metadata.response) as edge_logs_response++ -- ONLY include logs where the path does not include /rest/+ WHERE edge_logs_request.path NOT LIKE '%/rest/%'+ AND edge_logs_request.path NOT LIKE '%/storage/%'++ `+}++// Postgrest logs++// WHERE pathname includes `/rest/`+const getPostgrestLogsQuery = () => {+ return `+ select+ id,+ null as source_id,+ el.timestamp as timestamp,+ 'postgrest' as log_type,+ CAST(edge_logs_response.status_code AS STRING) as status,+ CASE+ WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN edge_logs_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ edge_logs_request.path as pathname,+ null as event_message,+ edge_logs_request.method as method,+ null as log_count,+ null as logs+ from edge_logs as el+ cross join unnest(metadata) as edge_logs_metadata+ cross join unnest(edge_logs_metadata.request) as edge_logs_request+ cross join unnest(edge_logs_metadata.response) as edge_logs_response++ -- ONLY include logs where the path includes /rest/+ WHERE edge_logs_request.path LIKE '%/rest/%'+ `+}++/**+ * Postgres logs query fragment+ */+const getPostgresLogsQuery = () => {+ return `+ select+ id,+ null as source_id,+ pgl.timestamp as timestamp,+ 'postgres' as log_type,+ CAST(pgl_parsed.sql_state_code AS STRING) as status,+ CASE+ WHEN pgl_parsed.error_severity = 'LOG' THEN 'success'+ WHEN pgl_parsed.error_severity = 'WARNING' THEN 'warning'+ WHEN pgl_parsed.error_severity = 'FATAL' THEN 'error'+ WHEN pgl_parsed.error_severity = 'ERROR' THEN 'error'+ ELSE null+ END as level,+ null as pathname,+ event_message as event_message,+ null as method,+ null as log_count,+ null as logs+ from postgres_logs as pgl+ cross join unnest(pgl.metadata) as pgl_metadata+ cross join unnest(pgl_metadata.parsed) as pgl_parsed+ `+}++/**+ * Edge function logs query fragment+ */+const getEdgeFunctionLogsQuery = () => {+ return `+ select+ id,+ null as source_id,+ fel.timestamp as timestamp,+ 'edge function' as log_type,+ CAST(fel_response.status_code AS STRING) as status,+ CASE+ WHEN fel_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN fel_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN fel_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ fel_request.pathname as pathname,+ COALESCE(function_logs_agg.last_event_message, '') as event_message,+ fel_request.method as method,+ function_logs_agg.function_log_count as log_count,+ null as logs+ from function_edge_logs as fel+ cross join unnest(metadata) as fel_metadata+ cross join unnest(fel_metadata.response) as fel_response+ cross join unnest(fel_metadata.request) as fel_request+ left join (+ SELECT+ fl_metadata.request_id,+ COUNT(fl.id) as function_log_count,+ ANY_VALUE(fl.event_message) as last_event_message+ FROM function_logs as fl+ CROSS JOIN UNNEST(fl.metadata) as fl_metadata+ WHERE fl_metadata.request_id IS NOT NULL+ GROUP BY fl_metadata.request_id+ ) as function_logs_agg on fel_metadata.request_id = function_logs_agg.request_id+ `+}++/**+ * Auth logs query fragment+ */+const getAuthLogsQuery = () => {+ return `+ select+ el_in_al.id as id,+ al.id as source_id,+ el_in_al.timestamp as timestamp,+ 'auth' as log_type,+ CAST(el_in_al_response.status_code AS STRING) as status,+ CASE+ WHEN el_in_al_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN el_in_al_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN el_in_al_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ el_in_al_request.path as pathname,+ null as event_message,+ el_in_al_request.method as method,+ null as log_count,+ null as logs+ from auth_logs as al+ cross join unnest(metadata) as al_metadata+ left join (+ edge_logs as el_in_al+ cross join unnest (metadata) as el_in_al_metadata+ cross join unnest (el_in_al_metadata.response) as el_in_al_response+ cross join unnest (el_in_al_response.headers) as el_in_al_response_headers+ cross join unnest (el_in_al_metadata.request) as el_in_al_request+ )+ on al_metadata.request_id = el_in_al_response_headers.cf_ray+ WHERE al_metadata.request_id is not null+ `+}++/**+ * Supabase storage logs query fragment+ */+const getSupabaseStorageLogsQuery = () => {+ return `+ select+ id,+ null as source_id,+ el.timestamp as timestamp,+ 'storage' as log_type,+ CAST(edge_logs_response.status_code AS STRING) as status,+ CASE+ WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN edge_logs_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ edge_logs_request.path as pathname,+ null as event_message,+ edge_logs_request.method as method,+ null as log_count,+ null as logs+ from edge_logs as el+ cross join unnest(metadata) as edge_logs_metadata+ cross join unnest(edge_logs_metadata.request) as edge_logs_request+ cross join unnest(edge_logs_metadata.response) as edge_logs_response+ -- ONLY include logs where the path includes /storage/+ WHERE edge_logs_request.path LIKE '%/storage/%'+ `+}++const LOG_TYPE_QUERIES: Record<string, () => string> = {+ edge: getEdgeLogsQuery,+ postgrest: getPostgrestLogsQuery,+ postgres: getPostgresLogsQuery,+ 'edge function': getEdgeFunctionLogsQuery,+ auth: getAuthLogsQuery,+ storage: getSupabaseStorageLogsQuery,+}++/**+ * Combine the requested log sources to create the unified logs CTE.+ * Defaults to postgres + postgrest on first load to reduce query cost.+ */+export const getUnifiedLogsCTE = (logTypes: string[] = [...DEFAULT_LOG_TYPES]) => {+ const queries = logTypes+ .filter((type) => type in LOG_TYPE_QUERIES)+ .map((type) => LOG_TYPE_QUERIES[type]())++ const effectiveQueries =+ queries.length > 0 ? queries : DEFAULT_LOG_TYPES.map((type) => LOG_TYPE_QUERIES[type]())++ return `+WITH unified_logs AS (+ ${effectiveQueries.join('\n union all\n ')}+)+ `+}++/**+ * Unified logs SQL query+ */+export const getUnifiedLogsQuery = (search: QuerySearchParamsType): string => {+ const { finalWhere } = buildQueryConditions(search)+ const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]++ const sql = `+${getUnifiedLogsCTE(effectiveLogTypes)}+SELECT+ id,+ source_id,+ timestamp,+ log_type,+ status,+ level,+ pathname,+ event_message,+ method,+ log_count,+ logs+FROM unified_logs+${finalWhere}+`++ return sql+}++/**+ * Get a count query for the total logs within the timeframe+ * Uses proper faceted search behavior where facets show "what would I get if I selected ONLY this option"+ */++// Helper function to build WHERE clause excluding a specific field+const buildFacetWhere = (search: QuerySearchParamsType, excludeField: string): string => {+ const conditions: string[] = []++ Object.entries(search).forEach(([key, value]) => {+ if (key === excludeField) return // Skip the field we're getting facets for+ if ((EXCLUDED_QUERY_PARAMS as readonly string[]).includes(key)) return // Skip pagination and special params++ // Handle array filters (IN clause)+ if (Array.isArray(value) && value.length > 0) {+ conditions.push(`${key} IN (${value.map((v) => literal(String(v))).join(',')})`)+ return+ }++ // Handle scalar values+ if (value !== null && value !== undefined) {+ if (['host', 'pathname'].includes(key)) {+ conditions.push(`${key} LIKE '%${likeValue(value)}%'`)+ } else {+ conditions.push(`${key} = ${literal(String(value))}`)+ }+ }+ })++ return conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''+}++export const getFacetCountCTE = ({+ search,+ facet,+ facetSearch,+}: {+ search: QuerySearchParamsType+ facet: string+ facetSearch?: string+}) => {+ const MAX_FACETS_QUANTITY = 20++ return `+${facet}_count AS (+ SELECT '${facet}' as dimension, ${facet} as value, COUNT(*) as count+ FROM unified_logs+ ${buildFacetWhere(search, `${facet}`) || `WHERE ${facet} IS NOT NULL`}+ ${buildFacetWhere(search, `${facet}`) ? ` AND ${facet} IS NOT NULL` : ''}+ ${!!facetSearch ? `AND ${facet} LIKE '%${likeValue(facetSearch)}%'` : ''}+ GROUP BY ${facet}+ LIMIT ${MAX_FACETS_QUANTITY}+)+`.trim()+}++export const getUnifiedLogsCountCTE = () => {+ return `+WITH unified_logs AS (+ -- Single scan of edge_logs covering edge gateway, postgrest, and storage+ select+ id,+ CASE+ WHEN edge_logs_request.path LIKE '%/rest/%' THEN 'postgrest'+ WHEN edge_logs_request.path LIKE '%/storage/%' THEN 'storage'+ ELSE 'edge'+ END as log_type,+ CAST(edge_logs_response.status_code AS STRING) as status,+ CASE+ WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN edge_logs_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ edge_logs_request.path as pathname,+ edge_logs_request.method as method+ from edge_logs as el+ cross join unnest(metadata) as edge_logs_metadata+ cross join unnest(edge_logs_metadata.request) as edge_logs_request+ cross join unnest(edge_logs_metadata.response) as edge_logs_response++ union all++ -- Postgres logs+ select+ id,+ 'postgres' as log_type,+ CAST(pgl_parsed.sql_state_code AS STRING) as status,+ CASE+ WHEN pgl_parsed.error_severity = 'LOG' THEN 'success'+ WHEN pgl_parsed.error_severity = 'WARNING' THEN 'warning'+ WHEN pgl_parsed.error_severity = 'FATAL' THEN 'error'+ WHEN pgl_parsed.error_severity = 'ERROR' THEN 'error'+ ELSE null+ END as level,+ null as pathname,+ null as method+ from postgres_logs as pgl+ cross join unnest(pgl.metadata) as pgl_metadata+ cross join unnest(pgl_metadata.parsed) as pgl_parsed++ union all++ -- Edge function logs+ select+ fel.id,+ 'edge function' as log_type,+ CAST(fel_response.status_code AS STRING) as status,+ CASE+ WHEN fel_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN fel_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN fel_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ fel_request.pathname as pathname,+ fel_request.method as method+ from function_edge_logs as fel+ cross join unnest(metadata) as fel_metadata+ cross join unnest(fel_metadata.response) as fel_response+ cross join unnest(fel_metadata.request) as fel_request++ union all++ -- Auth logs+ select+ el_in_al.id as id,+ 'auth' as log_type,+ CAST(el_in_al_response.status_code AS STRING) as status,+ CASE+ WHEN el_in_al_response.status_code BETWEEN 200 AND 299 THEN 'success'+ WHEN el_in_al_response.status_code BETWEEN 400 AND 499 THEN 'warning'+ WHEN el_in_al_response.status_code >= 500 THEN 'error'+ ELSE 'success'+ END as level,+ el_in_al_request.path as pathname,+ el_in_al_request.method as method+ from auth_logs as al+ cross join unnest(metadata) as al_metadata+ left join (+ edge_logs as el_in_al+ cross join unnest(metadata) as el_in_al_metadata+ cross join unnest(el_in_al_metadata.response) as el_in_al_response+ cross join unnest(el_in_al_response.headers) as el_in_al_response_headers+ cross join unnest(el_in_al_metadata.request) as el_in_al_request+ )+ on al_metadata.request_id = el_in_al_response_headers.cf_ray+ WHERE al_metadata.request_id is not null+)+ `+}++export const getLogsCountQuery = (search: QuerySearchParamsType): string => {+ const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]+ const logTypeWhere = buildFacetWhere(search, 'log_type') || 'WHERE log_type IS NOT NULL'+ const levelWhere = buildFacetWhere(search, 'level') || 'WHERE level IS NOT NULL'++ const sql = `+${getUnifiedLogsCTE(effectiveLogTypes)},++-- Single COUNTIF pass for all log_type buckets + total (no GROUP BY / sort needed)+log_type_counts AS (+ SELECT+ COUNT(*) AS total,+ COUNTIF(log_type = 'edge') AS edge_count,+ COUNTIF(log_type = 'postgrest') AS postgrest_count,+ COUNTIF(log_type = 'storage') AS storage_count,+ COUNTIF(log_type = 'postgres') AS postgres_count,+ COUNTIF(log_type = 'edge function') AS edge_function_count,+ COUNTIF(log_type = 'auth') AS auth_count+ FROM unified_logs+ ${logTypeWhere}+),++-- Single COUNTIF pass for all level buckets+level_counts AS (+ SELECT+ COUNTIF(level = 'success') AS success_count,+ COUNTIF(level = 'warning') AS warning_count,+ COUNTIF(level = 'error') AS error_count+ FROM unified_logs+ ${levelWhere}+),++-- Variable facets: open-ended values still need GROUP BY+${getFacetCountCTE({ search, facet: 'method' })},+${getFacetCountCTE({ search, facet: 'status' })},+${getFacetCountCTE({ search, facet: 'pathname' })}++SELECT 'total' AS dimension, 'all' AS value, total AS count FROM log_type_counts+UNION ALL SELECT 'log_type', 'edge', edge_count FROM log_type_counts+UNION ALL SELECT 'log_type', 'postgrest', postgrest_count FROM log_type_counts+UNION ALL SELECT 'log_type', 'storage', storage_count FROM log_type_counts+UNION ALL SELECT 'log_type', 'postgres', postgres_count FROM log_type_counts+UNION ALL SELECT 'log_type', 'edge function', edge_function_count FROM log_type_counts+UNION ALL SELECT 'log_type', 'auth', auth_count FROM log_type_counts+UNION ALL SELECT 'level', 'success', success_count FROM level_counts+UNION ALL SELECT 'level', 'warning', warning_count FROM level_counts+UNION ALL SELECT 'level', 'error', error_count FROM level_counts+UNION ALL SELECT dimension, value, count FROM method_count+UNION ALL SELECT dimension, value, count FROM status_count+UNION ALL SELECT dimension, value, count FROM pathname_count+`++ return sql+}++/**+ * Enhanced logs chart query with dynamic bucketing based on time range+ * Incorporates dynamic bucketing from the older implementation+ */+export const getLogsChartQuery = (search: QuerySearchParamsType): string => {+ const { finalWhere } = buildQueryConditions(search)+ const truncationLevel = calculateChartBucketing(search)+ const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]++ return `+${getUnifiedLogsCTE(effectiveLogTypes)}+SELECT+ TIMESTAMP_TRUNC(timestamp, ${truncationLevel}) as time_bucket,+ COUNTIF(level = 'success') as success,+ COUNTIF(level = 'warning') as warning,+ COUNTIF(level = 'error') as error,+ COUNT(*) as total_per_bucket+FROM unified_logs+${finalWhere}+GROUP BY time_bucket+ORDER BY time_bucket ASC+`+}diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.tsnew file mode 100644index 00000000000..b1b43e3e36a--- /dev/null+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts@@ -0,0 +1,137 @@+import { describe, expect, it } from 'vitest'++import {+ getFacetCountQuery,+ getLogsChartQuery,+ getLogsCountQuery,+ getUnifiedLogsQuery,+} from './UnifiedLogs.queries'++const baseSearch = {+ date: [new Date('2026-05-08T09:00:00Z'), new Date('2026-05-08T10:00:00Z')],+} as any++describe('UnifiedLogs.queries (OTEL flat)', () => {+ describe('getUnifiedLogsQuery', () => {+ it('defaults to postgres + postgrest log types when none specified', () => {+ const sql = getUnifiedLogsQuery(baseSearch)+ expect(sql).toContain(`source = 'postgres_logs'`)+ // postgrest = edge_logs filtered by /rest/ path+ expect(sql).toContain(+ `source = 'edge_logs' AND log_attributes['request.path'] LIKE '%/rest/%'`+ )+ })++ it('routes the `edge` log type to edge_logs without /rest/ or /storage/ paths', () => {+ const sql = getUnifiedLogsQuery({ ...baseSearch, log_type: ['edge'] } as any)+ expect(sql).toContain(`NOT LIKE '%/rest/%'`)+ expect(sql).toContain(`NOT LIKE '%/storage/%'`)+ const where = sql.split(/\bWHERE\b/)[1] ?? ''+ expect(where).not.toContain(`source = 'postgres_logs'`)+ })++ it('routes the `storage` log type to edge_logs filtered by /storage/', () => {+ const sql = getUnifiedLogsQuery({ ...baseSearch, log_type: ['storage'] } as any)+ expect(sql).toContain(+ `source = 'edge_logs' AND log_attributes['request.path'] LIKE '%/storage/%'`+ )+ })++ it('escapes single quotes in filter values to prevent SQL injection', () => {+ const sql = getUnifiedLogsQuery({+ ...baseSearch,+ method: [`G'ET`],+ pathname: `/customers'; DROP TABLE logs --`,+ } as any)+ // Single quotes are doubled (SQL-standard escaping) by pg-meta's+ // literal(); a raw single quote from user input never closes its+ // string literal early.+ expect(sql).toContain(`'G''ET'`)+ expect(sql).toContain(`%/customers''; DROP TABLE logs --%`)+ })++ it('translates method/status/pathname filters to log_attributes predicates', () => {+ const sql = getUnifiedLogsQuery({+ ...baseSearch,+ method: ['GET'],+ status: ['401'],+ pathname: '/customers',+ } as any)+ expect(sql).toContain(`log_attributes['request.method'] IN ('GET')`)+ expect(sql).toContain(`log_attributes['response.status_code'] IN ('401')`)+ expect(sql).toContain(`log_attributes['request.path'] LIKE '%/customers%'`)+ })++ it('does not emit subqueries or CTEs (rejected by the OTEL endpoint)', () => {+ const sql = getUnifiedLogsQuery(baseSearch)+ expect(sql).not.toMatch(/WITH\s+\w+\s+AS\s*\(/i)+ expect(sql).not.toMatch(/FROM\s*\(\s*SELECT/i)+ // Single SELECT * FROM logs (not "SELECT *" wildcard usage either).+ expect(sql).not.toMatch(/SELECT\s+\*/)+ })+ })++ describe('getLogsCountQuery', () => {+ it('emits one UNION ALL branch per log_type bucket and per level', () => {+ const sql = getLogsCountQuery(baseSearch)+ // Per-log-type counts+ for (const lt of ['edge', 'postgrest', 'storage', 'postgres', 'edge function', 'auth']) {+ expect(sql).toContain(`'${lt}'`)+ }+ // Per-level counts+ for (const lvl of ['success', 'warning', 'error']) {+ expect(sql).toContain(`'${lvl}'`)+ }+ // Bundled via UNION ALL — multiple occurrences expected+ expect(sql.match(/UNION ALL/g)?.length ?? 0).toBeGreaterThan(5)+ })++ it('honours an active log_type filter in the total count branch', () => {+ const sql = getLogsCountQuery({ ...baseSearch, log_type: ['edge'] } as any)+ // The first branch is the total — its WHERE must include the edge+ // log_type predicate, otherwise the total badge would over-count+ // when a log_type filter is active.+ const totalBranch = sql.split(/\bUNION ALL\b/)[0]+ expect(totalBranch).toContain(`'total'`)+ expect(totalBranch).toContain(`source = 'edge_logs'`)+ expect(totalBranch).not.toContain(`source = 'postgres_logs'`)+ })+ })++ describe('getLogsChartQuery', () => {+ it('uses minute bucketing for short ranges', () => {+ const sql = getLogsChartQuery(baseSearch)+ expect(sql).toContain('toStartOfMinute(timestamp)')+ })++ it('uses hour bucketing for ranges spanning more than 12 hours', () => {+ const sql = getLogsChartQuery({+ ...baseSearch,+ date: [new Date('2026-05-08T00:00:00Z'), new Date('2026-05-08T18:00:00Z')],+ } as any)+ expect(sql).toContain('toStartOfHour(timestamp)')+ })++ it('uses day bucketing for ranges spanning more than 2 days', () => {+ const sql = getLogsChartQuery({+ ...baseSearch,+ date: [new Date('2026-05-01T00:00:00Z'), new Date('2026-05-08T00:00:00Z')],+ } as any)+ expect(sql).toContain('toStartOfDay(timestamp)')+ })+ })++ describe('getFacetCountQuery', () => {+ it('groups by the requested facet and excludes that facet from the WHERE filters', () => {+ const sql = getFacetCountQuery({+ search: { ...baseSearch, method: ['GET'], status: ['200'] } as any,+ facet: 'method',+ })+ // Filtered facet is excluded from WHERE; other filters still applied+ expect(sql).not.toContain(`log_attributes['request.method'] IN ('GET')`)+ expect(sql).toContain(`log_attributes['response.status_code'] 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.tsindex 86e4de98f7b..543e36b0b44 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts@@ -1,8 +1,20 @@+import { literal } from '@supabase/pg-meta/src/pg-format'import dayjs from 'dayjs'import { DEFAULT_LOG_TYPES } from './UnifiedLogs.constants'import { QuerySearchParamsType, SearchParamsType } from './UnifiedLogs.types'+// Escapes a substring of a `LIKE '%...%'` clause so a value containing a+// single quote can't terminate the literal early. `literal()` would wrap+// the whole value in quotes, which we don't want when concatenating wild-+// card characters; this strips its quoting back off.+const likeValue = (value: unknown): string => {+ const escaped = literal(String(value))+ // literal() wraps strings as 'value' (and prefixes with E for backslash+ // values); strip the surrounding quotes since we glue this between %.+ return escaped.replace(/^E?'/, '').replace(/'$/, '')+}+// Pagination and control parametersconst PAGINATION_PARAMS = ['sort', 'start', 'size', 'uuid', 'cursor', 'direction', 'live'] as const@@ -13,395 +25,241 @@ const SPECIAL_FILTER_PARAMS = ['date'] as constconst EXCLUDED_QUERY_PARAMS = [...PAGINATION_PARAMS, ...SPECIAL_FILTER_PARAMS] as constconst FACET_FIELDS = ['log_type', 'level', 'method', 'status', 'pathname'] as const+// OTEL log_attributes keys for HTTP-style fields. Centralized so they can be+// adjusted in one place if the backend conventions change.+const ATTR = {+ method: `log_attributes['request.method']`,+ status: `log_attributes['response.status_code']`,+ path: `log_attributes['request.path']`,+} as const+/**- * Builds query conditions from search parameters and returns WHERE clause- * @param search SearchParamsType object containing query parameters- * @returns Object with whereConditions array and formatted WHERE clause+ * Predicate that matches rows belonging to a given log_type. Mirrors the+ * shape of the original BigQuery unified-logs CTEs: edge gateway traffic+ * (`source = 'edge_logs'`) is split between `edge`, `postgrest` and `storage`+ * based on URL path. Other types map straight to a single source.+ *+ * The OTEL `postgrest_logs` and `storage_logs` sources contain process-level+ * logs from postgREST / storage-api and are intentionally not part of unified+ * logs; the UI surfaces gateway HTTP traffic for those buckets.*/-const buildQueryConditions = (search: QuerySearchParamsType) => {- const whereConditions: string[] = []+const LOG_TYPE_PREDICATE: Record<string, string> = {+ edge: `source = 'edge_logs' AND ${ATTR.path} NOT LIKE '%/rest/%' AND ${ATTR.path} NOT LIKE '%/storage/%'`,+ postgrest: `source = 'edge_logs' AND ${ATTR.path} LIKE '%/rest/%'`,+ storage: `source = 'edge_logs' AND ${ATTR.path} LIKE '%/storage/%'`,+ postgres: `source = 'postgres_logs'`,+ 'edge function': `source = 'function_edge_logs'`,+ auth: `source = 'auth_logs'`,+}- // Process all search parameters for filtering- Object.entries(search).forEach(([key, value]) => {- // Skip pagination/control parameters- if ((EXCLUDED_QUERY_PARAMS as readonly string[]).includes(key)) {- return- }+// Derived `log_type` column for SELECT / GROUP BY / countIf use.+const LOG_TYPE_EXPR = `CASE+ WHEN source = 'edge_logs' AND ${ATTR.path} LIKE '%/rest/%' THEN 'postgrest'+ WHEN source = 'edge_logs' AND ${ATTR.path} LIKE '%/storage/%' THEN 'storage'+ WHEN source = 'edge_logs' THEN 'edge'+ WHEN source = 'postgres_logs' THEN 'postgres'+ WHEN source = 'function_edge_logs' THEN 'edge function'+ WHEN source = 'auth_logs' THEN 'auth'+ ELSE source+ END`++// 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.+const STATUS_EXPR = `CASE+ WHEN source = 'postgres_logs' THEN toString(log_attributes['parsed.sql_state_code'])+ ELSE toString(${ATTR.status})+ 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+// `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 = `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 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'+ ELSE 'success'+ END`++const logTypeWherePredicate = (logTypes: string[]) => {+ const effective = logTypes.filter((t) => t in LOG_TYPE_PREDICATE)+ const types = effective.length ? effective : [...DEFAULT_LOG_TYPES]+ return `(${types.map((t) => `(${LOG_TYPE_PREDICATE[t]})`).join(' OR ')})`+}- // Handle array filters (IN clause)- if (Array.isArray(value) && value.length > 0) {- whereConditions.push(- `${key} IN (${value.map((v) => `'${String(v).replace(/'/g, "''")}'`).join(',')})`- )- return- }+/**+ * Translates a frontend filter key/value pair into an underlying SQL predicate.+ * The OTEL endpoint won't accept queries that reference derived aliases like+ * `log_type` or `level` in WHERE for some shapes, so we always emit raw-column+ * predicates (source/severity_text/log_attributes[…]).+ */+const translateFilter = (key: string, value: unknown): string | null => {+ if (value === null || value === undefined) return null- // Handle scalar values- if (value !== null && value !== undefined) {- if (['host', 'pathname'].includes(key)) {- whereConditions.push(`${key} LIKE '%${String(value).replace(/'/g, "''")}%'`)- } else {- whereConditions.push(`${key} = '${String(value).replace(/'/g, "''")}'`)- }- }- })+ const arr = Array.isArray(value) ? (value.length > 0 ? value : null) : null+ if (Array.isArray(value) && !arr) return null- // Create final WHERE clause- const finalWhere = whereConditions.length > 0 ? `WHERE ${whereConditions.join(' AND ')}` : ''+ const inList = (values: readonly unknown[]) =>+ `(${values.map((v) => literal(String(v))).join(',')})`- return { whereConditions, finalWhere }+ switch (key) {+ case 'log_type': {+ const types = (arr ?? [value]).map((v) => String(v))+ return `(${types+ .map((t) => `(${LOG_TYPE_PREDICATE[t] ?? `source = ${literal(t)}`})`)+ .join(' OR ')})`+ }+ case 'level': {+ // No simple raw column for level; reference the inline CASE expression.+ const levels = arr ?? [value]+ return `(${LEVEL_EXPR}) IN ${inList(levels.map((v) => String(v)))}`+ }+ case 'method':+ return arr ? `${ATTR.method} IN ${inList(arr)}` : `${ATTR.method} = ${literal(String(value))}`+ case 'status':+ return arr ? `${ATTR.status} IN ${inList(arr)}` : `${ATTR.status} = ${literal(String(value))}`+ case 'pathname':+ return arr+ ? `(${arr.map((v) => `${ATTR.path} LIKE '%${likeValue(v)}%'`).join(' OR ')})`+ : `${ATTR.path} LIKE '%${likeValue(value)}%'`+ case 'host':+ // Best-effort: use full request URL since `host` isn't a top-level field.+ return arr+ ? `(${arr.map((v) => `log_attributes['request.url'] LIKE '%${likeValue(v)}%'`).join(' OR ')})`+ : `log_attributes['request.url'] LIKE '%${likeValue(value)}%'`+ default:+ // Unknown filter key — fall back to a generic equality on log_attributes.+ // We don't pass `key` through literal() because Map key access in+ // ClickHouse uses bracket syntax with a string literal; the existing+ // EXCLUDED_QUERY_PARAMS list and the typed search params surface this+ // from a static allow-list, not user input.+ return arr+ ? `log_attributes['${key}'] IN ${inList(arr)}`+ : `log_attributes['${key}'] = ${literal(String(value))}`+ }}/**- * Calculates how much the chart start datetime should be offset given the current datetime filter params- * and determines the appropriate bucketing level (minute, hour, day)- * Ported from the older implementation (apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts)+ * Builds an array of WHERE predicate strings from search params, optionally+ * skipping a specific facet field (used when computing faceted counts).+ * `log_type` is always handled separately (see `logTypeWherePredicate`).*/-const calculateChartBucketing = (search: SearchParamsType | Record<string, unknown>): string => {- // Extract start and end times from the date array if available+const buildPredicates = (search: QuerySearchParamsType, excludeField?: string) => {+ const predicates: string[] = []+ Object.entries(search).forEach(([key, value]) => {+ if (key === excludeField) return+ if (key === 'log_type') return+ if ((EXCLUDED_QUERY_PARAMS as readonly string[]).includes(key)) return+ const predicate = translateFilter(key, value)+ if (predicate) predicates.push(predicate)+ })+ return predicates+}++const whereClause = (predicates: string[]) =>+ predicates.length > 0 ? `WHERE ${predicates.join(' AND ')}` : ''++/**+ * Calculates the chart bucketing level (minute/hour/day) given the date range.+ */+const calculateChartBucketing = (+ search: SearchParamsType | Record<string, unknown>+): 'MINUTE' | 'HOUR' | 'DAY' => {const dateRange = (search.date as Array<Date | string | number | null | undefined>) || []- // Handle timestamps that could be in various formatsconst convertToMillis = (timestamp: Date | string | number | null | undefined) => {if (!timestamp) return null- // If timestamp is a Date objectif (timestamp instanceof Date) return timestamp.getTime()-- // If timestamp is a string that needs parsingif (typeof timestamp === 'string') return dayjs(timestamp).valueOf()-- // If timestamp is already a number (unix timestamp)- // Check if microseconds (16 digits) and convert to millisecondsif (typeof timestamp === 'number') {const str = timestamp.toString()if (str.length >= 16) return Math.floor(timestamp / 1000)return timestamp}-return null}let startMillis = convertToMillis(dateRange[0])let endMillis = convertToMillis(dateRange[1])- // Default values if not setif (!startMillis) startMillis = dayjs().subtract(1, 'hour').valueOf()if (!endMillis) endMillis = dayjs().valueOf()const startTime = dayjs(startMillis)const endTime = dayjs(endMillis)- let truncationLevel = 'MINUTE'-const hourDiff = endTime.diff(startTime, 'hour')const dayDiff = endTime.diff(startTime, 'day')- // Adjust bucketing based on time range- if (dayDiff >= 2) {- truncationLevel = 'DAY'- } else if (hourDiff >= 12) {- truncationLevel = 'HOUR'- } else {- truncationLevel = 'MINUTE'- }-- return truncationLevel-}--/**- * Edge logs query fragment- *- * excludes `/rest/` in the path- */-const getEdgeLogsQuery = () => {- return `- select- id,- null as source_id,- el.timestamp as timestamp,- 'edge' as log_type,- CAST(edge_logs_response.status_code AS STRING) as status,- CASE- WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN edge_logs_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- edge_logs_request.path as pathname,- null as event_message,- edge_logs_request.method as method,- null as log_count,- null as logs- from edge_logs as el- cross join unnest(metadata) as edge_logs_metadata- cross join unnest(edge_logs_metadata.request) as edge_logs_request- cross join unnest(edge_logs_metadata.response) as edge_logs_response-- -- ONLY include logs where the path does not include /rest/- WHERE edge_logs_request.path NOT LIKE '%/rest/%'- AND edge_logs_request.path NOT LIKE '%/storage/%'-- `-}--// Postgrest logs--// WHERE pathname includes `/rest/`-const getPostgrestLogsQuery = () => {- return `- select- id,- null as source_id,- el.timestamp as timestamp,- 'postgrest' as log_type,- CAST(edge_logs_response.status_code AS STRING) as status,- CASE- WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN edge_logs_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- edge_logs_request.path as pathname,- null as event_message,- edge_logs_request.method as method,- null as log_count,- null as logs- from edge_logs as el- cross join unnest(metadata) as edge_logs_metadata- cross join unnest(edge_logs_metadata.request) as edge_logs_request- cross join unnest(edge_logs_metadata.response) as edge_logs_response-- -- ONLY include logs where the path includes /rest/- WHERE edge_logs_request.path LIKE '%/rest/%'- `-}--/**- * Postgres logs query fragment- */-const getPostgresLogsQuery = () => {- return `- select- id,- null as source_id,- pgl.timestamp as timestamp,- 'postgres' as log_type,- CAST(pgl_parsed.sql_state_code AS STRING) as status,- CASE- WHEN pgl_parsed.error_severity = 'LOG' THEN 'success'- WHEN pgl_parsed.error_severity = 'WARNING' THEN 'warning'- WHEN pgl_parsed.error_severity = 'FATAL' THEN 'error'- WHEN pgl_parsed.error_severity = 'ERROR' THEN 'error'- ELSE null- END as level,- null as pathname,- event_message as event_message,- null as method,- null as log_count,- null as logs- from postgres_logs as pgl- cross join unnest(pgl.metadata) as pgl_metadata- cross join unnest(pgl_metadata.parsed) as pgl_parsed- `+ if (dayDiff >= 2) return 'DAY'+ if (hourDiff >= 12) return 'HOUR'+ return 'MINUTE'}-/**- * Edge function logs query fragment- */-const getEdgeFunctionLogsQuery = () => {- return `- select- id,- null as source_id,- fel.timestamp as timestamp,- 'edge function' as log_type,- CAST(fel_response.status_code AS STRING) as status,- CASE- WHEN fel_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN fel_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN fel_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- fel_request.pathname as pathname,- COALESCE(function_logs_agg.last_event_message, '') as event_message,- fel_request.method as method,- function_logs_agg.function_log_count as log_count,- null as logs- from function_edge_logs as fel- cross join unnest(metadata) as fel_metadata- cross join unnest(fel_metadata.response) as fel_response- cross join unnest(fel_metadata.request) as fel_request- left join (- SELECT- fl_metadata.request_id,- COUNT(fl.id) as function_log_count,- ANY_VALUE(fl.event_message) as last_event_message- FROM function_logs as fl- CROSS JOIN UNNEST(fl.metadata) as fl_metadata- WHERE fl_metadata.request_id IS NOT NULL- GROUP BY fl_metadata.request_id- ) as function_logs_agg on fel_metadata.request_id = function_logs_agg.request_id- `-}--/**- * Auth logs query fragment- */-const getAuthLogsQuery = () => {- return `- select- el_in_al.id as id,- al.id as source_id,- el_in_al.timestamp as timestamp,- 'auth' as log_type,- CAST(el_in_al_response.status_code AS STRING) as status,- CASE- WHEN el_in_al_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN el_in_al_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN el_in_al_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- el_in_al_request.path as pathname,- null as event_message,- el_in_al_request.method as method,- null as log_count,- null as logs- from auth_logs as al- cross join unnest(metadata) as al_metadata- left join (- edge_logs as el_in_al- cross join unnest (metadata) as el_in_al_metadata- cross join unnest (el_in_al_metadata.response) as el_in_al_response- cross join unnest (el_in_al_response.headers) as el_in_al_response_headers- cross join unnest (el_in_al_metadata.request) as el_in_al_request- )- on al_metadata.request_id = el_in_al_response_headers.cf_ray- WHERE al_metadata.request_id is not null- `-}--/**- * Supabase storage logs query fragment- */-const getSupabaseStorageLogsQuery = () => {- return `- select- id,- null as source_id,- el.timestamp as timestamp,- 'storage' as log_type,- CAST(edge_logs_response.status_code AS STRING) as status,- CASE- WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN edge_logs_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- edge_logs_request.path as pathname,- null as event_message,- edge_logs_request.method as method,- null as log_count,- null as logs- from edge_logs as el- cross join unnest(metadata) as edge_logs_metadata- cross join unnest(edge_logs_metadata.request) as edge_logs_request- cross join unnest(edge_logs_metadata.response) as edge_logs_response- -- ONLY include logs where the path includes /storage/- WHERE edge_logs_request.path LIKE '%/storage/%'- `-}--const LOG_TYPE_QUERIES: Record<string, () => string> = {- edge: getEdgeLogsQuery,- postgrest: getPostgrestLogsQuery,- postgres: getPostgresLogsQuery,- 'edge function': getEdgeFunctionLogsQuery,- auth: getAuthLogsQuery,- storage: getSupabaseStorageLogsQuery,-}--/**- * Combine the requested log sources to create the unified logs CTE.- * Defaults to postgres + postgrest on first load to reduce query cost.- */-export const getUnifiedLogsCTE = (logTypes: string[] = [...DEFAULT_LOG_TYPES]) => {- const queries = logTypes- .filter((type) => type in LOG_TYPE_QUERIES)- .map((type) => LOG_TYPE_QUERIES[type]())-- const effectiveQueries =- queries.length > 0 ? queries : DEFAULT_LOG_TYPES.map((type) => LOG_TYPE_QUERIES[type]())-- return `-WITH unified_logs AS (- ${effectiveQueries.join('\n union all\n ')}-)- `+const truncationFunction = (level: 'MINUTE' | 'HOUR' | 'DAY') => {+ switch (level) {+ case 'DAY':+ return 'toStartOfDay'+ case 'HOUR':+ return 'toStartOfHour'+ case 'MINUTE':+ default:+ return 'toStartOfMinute'+ }}/**- * Unified logs SQL query+ * Returns the projection list for a unified-logs row. All derivations are+ * inlined so the result can be referenced (or filtered) at the same query+ * level — the OTEL endpoint rejects subqueries.*/-export const getUnifiedLogsQuery = (search: QuerySearchParamsType): string => {- const { finalWhere } = buildQueryConditions(search)- const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]-- const sql = `-${getUnifiedLogsCTE(effectiveLogTypes)}-SELECT+const rowProjection = () => `id,- source_id,+ null AS source_id,timestamp,- log_type,- status,- level,- pathname,+ ${LOG_TYPE_EXPR} AS log_type,+ ${STATUS_EXPR} AS status,+ ${LEVEL_EXPR} AS level,+ ${ATTR.path} AS pathname,event_message,- method,- log_count,- logs-FROM unified_logs-${finalWhere}+ ${ATTR.method} AS method,+ null AS log_count,+ null AS logs`- return sql+const buildBaseWhere = (search: QuerySearchParamsType, excludeField?: string) => {+ const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]+ const parts: string[] = []+ if (excludeField !== 'log_type') {+ parts.push(logTypeWherePredicate(effectiveLogTypes))+ }+ parts.push(...buildPredicates(search, excludeField))+ return parts}/**- * Get a count query for the total logs within the timeframe- * Uses proper faceted search behavior where facets show "what would I get if I selected ONLY this option"+ * Unified logs row query — flat SELECT, no subquery wrapper.*/--// Helper function to build WHERE clause excluding a specific field-const buildFacetWhere = (search: QuerySearchParamsType, excludeField: string): string => {- const conditions: string[] = []-- Object.entries(search).forEach(([key, value]) => {- if (key === excludeField) return // Skip the field we're getting facets for- if ((EXCLUDED_QUERY_PARAMS as readonly string[]).includes(key)) return // Skip pagination and special params-- // Handle array filters (IN clause)- if (Array.isArray(value) && value.length > 0) {- conditions.push(- `${key} IN (${value.map((v) => `'${String(v).replace(/'/g, "''")}'`).join(',')})`- )- return- }-- // Handle scalar values- if (value !== null && value !== undefined) {- if (['host', 'pathname'].includes(key)) {- conditions.push(`${key} LIKE '%${String(value).replace(/'/g, "''")}%'`)- } else {- conditions.push(`${key} = '${String(value).replace(/'/g, "''")}'`)- }- }- })-- return conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''+export const getUnifiedLogsQuery = (search: QuerySearchParamsType): string => {+ const predicates = buildBaseWhere(search)+ return `+SELECT ${rowProjection()}+FROM logs+${whereClause(predicates)}+`}-export const getFacetCountCTE = ({+/**+ * Single-facet count query — a complete flat SELECT with GROUP BY.+ */+export const getFacetCountQuery = ({search,facet,facetSearch,@@ -416,188 +274,97 @@ export const getFacetCountCTE = ({const MAX_FACETS_QUANTITY = 20+ const facetExpr =+ facet === 'log_type'+ ? LOG_TYPE_EXPR+ : facet === 'level'+ ? LEVEL_EXPR+ : facet === 'method'+ ? ATTR.method+ : facet === 'status'+ ? STATUS_EXPR+ : facet === 'pathname'+ ? ATTR.path+ : `log_attributes['${facet}']`++ const predicates = [+ ...buildBaseWhere(search, facet),+ `(${facetExpr}) IS NOT NULL AND (${facetExpr}) != ''`,+ ]+ if (facetSearch) {+ predicates.push(`(${facetExpr}) LIKE '%${likeValue(facetSearch)}%'`)+ }+return `-${facet}_count AS (- SELECT '${facet}' as dimension, ${facet} as value, COUNT(*) as count- FROM unified_logs- ${buildFacetWhere(search, `${facet}`) || `WHERE ${facet} IS NOT NULL`}- ${buildFacetWhere(search, `${facet}`) ? ` AND ${facet} IS NOT NULL` : ''}- ${!!facetSearch ? `AND ${facet} LIKE '%${facetSearch.replace(/'/g, "''")}%'` : ''}- GROUP BY ${facet}- LIMIT ${MAX_FACETS_QUANTITY}-)+SELECT '${facet}' AS dimension, (${facetExpr}) AS value, count() AS count+FROM logs+${whereClause(predicates)}+GROUP BY value+LIMIT ${MAX_FACETS_QUANTITY}`.trim()}-export const getUnifiedLogsCountCTE = () => {- return `-WITH unified_logs AS (- -- Single scan of edge_logs covering edge gateway, postgrest, and storage- select- id,- CASE- WHEN edge_logs_request.path LIKE '%/rest/%' THEN 'postgrest'- WHEN edge_logs_request.path LIKE '%/storage/%' THEN 'storage'- ELSE 'edge'- END as log_type,- CAST(edge_logs_response.status_code AS STRING) as status,- CASE- WHEN edge_logs_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN edge_logs_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN edge_logs_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- edge_logs_request.path as pathname,- edge_logs_request.method as method- from edge_logs as el- cross join unnest(metadata) as edge_logs_metadata- cross join unnest(edge_logs_metadata.request) as edge_logs_request- cross join unnest(edge_logs_metadata.response) as edge_logs_response-- union all-- -- Postgres logs- select- id,- 'postgres' as log_type,- CAST(pgl_parsed.sql_state_code AS STRING) as status,- CASE- WHEN pgl_parsed.error_severity = 'LOG' THEN 'success'- WHEN pgl_parsed.error_severity = 'WARNING' THEN 'warning'- WHEN pgl_parsed.error_severity = 'FATAL' THEN 'error'- WHEN pgl_parsed.error_severity = 'ERROR' THEN 'error'- ELSE null- END as level,- null as pathname,- null as method- from postgres_logs as pgl- cross join unnest(pgl.metadata) as pgl_metadata- cross join unnest(pgl_metadata.parsed) as pgl_parsed-- union all-- -- Edge function logs- select- fel.id,- 'edge function' as log_type,- CAST(fel_response.status_code AS STRING) as status,- CASE- WHEN fel_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN fel_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN fel_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- fel_request.pathname as pathname,- fel_request.method as method- from function_edge_logs as fel- cross join unnest(metadata) as fel_metadata- cross join unnest(fel_metadata.response) as fel_response- cross join unnest(fel_metadata.request) as fel_request-- union all-- -- Auth logs- select- el_in_al.id as id,- 'auth' as log_type,- CAST(el_in_al_response.status_code AS STRING) as status,- CASE- WHEN el_in_al_response.status_code BETWEEN 200 AND 299 THEN 'success'- WHEN el_in_al_response.status_code BETWEEN 400 AND 499 THEN 'warning'- WHEN el_in_al_response.status_code >= 500 THEN 'error'- ELSE 'success'- END as level,- el_in_al_request.path as pathname,- el_in_al_request.method as method- from auth_logs as al- cross join unnest(metadata) as al_metadata- left join (- edge_logs as el_in_al- cross join unnest(metadata) as el_in_al_metadata- cross join unnest(el_in_al_metadata.response) as el_in_al_response- cross join unnest(el_in_al_response.headers) as el_in_al_response_headers- cross join unnest(el_in_al_metadata.request) as el_in_al_request+/**+ * Bundled count query — UNION ALL of (dimension, value, count) rows so the+ * frontend can render facet counts and total in one round trip.+ */+export const getLogsCountQuery = (search: QuerySearchParamsType): string => {+ const baseFiltersFor = (excludeField?: string) =>+ buildBaseWhere(search, excludeField).join(' AND ')++ // The "total" badge should reflect the user's *current* filter set,+ // including any active log_type filter. Pass no excludeField so the+ // log_type predicate is included.+ const totalSql = `+SELECT 'total' AS dimension, 'all' AS value, count() AS count+FROM logs+WHERE ${baseFiltersFor()}+`.trim()++ const logTypeBranches = Object.entries(LOG_TYPE_PREDICATE)+ .map(([logType, predicate]) =>+ `+SELECT 'log_type' AS dimension, '${logType}' AS value, countIf(${predicate}) AS count+FROM logs+WHERE ${baseFiltersFor('log_type')}+`.trim())- on al_metadata.request_id = el_in_al_response_headers.cf_ray- WHERE al_metadata.request_id is not null-)- `-}+ .join('\nUNION ALL\n')++ const levelBranches = ['success', 'warning', 'error']+ .map((lvl) =>+ `+SELECT 'level' AS dimension, '${lvl}' AS value, countIf((${LEVEL_EXPR}) = '${lvl}') AS count+FROM logs+WHERE ${baseFiltersFor('level')}+`.trim()+ )+ .join('\nUNION ALL\n')-export const getLogsCountQuery = (search: QuerySearchParamsType): string => {- const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]- const logTypeWhere = buildFacetWhere(search, 'log_type') || 'WHERE log_type IS NOT NULL'- const levelWhere = buildFacetWhere(search, 'level') || 'WHERE level IS NOT NULL'-- const sql = `-${getUnifiedLogsCTE(effectiveLogTypes)},---- Single COUNTIF pass for all log_type buckets + total (no GROUP BY / sort needed)-log_type_counts AS (- SELECT- COUNT(*) AS total,- COUNTIF(log_type = 'edge') AS edge_count,- COUNTIF(log_type = 'postgrest') AS postgrest_count,- COUNTIF(log_type = 'storage') AS storage_count,- COUNTIF(log_type = 'postgres') AS postgres_count,- COUNTIF(log_type = 'edge function') AS edge_function_count,- COUNTIF(log_type = 'auth') AS auth_count- FROM unified_logs- ${logTypeWhere}-),---- Single COUNTIF pass for all level buckets-level_counts AS (- SELECT- COUNTIF(level = 'success') AS success_count,- COUNTIF(level = 'warning') AS warning_count,- COUNTIF(level = 'error') AS error_count- FROM unified_logs- ${levelWhere}-),---- Variable facets: open-ended values still need GROUP BY-${getFacetCountCTE({ search, facet: 'method' })},-${getFacetCountCTE({ search, facet: 'status' })},-${getFacetCountCTE({ search, facet: 'pathname' })}--SELECT 'total' AS dimension, 'all' AS value, total AS count FROM log_type_counts-UNION ALL SELECT 'log_type', 'edge', edge_count FROM log_type_counts-UNION ALL SELECT 'log_type', 'postgrest', postgrest_count FROM log_type_counts-UNION ALL SELECT 'log_type', 'storage', storage_count FROM log_type_counts-UNION ALL SELECT 'log_type', 'postgres', postgres_count FROM log_type_counts-UNION ALL SELECT 'log_type', 'edge function', edge_function_count FROM log_type_counts-UNION ALL SELECT 'log_type', 'auth', auth_count FROM log_type_counts-UNION ALL SELECT 'level', 'success', success_count FROM level_counts-UNION ALL SELECT 'level', 'warning', warning_count FROM level_counts-UNION ALL SELECT 'level', 'error', error_count FROM level_counts-UNION ALL SELECT dimension, value, count FROM method_count-UNION ALL SELECT dimension, value, count FROM status_count-UNION ALL SELECT dimension, value, count FROM pathname_count-`+ const facetBranches = ['method', 'status', 'pathname']+ .map((facet) => getFacetCountQuery({ search, facet }))+ .join('\nUNION ALL\n')- return sql+ return [totalSql, logTypeBranches, levelBranches, facetBranches].join('\nUNION ALL\n')}/**- * Enhanced logs chart query with dynamic bucketing based on time range- * Incorporates dynamic bucketing from the older implementation+ * Logs chart query with dynamic bucketing based on time range.*/export const getLogsChartQuery = (search: QuerySearchParamsType): string => {- const { finalWhere } = buildQueryConditions(search)const truncationLevel = calculateChartBucketing(search)- const effectiveLogTypes = search.log_type?.length ? search.log_type : [...DEFAULT_LOG_TYPES]+ const truncFn = truncationFunction(truncationLevel)+ const predicates = buildBaseWhere(search)return `-${getUnifiedLogsCTE(effectiveLogTypes)}SELECT- TIMESTAMP_TRUNC(timestamp, ${truncationLevel}) as time_bucket,- COUNTIF(level = 'success') as success,- COUNTIF(level = 'warning') as warning,- COUNTIF(level = 'error') as error,- COUNT(*) as total_per_bucket-FROM unified_logs-${finalWhere}+ ${truncFn}(timestamp) AS time_bucket,+ countIf((${LEVEL_EXPR}) = 'success') AS success,+ countIf((${LEVEL_EXPR}) = 'warning') AS warning,+ countIf((${LEVEL_EXPR}) = 'error') AS error,+ count() AS total_per_bucket+FROM logs+${whereClause(predicates)}GROUP BY time_bucketORDER BY time_bucket ASC`diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.tsindex 7df87e2356a..bf9f0455b91 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts@@ -37,6 +37,21 @@ export const getFacetedMinMaxValues = <TData>(facets?: Record<string, FacetMetad}}+/**+ * Returns a unified-logs row's timestamp in epoch milliseconds.+ *+ * The row mapper attaches a pre-parsed `date` (works for both BigQuery+ * microsecond timestamps and OTEL ISO strings); fall back to the raw+ * `timestamp` value when it's a number (older BQ-style microseconds).+ */+export function getRowTimestampMs(+ row: { date?: Date | null; timestamp?: number | string | null } | null | undefined+): number | null {+ if (row?.date instanceof Date) return row.date.getTime()+ if (typeof row?.timestamp === 'number') return row.timestamp / 1000+ return null+}+export const getLevelLabel = (value: (typeof LEVELS)[number]): string => {switch (value) {case 'success':diff --git a/apps/studio/data/logs/get-unified-logs.ts b/apps/studio/data/logs/get-unified-logs.tsindex 65c2cf9834a..ef077ae76f6 100644--- a/apps/studio/data/logs/get-unified-logs.ts+++ b/apps/studio/data/logs/get-unified-logs.ts@@ -1,8 +1,11 @@import { useMutation } from '@tanstack/react-query'+import { useFlag } from 'common'import { toast } from 'sonner'+import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'import { getUnifiedLogsISOStartEnd } from './unified-logs-infinite-query'import { getUnifiedLogsQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'+import { getUnifiedLogsQuery as getUnifiedLogsQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'import { QuerySearchParamsType } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'import { handleError, post } from '@/data/fetchers'import type { ResponseError, UseCustomMutationOptions } from '@/types'@@ -12,6 +15,7 @@ export type getUnifiedLogsVariables = {search: QuerySearchParamsTypelimit: numberhoursAgo?: number+ useOtel?: boolean}// [Joshen] Mainly for retrieving logs on demand for downloading@@ -20,14 +24,17 @@ export async function retrieveUnifiedLogs({search,limit,hoursAgo,+ useOtel = false,}: getUnifiedLogsVariables) {if (typeof projectRef === 'undefined')throw new Error('projectRef is required for retrieveUnifiedLogs')const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search, hoursAgo)- const sql = `${getUnifiedLogsQuery(search)} ORDER BY timestamp DESC, id DESC LIMIT ${limit}`+ const buildQuery = pickLogsQueryBuilder(useOtel, getUnifiedLogsQuery, getUnifiedLogsQueryBq)+ const sql = `${buildQuery(search)} ORDER BY timestamp DESC, id DESC LIMIT ${limit}`- const { data, error } = await post(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {+ const endpoint = logsAllEndpointUrl(useOtel)+ const { data, error } = await post(endpoint, {params: { path: { ref: projectRef } },body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: isoTimestampEnd, sql },})@@ -37,7 +44,11 @@ export async function retrieveUnifiedLogs({const resultData = data?.result ?? []const result = resultData.map((row: any) => {- const date = new Date(Number(row.timestamp) / 1000)+ const ts = String(row.timestamp ?? '')+ const looksLikeIso = /[T-]/.test(ts)+ const date = looksLikeIso+ ? new Date(/Z$|[+-]\d{2}:?\d{2}$/.test(ts) ? ts : `${ts}Z`)+ : new Date(Number(ts) / 1000)return {id: row.id,date,@@ -72,8 +83,9 @@ export const useGetUnifiedLogsMutation = ({UseCustomMutationOptions<LogDrainCreateData, ResponseError, getUnifiedLogsVariables>,'mutationFn'> = {}) => {+ const useOtel = useFlag('otelUnifiedLogs')return useMutation<LogDrainCreateData, ResponseError, getUnifiedLogsVariables>({- mutationFn: (vars) => retrieveUnifiedLogs(vars),+ mutationFn: (vars) => retrieveUnifiedLogs({ ...vars, useOtel: vars.useOtel ?? useOtel }),async onSuccess(data, variables, context) {await onSuccess?.(data, variables, context)},diff --git a/apps/studio/data/logs/logs-endpoint.ts b/apps/studio/data/logs/logs-endpoint.tsnew file mode 100644index 00000000000..b3f73a08734--- /dev/null+++ b/apps/studio/data/logs/logs-endpoint.ts@@ -0,0 +1,11 @@+export const logsAllEndpointUrl = (useOtel: boolean) =>+ useOtel+ ? ('/platform/projects/{ref}/analytics/endpoints/logs.all.otel' as const)+ : ('/platform/projects/{ref}/analytics/endpoints/logs.all' as const)++/**+ * Convenience picker for a (bq, otel) pair. Preserves the input type so+ * callers keep the original signature.+ */+export const pickLogsQueryBuilder = <T>(useOtel: boolean, otel: T, bq: T): T =>+ useOtel ? otel : bqdiff --git a/apps/studio/data/logs/otel-inspection.utils.test.ts b/apps/studio/data/logs/otel-inspection.utils.test.tsnew file mode 100644index 00000000000..458d6745c20--- /dev/null+++ b/apps/studio/data/logs/otel-inspection.utils.test.ts@@ -0,0 +1,163 @@+import { describe, expect, it } from 'vitest'++import {+ aggregateFunctionLogs,+ flattenOtelInspectionRow,+ type OtelLogRow,+} from './otel-inspection.utils'++const edgeRow: OtelLogRow = {+ id: 'a1f2ba98-5701-4044-aed8-5dbcd36256f5',+ timestamp: '2026-05-08T17:42:31.351000',+ source: 'edge_logs',+ event_message: 'GET | 401 | https://example.supabase.red/rest/v1/customers',+ log_attributes: {+ 'request.method': 'GET',+ 'request.path': '/rest/v1/customers',+ 'request.host': 'example.supabase.red',+ 'request.url': 'https://example.supabase.red/rest/v1/customers?select=id',+ 'request.headers.user_agent': 'pg_net/0.8.0',+ 'request.headers.cf_ray': '9f7f5511fcfece4b',+ 'request.headers.cf_connecting_ip': '2406:da18::1',+ 'request.cf.country': 'SG',+ 'request.cf.city': 'Singapore',+ 'request.cf.colo': 'SIN',+ 'request.cf.httpProtocol': 'HTTP/2',+ 'response.status_code': '401',+ 'response.headers.content_type': 'application/json;charset=UTF-8',+ },+}++const fnEdgeRow: OtelLogRow = {+ id: 'cf7fbfd7-a23f-4cc5-a526-6fef4c5e47f9',+ timestamp: '2026-05-08T16:59:40.000000',+ source: 'function_edge_logs',+ event_message: 'POST | 401 | /functions/v1/hello-world',+ log_attributes: {+ 'request.method': 'POST',+ 'request.pathname': '/functions/v1/hello-world',+ 'request.host': 'example.supabase.red',+ 'request.url': 'https://example.supabase.red/functions/v1/hello-world',+ 'response.status_code': '401',+ execution_id: '1221d106-cc1c-463a-a42a-3decf6c52884',+ function_id: '0a47f8f5-afce-485a-8705-6e1f71c6784a',+ deployment_id: 'deploy-123',+ execution_time_ms: '258',+ 'response.headers.x_sb_edge_region': 'ap-southeast-1',+ },+}++const pgRow: OtelLogRow = {+ id: '972f990f-2d81-4473-99c5-b78b8943ea71',+ timestamp: '2026-05-08T17:58:53.121000',+ source: 'postgres_logs',+ event_message: 'connection authorized: user=postgres database=postgres',+ log_attributes: {+ 'parsed.error_severity': 'LOG',+ 'parsed.process_id': '848',+ 'parsed.session_id': '69399f78.350',+ 'parsed.session_start_time': '2025-12-10 16:27:36 UTC',+ 'parsed.transaction_id': '0',+ 'parsed.virtual_transaction_id': '3/0',+ 'parsed.sql_state_code': '00000',+ },+}++describe('flattenOtelInspectionRow', () => {+ it('maps edge_logs request/CF/headers attributes to underscored aliases', () => {+ const e = flattenOtelInspectionRow(edgeRow) as unknown as Record<string, unknown>+ expect(e.request_method).toBe('GET')+ expect(e.request_path).toBe('/rest/v1/customers')+ expect(e.request_host).toBe('example.supabase.red')+ expect(e.headers_user_agent).toBe('pg_net/0.8.0')+ expect(e.cf_ray).toBe('9f7f5511fcfece4b')+ expect(e.cf_country).toBe('SG')+ expect(e.client_city).toBe('Singapore')+ expect(e.network_protocol).toBe('HTTP/2')+ expect(e.network_datacenter).toBe('SIN')+ expect(e.client_ip).toBe('2406:da18::1')+ expect(e.status_code).toBe('401')+ expect(e.level).toBe('warning')+ })++ it('coalesces request.path / request.pathname for function_edge_logs', () => {+ const e = flattenOtelInspectionRow(fnEdgeRow) as unknown as Record<string, unknown>+ expect(e.request_path).toBe('/functions/v1/hello-world')+ expect(e.path).toBe('/functions/v1/hello-world')+ })++ it('exposes edge function specific fields', () => {+ const e = flattenOtelInspectionRow(fnEdgeRow) as unknown as Record<string, unknown>+ expect(e.execution_id).toBe('1221d106-cc1c-463a-a42a-3decf6c52884')+ expect(e.function_id).toBe('0a47f8f5-afce-485a-8705-6e1f71c6784a')+ expect(e.execution_time_ms).toBe('258')+ expect(e.execution_region).toBe('ap-southeast-1')+ })++ it('maps postgres parsed.* attributes and derives level from severity', () => {+ const e = flattenOtelInspectionRow(pgRow) as unknown as Record<string, unknown>+ expect(e.process_id).toBe('848')+ expect(e.session_id).toBe('69399f78.350')+ expect(e.transaction_id).toBe('0')+ expect(e.error_severity).toBe('LOG')+ expect(e.sql_state_code).toBe('00000')+ expect(e.level).toBe('success')+ })++ it('derives level=error for 5xx responses', () => {+ const row: OtelLogRow = {+ ...edgeRow,+ log_attributes: { ...edgeRow.log_attributes, 'response.status_code': '503' },+ }+ expect((flattenOtelInspectionRow(row) as unknown as Record<string, unknown>).level).toBe(+ 'error'+ )+ })++ it('falls back gracefully when log_attributes is missing', () => {+ const row: OtelLogRow = {+ id: 'x',+ timestamp: '2026-01-01T00:00:00Z',+ source: 'edge_logs',+ event_message: 'hi',+ }+ const e = flattenOtelInspectionRow(row) as unknown as Record<string, unknown>+ expect(e.id).toBe('x')+ expect(e.method).toBe('')+ expect(e.path).toBe('')+ expect(e.cf_country).toBeNull()+ })+})++describe('aggregateFunctionLogs', () => {+ it('returns zero counts for an empty result set', () => {+ const agg = aggregateFunctionLogs([])+ expect(agg.function_log_count).toBe(0)+ expect(agg.last_event_message).toBeNull()+ expect(agg.function_logs).toEqual([])+ })++ it('aggregates row count and exposes a sample event message + log entries', () => {+ const rows: OtelLogRow[] = [+ {+ id: '1',+ timestamp: '2026-05-08T17:00:00Z',+ source: 'function_logs',+ event_message: 'first',+ log_attributes: { event_type: 'Log', level: 'log' },+ },+ {+ id: '2',+ timestamp: '2026-05-08T17:00:01Z',+ source: 'function_logs',+ event_message: 'second',+ log_attributes: { event_type: 'Shutdown', level: 'log' },+ },+ ]+ const agg = aggregateFunctionLogs(rows)+ expect(agg.function_log_count).toBe(2)+ expect(agg.last_event_message).toBe('first')+ expect(agg.function_logs).toHaveLength(2)+ expect(agg.function_logs[0]).toMatchObject({ id: '1', event_type: 'Log', level: 'log' })+ })+})diff --git a/apps/studio/data/logs/otel-inspection.utils.ts b/apps/studio/data/logs/otel-inspection.utils.tsnew file mode 100644index 00000000000..71b78bc2281--- /dev/null+++ b/apps/studio/data/logs/otel-inspection.utils.ts@@ -0,0 +1,158 @@+import type { UnifiedLogInspectionEntry } from './unified-log-inspection-query'+import { LEVELS } from '@/components/ui/DataTable/DataTable.constants'++type Level = (typeof LEVELS)[number]++export type OtelLogRow = {+ id: string+ timestamp: string+ source: string+ event_message: string+ severity_text?: string+ log_attributes?: Record<string, any>+}++const httpStatusToLevel = (status: number): Level => {+ if (status >= 500) return 'error'+ if (status >= 400) return 'warning'+ return 'success'+}++const pgSeverityToLevel = (severity: string): Level => {+ switch (severity) {+ case 'WARNING':+ return 'warning'+ case 'ERROR':+ case 'FATAL':+ case 'CRITICAL':+ case 'PANIC':+ return 'error'+ default:+ return 'success'+ }+}++// Flattens a single OTEL row into the underscored field shape the service-flow+// panel components expect. Per-source `log_attributes` keys vary; unknown fields+// fall through to `null`.+export function flattenOtelInspectionRow(+ row: OtelLogRow+): UnifiedLogInspectionEntry & Record<string, unknown> {+ const attrs: Record<string, any> = row.log_attributes ?? {}++ // Path key differs across sources: edge_logs uses `request.path`,+ // function_edge_logs uses `request.pathname`. Coalesce.+ const requestPath = attrs['request.path'] ?? attrs['request.pathname']+ // Status: HTTP response code for gateway rows, Postgres SQL state code for postgres rows.+ const status =+ row.source === 'postgres_logs' ? attrs['parsed.sql_state_code'] : attrs['response.status_code']+ const statusNum = Number(status)++ // Derive level from HTTP status first, then postgres severity, then severity_text.+ let level = ''+ if (Number.isFinite(statusNum) && statusNum > 0) {+ level = httpStatusToLevel(statusNum)+ } else if (attrs['parsed.error_severity']) {+ level = pgSeverityToLevel(String(attrs['parsed.error_severity']))+ } else if (row.severity_text) {+ level = String(row.severity_text).toLowerCase()+ }++ return {+ // Spread raw attribute keys verbatim -- the OTEL dotted keys+ // (e.g. `request.path`) match some `enrichedData[...]` consumers.+ ...attrs,++ // Identity+ id: row.id,+ timestamp: row.timestamp,+ service_name: row.source ?? '',+ level,++ // Network -- flat aliases the panel reads as `enrichedData.request_path` etc.+ method: attrs['request.method'] ?? '',+ path: requestPath ?? '',+ host: attrs['request.host'] ?? '',+ status_code: status != null ? String(status) : '',+ request_path: requestPath ?? null,+ request_host: attrs['request.host'] ?? null,+ request_method: attrs['request.method'] ?? null,+ request_url: attrs['request.url'] ?? null,+ request_search: attrs['request.search'] ?? null,++ // Response+ response_origin_time: attrs['response.origin_time'] ?? null,+ response_content_type: attrs['response.headers.content_type'] ?? null,+ response_cache_status: attrs['response.headers.cf_cache_status'] ?? null,++ // Request headers+ headers_user_agent: attrs['request.headers.user_agent'] ?? null,+ headers_x_client_info: attrs['request.headers.x_client_info'] ?? null,+ headers_x_forwarded_proto: attrs['request.headers.x_forwarded_proto'] ?? null,+ headers_x_real_ip: attrs['request.headers.x_real_ip'] ?? null,+ headers_referer: attrs['request.headers.referer'] ?? null,++ // Cloudflare network info (also exposed under `client_*` for the panel)+ cf_ray: attrs['request.headers.cf_ray'] ?? null,+ cf_country: attrs['request.cf.country'] ?? attrs['request.headers.cf_ipcountry'] ?? null,+ cf_datacenter: attrs['request.cf.colo'] ?? null,+ client_ip:+ attrs['request.headers.cf_connecting_ip'] ?? attrs['request.headers.x_real_ip'] ?? null,+ client_continent: attrs['request.cf.continent'] ?? null,+ client_country: attrs['request.cf.country'] ?? null,+ client_city: attrs['request.cf.city'] ?? null,+ client_region: attrs['request.cf.region'] ?? null,+ client_region_code: attrs['request.cf.regionCode'] ?? null,+ client_latitude: attrs['request.cf.latitude'] ?? null,+ client_longitude: attrs['request.cf.longitude'] ?? null,+ client_timezone: attrs['request.cf.timezone'] ?? null,+ network_protocol: attrs['request.cf.httpProtocol'] ?? null,+ network_datacenter: attrs['request.cf.colo'] ?? null,++ // Edge function specific+ execution_id: attrs['execution_id'] ?? null,+ function_id: attrs['function_id'] ?? null,+ deployment_id: attrs['deployment_id'] ?? null,+ execution_time_ms: attrs['execution_time_ms'] ?? null,+ execution_region: attrs['response.headers.x_sb_edge_region'] ?? null,++ // Postgres specific (parsed.* keys)+ backend_type: attrs['parsed.backend_type'] ?? null,+ command_tag: attrs['parsed.command_tag'] ?? null,+ connection_from: attrs['parsed.connection_from'] ?? null,+ database_name: attrs['parsed.database_name'] ?? null,+ database_user: attrs['parsed.user_name'] ?? null,+ process_id: attrs['parsed.process_id'] ?? null,+ query_id: attrs['parsed.query_id'] ?? null,+ session_id: attrs['parsed.session_id'] ?? null,+ session_start_time: attrs['parsed.session_start_time'] ?? null,+ transaction_id: attrs['parsed.transaction_id'] ?? null,+ virtual_transaction_id: attrs['parsed.virtual_transaction_id'] ?? null,+ error_severity: attrs['parsed.error_severity'] ?? null,+ sql_state_code: attrs['parsed.sql_state_code'] ?? null,+ event_message: row.event_message,++ raw_log_data: row,+ service_specific_data: {},+ } as UnifiedLogInspectionEntry & Record<string, unknown>+}++// Aggregates `function_logs` rows into the count + sample fields the+// edge-function service-flow panel reads.+export function aggregateFunctionLogs(rows: OtelLogRow[]) {+ if (rows.length === 0) {+ return { function_log_count: 0, last_event_message: null, function_logs: [] }+ }+ return {+ function_log_count: rows.length,+ // BQ used ANY_VALUE; pick the most recent message for a more useful summary.+ last_event_message: rows[0]?.event_message ?? null,+ function_logs: rows.map((r) => ({+ id: r.id,+ timestamp: r.timestamp,+ event_message: r.event_message,+ level: r.severity_text ?? r.log_attributes?.level ?? null,+ event_type: r.log_attributes?.event_type ?? null,+ })),+ }+}diff --git a/apps/studio/data/logs/unified-log-inspection-query.ts b/apps/studio/data/logs/unified-log-inspection-query.tsindex b47772b13d1..96553c8aa82 100644--- a/apps/studio/data/logs/unified-log-inspection-query.ts+++ b/apps/studio/data/logs/unified-log-inspection-query.ts@@ -1,6 +1,13 @@+import { literal, safeSql } from '@supabase/pg-meta/src/pg-format'import { useQuery } from '@tanstack/react-query'+import { useFlag } from 'common'import { logsKeys } from './keys'+import {+ aggregateFunctionLogs,+ flattenOtelInspectionRow,+ type OtelLogRow,+} from './otel-inspection.utils'import {getUnifiedLogsISOStartEnd,UNIFIED_LOGS_QUERY_OPTIONS,@@ -16,7 +23,7 @@ import { QuerySearchParamsType } from '@/components/interfaces/UnifiedLogs/Unifiimport { handleError, post } from '@/data/fetchers'import type { ResponseError, UseCustomQueryOptions } from '@/types'-// Service flow types - subset of LOG_TYPES that support service flows+// Service flow types — subset of LOG_TYPES that support service flows.export const SERVICE_FLOW_TYPES = ['postgrest','auth',@@ -32,6 +39,7 @@ export type UnifiedLogInspectionVariables = {logId?: stringtype?: ServiceFlowTypesearch: QuerySearchParamsType+ useOtel?: boolean}export type UnifiedLogInspectionResponse = {@@ -116,7 +124,7 @@ export interface UnifiedLogInspectionEntry {}export async function getUnifiedLogInspection(- { projectRef, logId, type, search }: UnifiedLogInspectionVariables,+ { projectRef, logId, type, search, useOtel = false }: UnifiedLogInspectionVariables,signal?: AbortSignal) {if (!projectRef) {@@ -129,36 +137,71 @@ export async function getUnifiedLogInspection(throw new Error('type is required')}- let sql = ''- switch (type) {- case 'postgrest':- sql = getPostgrestServiceFlowQuery(logId)- break- case 'auth':- sql = getAuthServiceFlowQuery(logId)- break- case 'edge-function':- sql = getEdgeFunctionServiceFlowQuery(logId)- break- case 'storage':- sql = getStorageServiceFlowQuery(logId)- break- case 'postgres':- sql = getPostgresServiceFlowQuery(logId)- break- default:- throw new Error('Invalid type')+ const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)++ if (!useOtel) {+ let sql = ''+ switch (type) {+ case 'postgrest':+ sql = getPostgrestServiceFlowQuery(logId)+ break+ case 'auth':+ sql = getAuthServiceFlowQuery(logId)+ break+ case 'edge-function':+ sql = getEdgeFunctionServiceFlowQuery(logId)+ break+ case 'storage':+ sql = getStorageServiceFlowQuery(logId)+ break+ case 'postgres':+ sql = getPostgresServiceFlowQuery(logId)+ break+ default:+ throw new Error('Invalid type')+ }++ const { data, error } = await post('/platform/projects/{ref}/analytics/endpoints/logs.all', {+ params: { path: { ref: projectRef } },+ body: {+ iso_timestamp_start: isoTimestampStart,+ iso_timestamp_end: isoTimestampEnd,+ sql: sql,+ },+ signal,+ })++ if (error) {+ handleError(error)+ }++ return data as unknown as UnifiedLogInspectionResponse}- // Use the same timestamp logic as the main unified logs query- const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)+ // OTEL path: the endpoint stores all log sources in a single `logs` table+ // and exposes the rich detail through the `log_attributes` Map. We just+ // fetch the single row by id and flatten its attributes onto the response+ // so existing panel components that read `enrichedData['request.path']`+ // etc. keep working without per service flow SQL.+ //+ // `logId` ultimately originates from a URL query parameter, so we route+ // every interpolation through pg-meta's `literal()` / `safeSql` helpers.+ // The analytics endpoint uses ClickHouse SQL string literal escaping+ // rules (single quotes doubled), which matches Postgres and what+ // `literal()` produces.+ const sql = safeSql`+SELECT id, timestamp, source, event_message, severity_text, log_attributes+FROM logs+WHERE id = ${literal(logId)}+LIMIT 1+`.trim()- const { data, error } = await post('/platform/projects/{ref}/analytics/endpoints/logs.all', {+ 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: sql,+ sql,},signal,})@@ -167,7 +210,50 @@ export async function getUnifiedLogInspection(handleError(error)}- return data as unknown as UnifiedLogInspectionResponse+ const otelData = data as { result?: OtelLogRow[] } | undefined+ const row = otelData?.result?.[0]+ if (!row) {+ return { result: [] }+ }++ const entry = flattenOtelInspectionRow(row)++ // For edge function rows, fetch and aggregate the related `function_logs`+ // (the per-execution console.log output) — this is the only legitimate+ // cross-source join in the legacy BigQuery service-flow queries.+ if (row.source === 'function_edge_logs') {+ const executionId = row.log_attributes?.['execution_id'] ?? row.log_attributes?.['request_id']+ if (typeof executionId === 'string') {+ const fnSql = safeSql`+SELECT id, timestamp, source, event_message, severity_text, log_attributes+FROM logs+WHERE source = 'function_logs' AND log_attributes['execution_id'] = ${literal(executionId)}+ORDER BY timestamp ASC+LIMIT 100+`.trim()++ 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,+ },+ signal,+ }+ )++ if (!fnError) {+ const fnRows = ((fnData as { result?: OtelLogRow[] } | undefined)?.result ??+ []) as OtelLogRow[]+ Object.assign(entry, aggregateFunctionLogs(fnRows))+ }+ }+ }++ return { result: [entry] }}export type UnifiedLogInspectionData = Awaited<ReturnType<typeof getUnifiedLogInspection>>@@ -179,11 +265,14 @@ export const useUnifiedLogInspectionQuery = <TData = UnifiedLogInspectionData>(enabled = true,...options}: UseCustomQueryOptions<UnifiedLogInspectionData, UnifiedLogInspectionError, TData> = {}-) =>- useQuery<UnifiedLogInspectionData, UnifiedLogInspectionError, TData>({- queryKey: logsKeys.serviceFlow(projectRef, search, logId),- queryFn: ({ signal }) => getUnifiedLogInspection({ projectRef, logId, type, search }, signal),+) => {+ const useOtel = useFlag('otelUnifiedLogs')+ return useQuery<UnifiedLogInspectionData, UnifiedLogInspectionError, TData>({+ queryKey: [...logsKeys.serviceFlow(projectRef, search, logId), { otel: useOtel }],+ queryFn: ({ signal }) =>+ getUnifiedLogInspection({ projectRef, logId, type, search, useOtel }, signal),enabled: enabled && typeof projectRef !== 'undefined',...UNIFIED_LOGS_QUERY_OPTIONS,...options,})+}diff --git a/apps/studio/data/logs/unified-logs-chart-query.ts b/apps/studio/data/logs/unified-logs-chart-query.tsindex df60870f445..225e2c119fa 100644--- a/apps/studio/data/logs/unified-logs-chart-query.ts+++ b/apps/studio/data/logs/unified-logs-chart-query.ts@@ -1,14 +1,17 @@import { keepPreviousData, useQuery } from '@tanstack/react-query'+import { useFlag } from 'common'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'export async function getUnifiedLogsChart(- { projectRef, search }: UnifiedLogsVariables,+ { projectRef, search, useOtel = false }: UnifiedLogsVariables & { useOtel?: boolean },signal?: AbortSignal,headersInit?: HeadersInit) {@@ -37,11 +40,12 @@ export async function getUnifiedLogsChart(}// Get SQL query from utility function (with dynamic bucketing)- const sql = getLogsChartQuery(search)+ const sql = pickLogsQueryBuilder(useOtel, getLogsChartQuery, getLogsChartQueryBq)(search)let headers = new Headers(headersInit)- const { data, error } = await post(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {+ const endpoint = logsAllEndpointUrl(useOtel)+ const { data, error } = await post(endpoint, {params: { path: { ref: projectRef } },body: { sql, iso_timestamp_start: dateStart, iso_timestamp_end: dateEnd },signal,@@ -69,9 +73,13 @@ export async function getUnifiedLogsChart(if (data?.result) {data.result.forEach((row: any) => {- // The API returns timestamps in microseconds (needs to be converted to milliseconds for JS Date)- const microseconds = Number(row.time_bucket)- const milliseconds = Math.floor(microseconds / 1000)+ // Disambiguate by format rather than Number.isFinite — see+ // unified-logs-infinite-query.ts for the reasoning.+ const ts = String(row.time_bucket ?? '')+ const looksLikeIso = /[T-]/.test(ts)+ const milliseconds = looksLikeIso+ ? new Date(/Z$|[+-]\d{2}:?\d{2}$/.test(ts) ? ts : `${ts}Z`).getTime()+ : Math.floor(Number(ts) / 1000)// Create chart data pointconst dataPoint = {@@ -103,14 +111,11 @@ export async function getUnifiedLogsChart(const timeRangeHours = (endTimeMs - startTimeMs) / (1000 * 60 * 60)let bucketSizeMs: number- if (timeRangeHours > 72) {- // Day-level bucketing (for ranges > 3 days)+ if (timeRangeHours >= 48) {bucketSizeMs = 24 * 60 * 60 * 1000} else if (timeRangeHours > 12) {- // Hour-level bucketing (for ranges > 12 hours)bucketSizeMs = 60 * 60 * 1000} else {- // Minute-level bucketing (for shorter ranges)bucketSizeMs = 60 * 1000}@@ -150,12 +155,14 @@ export const useUnifiedLogsChartQuery = <TData = UnifiedLogsChartData>(enabled = true,...options}: UseCustomQueryOptions<UnifiedLogsChartData, UnifiedLogsChartError, TData> = {}-) =>- useQuery<UnifiedLogsChartData, UnifiedLogsChartError, TData>({- queryKey: logsKeys.unifiedLogsChart(projectRef, search),- queryFn: ({ signal }) => getUnifiedLogsChart({ projectRef, search }, signal),+) => {+ const useOtel = useFlag('otelUnifiedLogs')+ return useQuery<UnifiedLogsChartData, UnifiedLogsChartError, TData>({+ queryKey: [...logsKeys.unifiedLogsChart(projectRef, search), { otel: useOtel }],+ queryFn: ({ signal }) => getUnifiedLogsChart({ projectRef, search, useOtel }, signal),enabled: enabled && typeof projectRef !== 'undefined',placeholderData: keepPreviousData,...UNIFIED_LOGS_QUERY_OPTIONS,...options,})+}diff --git a/apps/studio/data/logs/unified-logs-count-query.ts b/apps/studio/data/logs/unified-logs-count-query.tsindex 3cd8612d647..81dfa7b54be 100644--- a/apps/studio/data/logs/unified-logs-count-query.ts+++ b/apps/studio/data/logs/unified-logs-count-query.ts@@ -1,29 +1,33 @@import { useQuery } from '@tanstack/react-query'+import { useFlag } from 'common'import { logsKeys } from './keys'+import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'import {getUnifiedLogsISOStartEnd,UNIFIED_LOGS_QUERY_OPTIONS,UnifiedLogsVariables,} from './unified-logs-infinite-query'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'export async function getUnifiedLogsCount(- { projectRef, search }: UnifiedLogsVariables,+ { projectRef, search, useOtel = false }: UnifiedLogsVariables & { useOtel?: boolean },signal?: AbortSignal) {if (typeof projectRef === 'undefined') {throw new Error('projectRef is required for getUnifiedLogsCount')}- const sql = getLogsCountQuery(search)+ const sql = pickLogsQueryBuilder(useOtel, getLogsCountQuery, getLogsCountQueryBq)(search)const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)- const { data, error } = await post(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {+ const endpoint = logsAllEndpointUrl(useOtel)+ const { data, error } = await post(endpoint, {params: { path: { ref: projectRef } },body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: isoTimestampEnd, sql },signal,@@ -83,11 +87,13 @@ export const useUnifiedLogsCountQuery = <TData = UnifiedLogsCountData>(enabled = true,...options}: UseCustomQueryOptions<UnifiedLogsCountData, UnifiedLogsCountError, TData> = {}-) =>- useQuery<UnifiedLogsCountData, UnifiedLogsCountError, TData>({- queryKey: logsKeys.unifiedLogsCount(projectRef, search),- queryFn: ({ signal }) => getUnifiedLogsCount({ projectRef, search }, signal),+) => {+ const useOtel = useFlag('otelUnifiedLogs')+ return useQuery<UnifiedLogsCountData, UnifiedLogsCountError, TData>({+ queryKey: [...logsKeys.unifiedLogsCount(projectRef, search), { otel: useOtel }],+ queryFn: ({ signal }) => getUnifiedLogsCount({ projectRef, search, useOtel }, signal),enabled: enabled && typeof projectRef !== 'undefined',...UNIFIED_LOGS_QUERY_OPTIONS,...options,})+}diff --git a/apps/studio/data/logs/unified-logs-facet-count-query.ts b/apps/studio/data/logs/unified-logs-facet-count-query.tsindex 3ea1f44f1cd..90d13406483 100644--- a/apps/studio/data/logs/unified-logs-facet-count-query.ts+++ b/apps/studio/data/logs/unified-logs-facet-count-query.ts@@ -1,15 +1,18 @@import { useQuery } from '@tanstack/react-query'+import { useFlag } from 'common'import { logsKeys } from './keys'+import { logsAllEndpointUrl } from './logs-endpoint'import {getUnifiedLogsISOStartEnd,UNIFIED_LOGS_QUERY_OPTIONS,UnifiedLogsVariables,} from './unified-logs-infinite-query'+import { getFacetCountQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'import {getFacetCountCTE,getUnifiedLogsCTE,-} from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'+} 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'@@ -18,10 +21,11 @@ import { UseCustomQueryOptions } from '@/types'type UnifiedLogsFacetCountVariables = UnifiedLogsVariables & {facet: stringfacetSearch?: string+ useOtel?: boolean}export async function getUnifiedLogsFacetCount(- { projectRef, search, facet, facetSearch }: UnifiedLogsFacetCountVariables,+ { projectRef, search, facet, facetSearch, useOtel = false }: UnifiedLogsFacetCountVariables,signal?: AbortSignal) {if (typeof projectRef === 'undefined') {@@ -29,12 +33,16 @@ export async function getUnifiedLogsFacetCount(}const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)- const sql = `+ const sql = useOtel+ ? getFacetCountQuery({ search, facet, facetSearch })+ : `${getUnifiedLogsCTE()},${getFacetCountCTE({ search, facet, facetSearch })}SELECT dimension, value, count from ${facet}_count;`.trim()- const { data, error } = await post(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {++ const endpoint = logsAllEndpointUrl(useOtel)+ const { data, error } = await post(endpoint, {params: { path: { ref: projectRef } },body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: isoTimestampEnd, sql },signal,@@ -53,12 +61,17 @@ export const useUnifiedLogsFacetCountQuery = <TData = UnifiedLogsFacetCountData>enabled = true,...options}: UseCustomQueryOptions<UnifiedLogsFacetCountData, UnifiedLogsFacetCountError, TData> = {}-) =>- useQuery<UnifiedLogsFacetCountData, UnifiedLogsFacetCountError, TData>({- queryKey: logsKeys.unifiedLogsFacetCount(projectRef, facet, facetSearch, search),+) => {+ const useOtel = useFlag('otelUnifiedLogs')+ return useQuery<UnifiedLogsFacetCountData, UnifiedLogsFacetCountError, TData>({+ queryKey: [+ ...logsKeys.unifiedLogsFacetCount(projectRef, facet, facetSearch, search),+ { otel: useOtel },+ ],queryFn: ({ signal }) =>- getUnifiedLogsFacetCount({ projectRef, search, facet, facetSearch }, signal),+ getUnifiedLogsFacetCount({ projectRef, search, facet, facetSearch, useOtel }, signal),enabled: enabled && typeof projectRef !== 'undefined',...UNIFIED_LOGS_QUERY_OPTIONS,...options,})+}diff --git a/apps/studio/data/logs/unified-logs-infinite-query.ts b/apps/studio/data/logs/unified-logs-infinite-query.tsindex 90c9f2b07c7..ea9fb942495 100644--- a/apps/studio/data/logs/unified-logs-infinite-query.ts+++ b/apps/studio/data/logs/unified-logs-infinite-query.ts@@ -1,7 +1,10 @@import { InfiniteData, keepPreviousData, useInfiniteQuery } from '@tanstack/react-query'+import { useFlag } from 'common'import { logsKeys } from './keys'+import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'import { getUnifiedLogsQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'+import { getUnifiedLogsQuery as getUnifiedLogsQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'import {PageParam,QuerySearchParamsType,@@ -47,7 +50,12 @@ export const getUnifiedLogsISOStartEnd = (}export async function getUnifiedLogs(- { projectRef, search, pageParam }: UnifiedLogsVariables & { pageParam: PageParam | null },+ {+ projectRef,+ search,+ pageParam,+ useOtel = false,+ }: UnifiedLogsVariables & { pageParam: PageParam | null; useOtel?: boolean },signal?: AbortSignal,headersInit?: HeadersInit) {@@ -74,7 +82,8 @@ export async function getUnifiedLogs(*/const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)- const sql = `${getUnifiedLogsQuery(search)} ORDER BY timestamp DESC, id DESC LIMIT ${LOGS_PAGE_LIMIT}`+ const buildQuery = pickLogsQueryBuilder(useOtel, getUnifiedLogsQuery, getUnifiedLogsQueryBq)+ const sql = `${buildQuery(search)} ORDER BY timestamp DESC, id DESC LIMIT ${LOGS_PAGE_LIMIT}`const cursorValue = pageParam?.cursorconst cursorDirection = pageParam?.direction@@ -85,17 +94,21 @@ export async function getUnifiedLogs(// Live mode: fetch logs newer than the cursortimestampEnd = new Date().toISOString()} else if (cursorDirection === 'next') {- // Regular pagination: fetch logs older than the cursor- timestampEnd = cursorValue- ? new Date(Number(cursorValue) / 1000).toISOString()- : isoTimestampEnd+ // Regular pagination: fetch logs older than the cursor.+ // The cursor is stored as milliseconds (set below from `date.getTime()`),+ // so we can convert it directly without worrying about the wire format.+ timestampEnd =+ cursorValue !== null && cursorValue !== undefined+ ? new Date(Number(cursorValue)).toISOString()+ : isoTimestampEnd} else {timestampEnd = isoTimestampEnd}let headers = new Headers(headersInit)- const { data, error } = await post(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {+ const endpoint = logsAllEndpointUrl(useOtel)+ const { data, error } = await post(endpoint, {params: { path: { ref: projectRef } },body: { iso_timestamp_start: isoTimestampStart, iso_timestamp_end: timestampEnd, sql },signal,@@ -107,8 +120,16 @@ export async function getUnifiedLogs(const resultData = data?.result ?? []const result = resultData.map((row: any) => {- // Create a date object for display purposes- const date = new Date(Number(row.timestamp) / 1000)+ // Disambiguate timestamp shape by format, not by Number.isFinite — a+ // numeric string of microseconds is always finite, so checking finite-+ // ness can't tell us whether the value is microseconds or already-ms.+ // ISO-like strings contain `T` or `-` and are parsed via Date; anything+ // else is treated as numeric microseconds-since-epoch.+ const ts = String(row.timestamp ?? '')+ const looksLikeIso = /[T-]/.test(ts)+ const date = looksLikeIso+ ? new Date(/Z$|[+-]\d{2}:?\d{2}$/.test(ts) ? ts : `${ts}Z`)+ : new Date(Number(ts) / 1000)return {id: row.id,@@ -135,11 +156,10 @@ export async function getUnifiedLogs(const lastRow = result.length > 0 ? result[result.length - 1] : nullconst hasMore = result.length >= LOGS_PAGE_LIMIT - 1- const nextCursor = lastRow ? lastRow.timestamp : null- // FIXED: Always provide prevCursor like DataTableDemo does- // This ensures live mode never breaks the infinite query chain- // DataTableDemo uses milliseconds, but our timestamps are in microseconds- const prevCursor = result.length > 0 ? firstRow!.timestamp : new Date().getTime() * 1000+ // Cursors are stored as milliseconds (Date.getTime()) so the OTEL endpoint's+ // wire format (ISO string vs numeric microseconds) doesn't bleed into pagination.+ const nextCursor = lastRow ? lastRow.date.getTime() : null+ const prevCursor = firstRow ? firstRow.date.getTime() : new Date().getTime()return {data: result,@@ -161,10 +181,11 @@ export const useUnifiedLogsInfiniteQuery = <TData = UnifiedLogsData>(PageParam | null> = {}) => {+ const useOtel = useFlag('otelUnifiedLogs')return useInfiniteQuery({- queryKey: logsKeys.unifiedLogsInfinite(projectRef, search),+ queryKey: [...logsKeys.unifiedLogsInfinite(projectRef, search), { otel: useOtel }],queryFn: ({ signal, pageParam }) => {- return getUnifiedLogs({ projectRef, search, pageParam }, signal)+ return getUnifiedLogs({ projectRef, search, pageParam, useOtel }, signal)},enabled: enabled && typeof projectRef !== 'undefined',placeholderData: keepPreviousData,diff --git a/apps/studio/hooks/analytics/useLogsQuery.tsx b/apps/studio/hooks/analytics/useLogsQuery.tsxindex a5a6756b1c9..51e0404ffae 100644--- a/apps/studio/hooks/analytics/useLogsQuery.tsx+++ b/apps/studio/hooks/analytics/useLogsQuery.tsx@@ -34,8 +34,10 @@ export interface LogsQueryHook {export const useLogsQuery = (projectRef: string,initialParams: Partial<LogsEndpointParams> = {},- enabled = true+ enabled = true,+ options: { useOtel?: boolean } = {}): LogsQueryHook => {+ const { useOtel = false } = optionsconst defaultHelper = getDefaultHelper(EXPLORER_DATEPICKER_HELPERS)const [params, setParams] = useState<LogsEndpointParams>({sql: initialParams?.sql || '',@@ -71,9 +73,12 @@ export const useLogsQuery = (isRefetching,refetch,} = useQuery({- queryKey: ['projects', projectRef, 'logs', params],+ queryKey: ['projects', projectRef, 'logs', params, { otel: useOtel }],queryFn: async ({ signal }) => {- const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {+ 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, {params: {path: { ref: projectRef },query: params,@@ -96,7 +101,9 @@ export const useLogsQuery = (error = data?.error}- if (IS_PLATFORM) {+ // BigQuery-specific parser limitations don't apply to the ClickHouse-backed+ // OTEL endpoint, so skip these warnings when querying it.+ if (IS_PLATFORM && !useOtel) {if (usesWith) {error = {message: 'The parser does not yet support WITH and subquery statements.',diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.tsindex c70246133b6..4db71799866 100644--- a/apps/studio/lib/constants/index.ts+++ b/apps/studio/lib/constants/index.ts@@ -10,6 +10,15 @@ export const IS_PLATFORM = process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'*/export const IS_TEST_ENV = process.env.NEXT_PUBLIC_NODE_ENV === 'test'+/**+ * True when running against the staging or local environments. Used to gate+ * staff-only debugging affordances (e.g. the unified logs OTEL toggle) that+ * should never be visible to customers on production.+ */+export const IS_STAGING_OR_LOCAL =+ process.env.NEXT_PUBLIC_ENVIRONMENT === 'staging' ||+ process.env.NEXT_PUBLIC_ENVIRONMENT === 'local'+export const API_URL = (() => {if (process.env.NODE_ENV === 'test') return 'http://localhost:3000/api'// If running in platform, use API_URL from the env vardiff --git a/apps/studio/pages/project/[ref]/logs/explorer/index.tsx b/apps/studio/pages/project/[ref]/logs/explorer/index.tsxindex 8e5b57e72df..c29beb0b7b3 100644--- a/apps/studio/pages/project/[ref]/logs/explorer/index.tsx+++ b/apps/studio/pages/project/[ref]/logs/explorer/index.tsx@@ -105,6 +105,11 @@ export const LogsExplorerPage: NextPageWithLayout = () => {[])+ const [useOtelEndpoint, setUseOtelEndpoint] = useLocalStorage<boolean>(+ `logs-explorer-use-otel-endpoint-${projectRef}`,+ false+ )+const { getEntitlementNumericValue } = useCheckEntitlements('log.retention_days')const entitledToAuditLogDays = getEntitlementNumericValue()@@ -136,7 +141,8 @@ export const LogsExplorerPage: NextPageWithLayout = () => {iso_timestamp_start: resolvedRange.from,iso_timestamp_end: resolvedRange.to,},- true+ true,+ { useOtel: useOtelEndpoint })const results = logData@@ -194,7 +200,7 @@ export const LogsExplorerPage: NextPageWithLayout = () => {addRecentLogSqlSnippet({ sql: template.searchString })}- const handleRun = (value?: string | React.MouseEvent<HTMLButtonElement>) => {+ const handleRun = (value?: string | React.MouseEvent) => {track('log_explorer_query_run_button_clicked', { is_saved_query: !!queryId })const query = typeof value === 'string' ? value || editorValue : editorValue@@ -379,6 +385,8 @@ export const LogsExplorerPage: NextPageWithLayout = () => {templates={allTemplates.filter((template) => template.mode === 'custom')}onSelectTemplate={onSelectTemplate}warnings={warnings}+ useOtel={useOtelEndpoint}+ onUseOtelChange={setUseOtelEndpoint}/><ShimmerLine active={isLoading} /><CodeEditor