Skip to content

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

feat(studio): migrate per-service log pages to OTEL endpoint behind a flag DEBUG-145 (#47264)

Jordi Enrice0ba04caf4e6
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts
index 55103491782..ec5fc44dbaf 100644
--- a/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts
@@ -287,7 +287,7 @@ limit 100;
]
type SqlFilterFn = (value: any) => SafeLogSqlFragment
-type SqlFilterEntry = SafeLogSqlFragment | SqlFilterFn
+export type SqlFilterEntry = SafeLogSqlFragment | SqlFilterFn
const _SQL_FILTER_COMMON: Record<string, SqlFilterEntry> = {
search_query: (value: string) =>
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.types.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.types.ts
index f29dc87a703..38e6d799807 100644
--- a/apps/studio/components/interfaces/Settings/Logs/Logs.types.ts
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.types.ts
@@ -3,7 +3,7 @@ import React from 'react'
import type { Datum } from '@/components/ui/Charts/Charts.types'
-interface Metadata {
+export interface Metadata {
[key: string]: string | number | Object | Object[] | any
}
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.test.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.test.ts
new file mode 100644
index 00000000000..89ecbf0c326
--- /dev/null
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.test.ts
@@ -0,0 +1,731 @@
+import { describe, expect, it } from 'vitest'
+
+import { LogsTableName } from './Logs.constants'
+import {
+ genChartQueryOtel,
+ genCountQueryOtel,
+ genDefaultQueryOtel,
+ genSingleLogQueryOtel,
+ mapOtelPreviewRow,
+ mapOtelSingleLogToLegacy,
+ otelTimestampToMicros,
+} from './Logs.utils.otel'
+import type { OtelLogRow } from '@/data/logs/otel-inspection.utils'
+import { formatSql } from '@/lib/formatSql'
+
+// Format generated SQL so snapshots are deterministic and full strings can be compared.
+const fmt = (fragment: string) => formatSql(fragment)
+
+describe('genDefaultQueryOtel', () => {
+ it('targets the OTEL logs table by source and aliases postgres columns', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.POSTGRES, {}, 100))).toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['postgres_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['identifier'] as identifier,
+ log_attributes['parsed.error_severity'] as error_severity,
+ log_attributes['parsed.detail'] as detail,
+ log_attributes['parsed.hint'] as hint
+ from
+ logs
+ where
+ source = 'postgres_logs'
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('aliases edge columns to the leaf names the api renderer reads', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.EDGE, {}, 50))).toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['edge_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['identifier'] as identifier,
+ log_attributes['request.method'] as method,
+ log_attributes['request.path'] as path,
+ log_attributes['request.search'] as search,
+ log_attributes['response.status_code'] as status_code
+ from
+ logs
+ where
+ source = 'edge_logs'
+ order by
+ timestamp desc
+ limit
+ 50"
+ `)
+ })
+
+ it('maps pg_cron onto postgres_logs with the cron predicate', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.PG_CRON, {}))).toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['pg_cron_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['parsed.error_severity'] as error_severity,
+ log_attributes['parsed.query'] as query
+ from
+ logs
+ where
+ source = 'postgres_logs'
+ and (
+ log_attributes['parsed.application_name'] = 'pg_cron'
+ or event_message ilike '%cron job%'
+ )
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('selects only id/timestamp/event_message for tables without a column map', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.MULTIGRES, {}))).toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['multigres_logs']
+ select
+ id,
+ timestamp,
+ event_message
+ from
+ logs
+ where
+ source = 'multigres_logs'
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('translates an edge status_code filter to a ClickHouse predicate', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.EDGE, { status_code: { error: true } })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['edge_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['identifier'] as identifier,
+ log_attributes['request.method'] as method,
+ log_attributes['request.path'] as path,
+ log_attributes['request.search'] as search,
+ log_attributes['response.status_code'] as status_code
+ from
+ logs
+ where
+ source = 'edge_logs'
+ and (
+ toInt32OrZero (log_attributes['response.status_code']) between 500 and 599
+ )
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('translates a search_query filter to a case-insensitive event_message match', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.POSTGRES, { search_query: 'deadlock' })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['postgres_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['identifier'] as identifier,
+ log_attributes['parsed.error_severity'] as error_severity,
+ log_attributes['parsed.detail'] as detail,
+ log_attributes['parsed.hint'] as hint
+ from
+ logs
+ where
+ source = 'postgres_logs'
+ and (event_message ilike '%deadlock%')
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('uses the shared auth severity condition for the severity.error filter', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.AUTH, { severity: { error: true } })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['auth_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['level'] as level,
+ log_attributes['status'] as status,
+ log_attributes['path'] as path,
+ log_attributes['msg'] as msg,
+ log_attributes['error'] as error
+ from
+ logs
+ where
+ source = 'auth_logs'
+ and (
+ log_attributes['level'] in ('error', 'fatal')
+ or toInt32OrZero (log_attributes['status']) >= 500
+ )
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('falls back to a log_attributes equality for unknown filter keys', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.AUTH, { trace_id: 'abc-123' })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['auth_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['level'] as level,
+ log_attributes['status'] as status,
+ log_attributes['path'] as path,
+ log_attributes['msg'] as msg,
+ log_attributes['error'] as error
+ from
+ logs
+ where
+ source = 'auth_logs'
+ and (log_attributes['trace_id'] = 'abc-123')
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('combines multiple filters with AND alongside the source predicate', () => {
+ expect(
+ fmt(
+ genDefaultQueryOtel(LogsTableName.EDGE, {
+ status_code: { error: true },
+ method: { get: true },
+ })
+ )
+ ).toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['edge_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['identifier'] as identifier,
+ log_attributes['request.method'] as method,
+ log_attributes['request.path'] as path,
+ log_attributes['request.search'] as search,
+ log_attributes['response.status_code'] as status_code
+ from
+ logs
+ where
+ source = 'edge_logs'
+ and (
+ toInt32OrZero (log_attributes['response.status_code']) between 500 and 599
+ )
+ and (log_attributes['request.method'] = 'GET')
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+})
+
+describe('genCountQueryOtel', () => {
+ it('counts rows for the source with translated auth filters', () => {
+ expect(fmt(genCountQueryOtel(LogsTableName.AUTH, { status_code: { server_error: true } })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Count Query (otel) ['auth_logs']
+ select
+ count() as count
+ from
+ logs
+ where
+ source = 'auth_logs'
+ and (
+ toInt32OrZero (log_attributes['status']) between 500 and 599
+ )"
+ `)
+ })
+})
+
+describe('genChartQueryOtel', () => {
+ const params = {
+ iso_timestamp_start: '2024-01-01T00:00:00.000Z',
+ iso_timestamp_end: '2024-01-01T00:30:00.000Z',
+ }
+
+ it('buckets by minute for short ranges and emits ok/error/warning counts', () => {
+ expect(fmt(genChartQueryOtel(LogsTableName.EDGE, params, {}))).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['edge_logs']
+ select
+ toStartOfMinute (timestamp) as timestamp,
+ countIf (
+ not (
+ (
+ toInt32OrZero (log_attributes['response.status_code']) >= 500
+ )
+ or (
+ toInt32OrZero (log_attributes['response.status_code']) between 400 and 499
+ )
+ )
+ ) as ok_count,
+ countIf (
+ toInt32OrZero (log_attributes['response.status_code']) >= 500
+ ) as error_count,
+ countIf (
+ toInt32OrZero (log_attributes['response.status_code']) between 400 and 499
+ ) as warning_count
+ from
+ logs
+ where
+ source = 'edge_logs'
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+
+ it('buckets by hour for ranges longer than 12 hours', () => {
+ expect(
+ fmt(
+ genChartQueryOtel(
+ LogsTableName.POSTGRES,
+ {
+ iso_timestamp_start: '2024-01-01T00:00:00.000Z',
+ iso_timestamp_end: '2024-01-02T00:00:00.000Z',
+ },
+ {}
+ )
+ )
+ ).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['postgres_logs']
+ select
+ toStartOfHour (timestamp) as timestamp,
+ countIf (
+ not (
+ (
+ log_attributes['parsed.error_severity'] in ('ERROR', 'FATAL', 'PANIC')
+ )
+ or (
+ log_attributes['parsed.error_severity'] = 'WARNING'
+ )
+ )
+ ) as ok_count,
+ countIf (
+ log_attributes['parsed.error_severity'] in ('ERROR', 'FATAL', 'PANIC')
+ ) as error_count,
+ countIf (
+ log_attributes['parsed.error_severity'] = 'WARNING'
+ ) as warning_count
+ from
+ logs
+ where
+ source = 'postgres_logs'
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+
+ it('classifies auth severity by level and status, matching the BigQuery conditions', () => {
+ expect(fmt(genChartQueryOtel(LogsTableName.AUTH, params, {}))).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['auth_logs']
+ select
+ toStartOfMinute (timestamp) as timestamp,
+ countIf (
+ not (
+ (
+ log_attributes['level'] in ('error', 'fatal')
+ or toInt32OrZero (log_attributes['status']) >= 500
+ )
+ or (
+ log_attributes['level'] = 'warning'
+ or toInt32OrZero (log_attributes['status']) between 400 and 499
+ )
+ )
+ ) as ok_count,
+ countIf (
+ log_attributes['level'] in ('error', 'fatal')
+ or toInt32OrZero (log_attributes['status']) >= 500
+ ) as error_count,
+ countIf (
+ log_attributes['level'] = 'warning'
+ or toInt32OrZero (log_attributes['status']) between 400 and 499
+ ) as warning_count
+ from
+ logs
+ where
+ source = 'auth_logs'
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+
+ it('uses JSONExtractString for multigres severity (event_message is a JSON string)', () => {
+ expect(fmt(genChartQueryOtel(LogsTableName.MULTIGRES, params, {}))).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['multigres_logs']
+ select
+ toStartOfMinute (timestamp) as timestamp,
+ countIf (
+ not (
+ (
+ JSONExtractString (event_message, 'level') in ('ERROR', 'FATAL', 'PANIC')
+ )
+ or (
+ JSONExtractString (event_message, 'level') in ('WARN', 'WARNING')
+ )
+ )
+ ) as ok_count,
+ countIf (
+ JSONExtractString (event_message, 'level') in ('ERROR', 'FATAL', 'PANIC')
+ ) as error_count,
+ countIf (
+ JSONExtractString (event_message, 'level') in ('WARN', 'WARNING')
+ ) as warning_count
+ from
+ logs
+ where
+ source = 'multigres_logs'
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+
+ it('counts pg_cron errors but never warnings, matching BigQuery (no pg_cron warning case)', () => {
+ expect(fmt(genChartQueryOtel(LogsTableName.PG_CRON, params, {}))).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['pg_cron_logs']
+ select
+ toStartOfMinute (timestamp) as timestamp,
+ countIf (
+ not (
+ (
+ log_attributes['parsed.error_severity'] in ('ERROR', 'FATAL', 'PANIC')
+ )
+ or (0)
+ )
+ ) as ok_count,
+ countIf (
+ log_attributes['parsed.error_severity'] in ('ERROR', 'FATAL', 'PANIC')
+ ) as error_count,
+ countIf (0) as warning_count
+ from
+ logs
+ where
+ source = 'postgres_logs'
+ and (
+ log_attributes['parsed.application_name'] = 'pg_cron'
+ or event_message ilike '%cron job%'
+ )
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+
+ it('emits constant-false severity for tables without a severity concept', () => {
+ expect(fmt(genChartQueryOtel(LogsTableName.STORAGE, params, {}))).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['storage_logs']
+ select
+ toStartOfMinute (timestamp) as timestamp,
+ countIf (
+ not (
+ (0)
+ or (0)
+ )
+ ) as ok_count,
+ countIf (0) as error_count,
+ countIf (0) as warning_count
+ from
+ logs
+ where
+ source = 'storage_logs'
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+
+ it('defaults the chart bucket to minute when no time range is given', () => {
+ expect(fmt(genChartQueryOtel(LogsTableName.EDGE, {}, {}))).toMatchInlineSnapshot(`
+ "-- Logs Chart Query (otel) ['edge_logs']
+ select
+ toStartOfMinute (timestamp) as timestamp,
+ countIf (
+ not (
+ (
+ toInt32OrZero (log_attributes['response.status_code']) >= 500
+ )
+ or (
+ toInt32OrZero (log_attributes['response.status_code']) between 400 and 499
+ )
+ )
+ ) as ok_count,
+ countIf (
+ toInt32OrZero (log_attributes['response.status_code']) >= 500
+ ) as error_count,
+ countIf (
+ toInt32OrZero (log_attributes['response.status_code']) between 400 and 499
+ ) as warning_count
+ from
+ logs
+ where
+ source = 'edge_logs'
+ group by
+ timestamp
+ order by
+ timestamp asc"
+ `)
+ })
+})
+
+describe('genSingleLogQueryOtel', () => {
+ it('fetches a single row by id with raw attributes', () => {
+ expect(fmt(genSingleLogQueryOtel('123e4567-e89b-12d3-a456-426614174000')))
+ .toMatchInlineSnapshot(`
+ "-- Single Log Query (otel)
+ select
+ id,
+ timestamp,
+ event_message,
+ source,
+ severity_text,
+ log_attributes
+ from
+ logs
+ where
+ id = '123e4567-e89b-12d3-a456-426614174000'
+ limit
+ 1"
+ `)
+ })
+
+ it('rejects a non-uuid id', () => {
+ expect(() => genSingleLogQueryOtel("1' OR '1'='1")).toThrow('Invalid logId')
+ })
+})
+
+describe('OTEL filter translation', () => {
+ it('translates the database filter for postgres', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.POSTGRES, { database: 'replica-1' })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['postgres_logs']
+ select
+ id,
+ timestamp,
+ event_message,
+ log_attributes['identifier'] as identifier,
+ log_attributes['parsed.error_severity'] as error_severity,
+ log_attributes['parsed.detail'] as detail,
+ log_attributes['parsed.hint'] as hint
+ from
+ logs
+ where
+ source = 'postgres_logs'
+ and (log_attributes['identifier'] = 'replica-1')
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('translates the database filter for supavisor', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.SUPAVISOR, { database: 'proj' })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['supavisor_logs']
+ select
+ id,
+ timestamp,
+ event_message
+ from
+ logs
+ where
+ source = 'supavisor_logs'
+ and (log_attributes['project'] like 'proj%')
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+
+ it('translates the etl pipeline_id filter', () => {
+ expect(fmt(genDefaultQueryOtel(LogsTableName.ETL, { pipeline_id: '42' })))
+ .toMatchInlineSnapshot(`
+ "-- Logs Preview Query (otel) ['etl_replication_logs']
+ select
+ id,
+ timestamp,
+ event_message
+ from
+ logs
+ where
+ source = 'etl_replication_logs'
+ and (log_attributes['pipeline_id'] = '42')
+ order by
+ timestamp desc
+ limit
+ 100"
+ `)
+ })
+})
+
+describe('otelTimestampToMicros', () => {
+ it('converts an ISO string to microseconds since epoch', () => {
+ const micros = otelTimestampToMicros('2024-01-01T00:00:00.000Z')
+ expect(micros).toBe(Date.UTC(2024, 0, 1) * 1000)
+ })
+
+ it('parses a space-separated ClickHouse datetime as UTC', () => {
+ const micros = otelTimestampToMicros('2024-01-01 00:00:00')
+ expect(micros).toBe(Date.UTC(2024, 0, 1) * 1000)
+ })
+
+ it('passes through numeric microseconds', () => {
+ const micros = otelTimestampToMicros(1704067200000000)
+ expect(micros).toBe(1704067200000000)
+ })
+
+ it('parses numeric microseconds supplied as a string', () => {
+ const micros = otelTimestampToMicros('1704067200000000')
+ expect(micros).toBe(1704067200000000)
+ })
+})
+
+describe('mapOtelPreviewRow', () => {
+ it('normalizes the timestamp to a microsecond number and keeps other columns', () => {
+ const row = mapOtelPreviewRow({
+ id: 'abc',
+ timestamp: '2024-01-01T00:00:00.000Z',
+ event_message: 'hello',
+ status_code: '200',
+ })
+ expect(row.timestamp).toBe(Date.UTC(2024, 0, 1) * 1000)
+ expect(row.event_message).toBe('hello')
+ expect((row as any).status_code).toBe('200')
+ })
+})
+
+describe('mapOtelSingleLogToLegacy', () => {
+ const baseRow: OtelLogRow = {
+ id: 'log-1',
+ timestamp: '2024-01-01T00:00:00.000Z',
+ source: 'edge_logs',
+ event_message: 'GET /rest/v1/foo',
+ log_attributes: {
+ 'request.method': 'GET',
+ 'request.path': '/rest/v1/foo',
+ 'request.search': '?select=*',
+ 'request.headers.user_agent': 'curl/8',
+ 'response.status_code': '200',
+ // Real OTEL gateway key is `sb_error_code` (BigQuery used `x_sb_error_code`).
+ 'response.headers.sb_error_code': 'none',
+ },
+ }
+
+ it('reconstructs the nested metadata shape the api panel reads', () => {
+ const log = mapOtelSingleLogToLegacy(baseRow, 'api') as any
+ expect(log.metadata[0].request[0].method).toBe('GET')
+ expect(log.metadata[0].request[0].path).toBe('/rest/v1/foo')
+ expect(log.metadata[0].request[0].search).toBe('?select=*')
+ expect(log.metadata[0].request[0].headers[0].user_agent).toBe('curl/8')
+ expect(log.metadata[0].response[0].status_code).toBe('200')
+ expect(log.metadata[0].response[0].headers[0].x_sb_error_code).toBe('none')
+ expect(log.timestamp).toBe(Date.UTC(2024, 0, 1) * 1000)
+ })
+
+ it('falls back to the legacy x_sb_error_code key when the OTEL key is absent', () => {
+ const row: OtelLogRow = {
+ ...baseRow,
+ log_attributes: {
+ 'request.method': 'GET',
+ 'response.status_code': '500',
+ // Only the legacy BigQuery-style key is present.
+ 'response.headers.x_sb_error_code': 'pgrst_error',
+ },
+ }
+ const log = mapOtelSingleLogToLegacy(row, 'api') as any
+ expect(log.metadata[0].response[0].headers[0].x_sb_error_code).toBe('pgrst_error')
+ })
+
+ it('reconstructs the nested parsed metadata for the database panel', () => {
+ const row: OtelLogRow = {
+ id: 'log-2',
+ timestamp: '2024-01-01T00:00:00.000Z',
+ source: 'postgres_logs',
+ event_message: 'ERROR: deadlock detected',
+ log_attributes: {
+ 'parsed.hint': 'retry',
+ 'parsed.detail': 'process 1 waits',
+ 'parsed.query': 'UPDATE foo',
+ },
+ }
+ const log = mapOtelSingleLogToLegacy(row, 'database') as any
+ expect(log.metadata[0].parsed[0].hint).toBe('retry')
+ expect(log.metadata[0].parsed[0].detail).toBe('process 1 waits')
+ expect(log.metadata[0].parsed[0].query).toBe('UPDATE foo')
+ })
+
+ it('returns a flat row with spread attributes for other query types', () => {
+ const row: OtelLogRow = {
+ id: 'log-3',
+ timestamp: '2024-01-01T00:00:00.000Z',
+ source: 'storage_logs',
+ event_message: 'object uploaded',
+ log_attributes: { level: 'info' },
+ }
+ const log = mapOtelSingleLogToLegacy(row, 'storage') as any
+ expect(log.metadata).toBeUndefined()
+ expect(log.level).toBe('info')
+ expect(log.event_message).toBe('object uploaded')
+ })
+
+ it('returns a safe empty row for an invalid (non-object) input', () => {
+ const log = mapOtelSingleLogToLegacy(undefined) as any
+ expect(log.id).toBe('')
+ expect(log.metadata).toBeUndefined()
+ })
+
+ it('fills api metadata with null when attributes are missing', () => {
+ const log = mapOtelSingleLogToLegacy({ log_attributes: {} }, 'api') as any
+ expect(log.metadata[0].request[0].method).toBeNull()
+ expect(log.metadata[0].response[0].status_code).toBeNull()
+ expect(log.metadata[0].response[0].headers[0].x_sb_error_code).toBeNull()
+ })
+
+ it('falls back to the legacy x_sb_error_code key for api rows', () => {
+ const log = mapOtelSingleLogToLegacy(
+ { log_attributes: { 'response.headers.x_sb_error_code': 'pgrst' } },
+ 'api'
+ ) as any
+ expect(log.metadata[0].response[0].headers[0].x_sb_error_code).toBe('pgrst')
+ })
+
+ it('fills database metadata with null when attributes are missing', () => {
+ const log = mapOtelSingleLogToLegacy({ log_attributes: {} }, 'database') as any
+ expect(log.metadata[0].parsed[0].hint).toBeNull()
+ expect(log.metadata[0].parsed[0].detail).toBeNull()
+ expect(log.metadata[0].parsed[0].query).toBeNull()
+ })
+})
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.ts
new file mode 100644
index 00000000000..c1072e5d86f
--- /dev/null
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.ts
@@ -0,0 +1,394 @@
+import dayjs from 'dayjs'
+import { z } from 'zod'
+
+import { LogsTableName, type SqlFilterEntry } from './Logs.constants'
+import type { Filters, LogData, LogsEndpointParams, Metadata, QueryType } from './Logs.types'
+import { buildWhereClauses } from './Logs.utils'
+import { parseOtelTimestamp } from '@/data/logs/otel-inspection.utils'
+import {
+ joinSqlFragments,
+ analyticsLiteral as lit,
+ safeSql,
+ type SafeLogSqlFragment,
+} from '@/data/logs/safe-analytics-sql'
+
+// Builds ClickHouse SQL for the OTEL `logs` table (one table for all sources,
+// fields in the `log_attributes` map). Mirrors the BigQuery builders in
+// Logs.utils.ts, aliasing columns to the names the row renderers already read.
+
+const attr = (key: string): SafeLogSqlFragment => safeSql`log_attributes[${lit(key)}]`
+const statusAsInt = (key: string): SafeLogSqlFragment => safeSql`toInt32OrZero(${attr(key)})`
+
+// Severity conditions, shared by the chart and the auth filters so a row is
+// classified the same everywhere.
+const HTTP_ERROR: SafeLogSqlFragment = safeSql`${statusAsInt('response.status_code')} >= 500`
+const HTTP_WARNING: SafeLogSqlFragment = safeSql`${statusAsInt('response.status_code')} BETWEEN 400 AND 499`
+const PG_ERROR: SafeLogSqlFragment = safeSql`${attr('parsed.error_severity')} IN ('ERROR', 'FATAL', 'PANIC')`
+const PG_WARNING: SafeLogSqlFragment = safeSql`${attr('parsed.error_severity')} = 'WARNING'`
+const FN_ERROR: SafeLogSqlFragment = safeSql`${attr('level')} IN ('error', 'fatal')`
+const FN_WARNING: SafeLogSqlFragment = safeSql`${attr('level')} = 'warning'`
+const MULTIGRES_ERROR: SafeLogSqlFragment = safeSql`JSONExtractString(event_message, 'level') IN ('ERROR', 'FATAL', 'PANIC')`
+const MULTIGRES_WARNING: SafeLogSqlFragment = safeSql`JSONExtractString(event_message, 'level') IN ('WARN', 'WARNING')`
+const AUTH_ERROR: SafeLogSqlFragment = safeSql`${attr('level')} IN ('error', 'fatal') OR ${statusAsInt('status')} >= 500`
+const AUTH_WARNING: SafeLogSqlFragment = safeSql`${attr('level')} = 'warning' OR ${statusAsInt('status')} BETWEEN 400 AND 499`
+const AUTH_INFO: SafeLogSqlFragment = safeSql`NOT (${AUTH_ERROR}) AND NOT (${AUTH_WARNING})`
+// Tables with no severity: every row counts as ok.
+const NO_MATCH: SafeLogSqlFragment = safeSql`0`
+
+const PG_CRON_CONDITION: SafeLogSqlFragment = safeSql`(${attr('parsed.application_name')} = 'pg_cron' OR event_message ILIKE '%cron job%')`
+
+const COMMON_FILTERS: Record<string, SqlFilterEntry> = {
+ search_query: (value: string) => safeSql`event_message ILIKE ${lit('%' + value + '%')}`,
+}
+const databaseFilter = (value: string): SafeLogSqlFragment =>
+ safeSql`${attr('identifier')} = ${lit(value)}`
+
+// ClickHouse filters per table. Tables not listed use COMMON_FILTERS only.
+const POSTGRES_FILTERS: Record<string, SqlFilterEntry> = {
+ ...COMMON_FILTERS,
+ database: databaseFilter,
+ 'severity.error': PG_ERROR,
+ 'severity.noError': safeSql`${attr('parsed.error_severity')} NOT IN ('ERROR', 'FATAL', 'PANIC')`,
+ 'severity.log': safeSql`${attr('parsed.error_severity')} = 'LOG'`,
+}
+const STATUS_CODE_FILTERS: Record<string, SqlFilterEntry> = {
+ 'status_code.error': safeSql`${statusAsInt('response.status_code')} BETWEEN 500 AND 599`,
+ 'status_code.success': safeSql`${statusAsInt('response.status_code')} BETWEEN 200 AND 299`,
+ 'status_code.warning': safeSql`${statusAsInt('response.status_code')} BETWEEN 400 AND 499`,
+}
+const EDGE_FILTERS: Record<string, SqlFilterEntry> = {
+ ...COMMON_FILTERS,
+ database: databaseFilter,
+ ...STATUS_CODE_FILTERS,
+ 'product.database': safeSql`${attr('request.path')} LIKE '/rest/%' OR ${attr('request.path')} LIKE '/graphql/%'`,
+ 'product.storage': safeSql`${attr('request.path')} LIKE '/storage/%'`,
+ 'product.auth': safeSql`${attr('request.path')} LIKE '/auth/%'`,
+ 'product.realtime': safeSql`${attr('request.path')} LIKE '/realtime/%'`,
+ 'method.get': safeSql`${attr('request.method')} = 'GET'`,
+ 'method.post': safeSql`${attr('request.method')} = 'POST'`,
+ 'method.put': safeSql`${attr('request.method')} = 'PUT'`,
+ 'method.patch': safeSql`${attr('request.method')} = 'PATCH'`,
+ 'method.delete': safeSql`${attr('request.method')} = 'DELETE'`,
+ 'method.options': safeSql`${attr('request.method')} = 'OPTIONS'`,
+}
+const FUNCTION_LOGS_FILTERS: Record<string, SqlFilterEntry> = {
+ ...COMMON_FILTERS,
+ 'severity.error': safeSql`${attr('level')} = 'error'`,
+ 'severity.notError': safeSql`${attr('level')} != 'error'`,
+ 'severity.log': safeSql`${attr('level')} = 'log'`,
+ 'severity.info': safeSql`${attr('level')} = 'info'`,
+ 'severity.debug': safeSql`${attr('level')} = 'debug'`,
+ 'severity.warn': safeSql`${attr('level')} = 'warn'`,
+}
+const AUTH_FILTERS: Record<string, SqlFilterEntry> = {
+ ...COMMON_FILTERS,
+ 'severity.error': AUTH_ERROR,
+ 'severity.warning': AUTH_WARNING,
+ 'severity.info': AUTH_INFO,
+ 'status_code.server_error': safeSql`${statusAsInt('status')} BETWEEN 500 AND 599`,
+ 'status_code.client_error': safeSql`${statusAsInt('status')} BETWEEN 400 AND 499`,
+ 'status_code.redirection': safeSql`${statusAsInt('status')} BETWEEN 300 AND 399`,
+ 'status_code.success': safeSql`${statusAsInt('status')} BETWEEN 200 AND 299`,
+ 'endpoints.admin': safeSql`match(${attr('path')}, '/admin')`,
+ 'endpoints.signup': safeSql`match(${attr('path')}, '/signup|/invite|/verify')`,
+ 'endpoints.authentication': safeSql`match(${attr('path')}, '/token|/authorize|/callback|/otp|/magiclink')`,
+ 'endpoints.recover': safeSql`match(${attr('path')}, '/recover')`,
+ 'endpoints.user': safeSql`match(${attr('path')}, '/user')`,
+ 'endpoints.logout': safeSql`match(${attr('path')}, '/logout')`,
+}
+const SUPAVISOR_FILTERS: Record<string, SqlFilterEntry> = {
+ ...COMMON_FILTERS,
+ database: (value: string) => safeSql`${attr('project')} LIKE ${lit(value + '%')}`,
+}
+const ETL_FILTERS: Record<string, SqlFilterEntry> = {
+ ...COMMON_FILTERS,
+ pipeline_id: (value: string | number) => safeSql`${attr('pipeline_id')} = ${lit(value)}`,
+}
+
+const col = (key: string, alias: SafeLogSqlFragment): SafeLogSqlFragment =>
+ safeSql`${attr(key)} AS ${alias}`
+
+// What varies per table. Everything but `source` is optional, so a plain
+// process-log table is just `{ source }`.
+interface OtelSourceDescriptor {
+ source: string
+ // Columns beyond id/timestamp/event_message, aliased to the renderer names.
+ columns?: SafeLogSqlFragment[]
+ filterTemplates?: Record<string, SqlFilterEntry>
+ // Chart severity; absent means the table has no error/warning.
+ error?: SafeLogSqlFragment
+ warning?: SafeLogSqlFragment
+ // Always ANDed with the source filter (e.g. the pg_cron subset).
+ baseCondition?: SafeLogSqlFragment
+}
+
+const OTEL_SOURCES: Record<LogsTableName, OtelSourceDescriptor> = {
+ [LogsTableName.EDGE]: {
+ source: 'edge_logs',
+ columns: [
+ col('identifier', safeSql`identifier`),
+ col('request.method', safeSql`method`),
+ col('request.path', safeSql`path`),
+ col('request.search', safeSql`search`),
+ col('response.status_code', safeSql`status_code`),
+ ],
+ filterTemplates: EDGE_FILTERS,
+ error: HTTP_ERROR,
+ warning: HTTP_WARNING,
+ },
+ [LogsTableName.POSTGRES]: {
+ source: 'postgres_logs',
+ columns: [
+ col('identifier', safeSql`identifier`),
+ col('parsed.error_severity', safeSql`error_severity`),
+ col('parsed.detail', safeSql`detail`),
+ col('parsed.hint', safeSql`hint`),
+ ],
+ filterTemplates: POSTGRES_FILTERS,
+ error: PG_ERROR,
+ warning: PG_WARNING,
+ },
+ [LogsTableName.PG_CRON]: {
+ source: 'postgres_logs',
+ columns: [
+ col('parsed.error_severity', safeSql`error_severity`),
+ col('parsed.query', safeSql`query`),
+ ],
+ error: PG_ERROR,
+ // No warning bucket, to match BigQuery (where pg_cron has no warning case).
+ baseCondition: PG_CRON_CONDITION,
+ },
+ [LogsTableName.FUNCTIONS]: {
+ source: 'function_logs',
+ columns: [
+ col('event_type', safeSql`event_type`),
+ col('function_id', safeSql`function_id`),
+ col('execution_id', safeSql`execution_id`),
+ col('level', safeSql`level`),
+ ],
+ filterTemplates: FUNCTION_LOGS_FILTERS,
+ error: FN_ERROR,
+ warning: FN_WARNING,
+ },
+ [LogsTableName.FN_EDGE]: {
+ source: 'function_edge_logs',
+ columns: [
+ col('response.status_code', safeSql`status_code`),
+ col('request.method', safeSql`method`),
+ col('request.pathname', safeSql`pathname`),
+ col('function_id', safeSql`function_id`),
+ col('execution_id', safeSql`execution_id`),
+ col('execution_time_ms', safeSql`execution_time_ms`),
+ col('deployment_id', safeSql`deployment_id`),
+ col('version', safeSql`version`),
+ ],
+ filterTemplates: { ...COMMON_FILTERS, ...STATUS_CODE_FILTERS },
+ error: HTTP_ERROR,
+ warning: HTTP_WARNING,
+ },
+ [LogsTableName.AUTH]: {
+ source: 'auth_logs',
+ columns: [
+ col('level', safeSql`level`),
+ col('status', safeSql`status`),
+ col('path', safeSql`path`),
+ col('msg', safeSql`msg`),
+ col('error', safeSql`error`),
+ ],
+ filterTemplates: AUTH_FILTERS,
+ error: AUTH_ERROR,
+ warning: AUTH_WARNING,
+ },
+ [LogsTableName.MULTIGRES]: {
+ source: 'multigres_logs',
+ error: MULTIGRES_ERROR,
+ warning: MULTIGRES_WARNING,
+ },
+ [LogsTableName.POSTGREST]: {
+ source: 'postgrest_logs',
+ filterTemplates: { ...COMMON_FILTERS, database: databaseFilter },
+ },
+ [LogsTableName.SUPAVISOR]: { source: 'supavisor_logs', filterTemplates: SUPAVISOR_FILTERS },
+ [LogsTableName.ETL]: { source: 'etl_replication_logs', filterTemplates: ETL_FILTERS },
+ [LogsTableName.AUTH_AUDIT]: { source: 'auth_audit_logs' },
+ [LogsTableName.REALTIME]: { source: 'realtime_logs' },
+ [LogsTableName.STORAGE]: { source: 'storage_logs' },
+ [LogsTableName.PGBOUNCER]: { source: 'pgbouncer_logs' },
+ [LogsTableName.PG_UPGRADE]: { source: 'pg_upgrade_logs' },
+}
+
+// Fallback for filter keys with no template: `log_attributes[key] = value`.
+// Drops the clause on non-scalar or un-encodable input.
+const resolveUnknownOtelClause = (dotKey: string, value: unknown): SafeLogSqlFragment | null => {
+ if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') {
+ return null
+ }
+ try {
+ return safeSql`${attr(dotKey)} = ${lit(value)}`
+ } catch {
+ return null
+ }
+}
+
+const genOtelWhere = (table: LogsTableName, filters: Filters): SafeLogSqlFragment => {
+ const desc = OTEL_SOURCES[table]
+ const conditions: SafeLogSqlFragment[] = [safeSql`source = ${lit(desc.source)}`]
+ if (desc.baseCondition) conditions.push(desc.baseCondition)
+ conditions.push(
+ ...buildWhereClauses(
+ table,
+ filters,
+ desc.filterTemplates ?? COMMON_FILTERS,
+ resolveUnknownOtelClause
+ )
+ )
+ return safeSql`WHERE ${joinSqlFragments(conditions, ' AND ')}`
+}
+
+const genOtelSelectColumns = (table: LogsTableName): SafeLogSqlFragment => {
+ const columns: SafeLogSqlFragment[] = [
+ safeSql`id`,
+ safeSql`timestamp`,
+ safeSql`event_message`,
+ ...(OTEL_SOURCES[table].columns ?? []),
+ ]
+ return joinSqlFragments(columns, ', ')
+}
+
+export const genDefaultQueryOtel = (
+ table: LogsTableName,
+ filters: Filters,
+ limit: number = 100
+): SafeLogSqlFragment => {
+ return safeSql`-- Logs Preview Query (otel) [${lit(table)}]
+SELECT ${genOtelSelectColumns(table)}
+FROM logs
+${genOtelWhere(table, filters)}
+ORDER BY timestamp DESC
+LIMIT ${lit(limit)}`
+}
+
+export const genCountQueryOtel = (table: LogsTableName, filters: Filters): SafeLogSqlFragment => {
+ return safeSql`-- Logs Count Query (otel) [${lit(table)}]
+SELECT count() AS count FROM logs ${genOtelWhere(table, filters)}`
+}
+
+// Bucket by minute up to 12h, otherwise by hour (matches BigQuery).
+const otelChartTruncFn = (params: LogsEndpointParams): SafeLogSqlFragment => {
+ const ite = params.iso_timestamp_end ? dayjs(params.iso_timestamp_end) : dayjs()
+ const its = params.iso_timestamp_start ? dayjs(params.iso_timestamp_start) : dayjs()
+ const minuteDiff = ite.diff(its, 'minute')
+ const hourDiff = ite.diff(its, 'hour')
+ if (minuteDiff > 60 * 12) return safeSql`toStartOfHour`
+ if (hourDiff > 24 * 3) return safeSql`toStartOfDay`
+ return safeSql`toStartOfMinute`
+}
+
+export const genChartQueryOtel = (
+ table: LogsTableName,
+ params: LogsEndpointParams,
+ filters: Filters
+): SafeLogSqlFragment => {
+ const truncFn = otelChartTruncFn(params)
+ const desc = OTEL_SOURCES[table]
+ const err = desc.error ?? NO_MATCH
+ const warn = desc.warning ?? NO_MATCH
+ // The time range comes from the endpoint params, not the SQL.
+ return safeSql`-- Logs Chart Query (otel) [${lit(table)}]
+SELECT
+ ${truncFn}(timestamp) AS timestamp,
+ countIf(NOT ((${err}) OR (${warn}))) AS ok_count,
+ countIf(${err}) AS error_count,
+ countIf(${warn}) AS warning_count
+FROM logs
+${genOtelWhere(table, filters)}
+GROUP BY timestamp
+ORDER BY timestamp ASC`
+}
+
+// Reject non-uuid ids before putting them in the query (injection guard).
+export const genSingleLogQueryOtel = (id: string): SafeLogSqlFragment => {
+ if (!/^[0-9a-fA-F-]{1,64}$/.test(id)) {
+ throw new Error('Invalid logId')
+ }
+ return safeSql`-- Single Log Query (otel)
+SELECT id, timestamp, event_message, source, severity_text, log_attributes
+FROM logs
+WHERE id = ${lit(id)}
+LIMIT 1`
+}
+
+// Renderers and the pagination cursor expect a microsecond number.
+export const otelTimestampToMicros = (timestamp: unknown): number =>
+ parseOtelTimestamp(timestamp).getTime() * 1000
+
+export const mapOtelPreviewRow = (row: Record<string, any>): LogData => {
+ return { ...row, timestamp: otelTimestampToMicros(row.timestamp) } as LogData
+}
+
+// The api/database detail panels read a nested `metadata` shape, so rebuild it
+// from the flat attributes. Other tables use a flat row.
+// TODO: edge logs also carry the JWT/apikey keys, so the panel's API key / role
+// rows could be rebuilt here later.
+const SINGLE_LOG_METADATA: Partial<Record<QueryType, (attrs: Record<string, string>) => Metadata>> =
+ {
+ api: (attrs) => ({
+ request: [
+ {
+ method: attrs['request.method'] ?? null,
+ path: attrs['request.path'] ?? null,
+ search: attrs['request.search'] ?? null,
+ headers: [{ user_agent: attrs['request.headers.user_agent'] ?? null }],
+ },
+ ],
+ response: [
+ {
+ status_code: attrs['response.status_code'] ?? null,
+ // OTEL uses `sb_error_code`; BigQuery used `x_sb_error_code`. Fall back
+ // to the old key so the panel works during the migration.
+ headers: [
+ {
+ x_sb_error_code:
+ attrs['response.headers.sb_error_code'] ??
+ attrs['response.headers.x_sb_error_code'] ??
+ null,
+ },
+ ],
+ },
+ ],
+ }),
+ database: (attrs) => ({
+ parsed: [
+ {
+ hint: attrs['parsed.hint'] ?? null,
+ detail: attrs['parsed.detail'] ?? null,
+ query: attrs['parsed.query'] ?? null,
+ },
+ ],
+ }),
+ }
+
+// OTEL rows come back untyped from the analytics endpoint; validate the shape we
+// read instead of casting. log_attributes is a string-keyed string map.
+const OtelLogRowSchema = z.object({
+ id: z.string().optional(),
+ timestamp: z.union([z.string(), z.number()]).optional(),
+ event_message: z.string().optional(),
+ log_attributes: z.record(z.string(), z.string()).optional(),
+})
+
+export const mapOtelSingleLogToLegacy = (row: unknown, queryType?: QueryType): LogData => {
+ const parsed = OtelLogRowSchema.safeParse(row)
+ if (!parsed.success) {
+ console.error('Unexpected OTEL log row shape:', parsed.error.message)
+ return { id: '', timestamp: 0, event_message: '' }
+ }
+ const { id = '', timestamp, event_message = '', log_attributes = {} } = parsed.data
+ const base: LogData = { id, timestamp: otelTimestampToMicros(timestamp), event_message }
+
+ const buildMetadata = queryType ? SINGLE_LOG_METADATA[queryType] : undefined
+ if (buildMetadata) {
+ return { ...base, metadata: [buildMetadata(log_attributes)] }
+ }
+ return { ...base, ...log_attributes }
+}
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts
index 6fa1baa0be2..44be04b8b07 100644
--- a/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts
@@ -11,6 +11,7 @@ import {
AUTH_LOG_WARNING_CONDITION,
LogsTableName,
SQL_FILTER_TEMPLATES,
+ type SqlFilterEntry,
} from './Logs.constants'
import type { Filters, LogData, LogsEndpointParams, QueryType } from './Logs.types'
import { convertResultsToCSV } from '@/components/interfaces/SQLEditor/UtilityPanel/Results.utils'
@@ -66,43 +67,43 @@ const getDotKeys = (obj: { [k: string]: unknown }, parent?: string): string[] =>
})
}
+// Fallback for filter keys with no template: a plain `key = value`.
+// Drops the clause on non-scalar or un-encodable input.
+const defaultResolveUnknownClause = (dotKey: string, value: unknown): SafeLogSqlFragment | null => {
+ if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') {
+ return null
+ }
+ try {
+ return safeSql`${quotedIdent(dotKey)} = ${analyticsLiteral(value)}`
+ } catch {
+ return null
+ }
+}
+
/**
- * Root keys in the filter object are considered to be AND filters.
- * Nested keys under a root key are considered to be OR filters.
- *
- * For example:
- * ```
- * {my_value: 'something', nested: {id: 123, test: 123 }}
- * ```
- * This would be converted into `WHERE (my_value = 'something') and (id = 123 or test = 123)
- *
- * The template of the filter determines the actual filter statement. If no template is provided, a generic equality statement will be used.
- * This only applies for root keys of the filter.
- * For example:
- * ```
- * {'my.nested.value': 123}
- * ```
- * with no template, it will be converted into `WHERE (my.nested.value = 123)
+ * Builds the AND-joined WHERE clauses for a table's filters. Top-level keys are
+ * ANDed, nested keys under one key are ORed.
*
- * @returns a where statement with WHERE clause.
+ * `filterTemplates` and `resolveUnknownClause` can be passed in so the OTEL
+ * builders reuse this with their own ClickHouse conditions. Defaults keep the
+ * BigQuery behavior.
*/
-const buildWhereClauses = (table: LogsTableName, filters: Filters): SafeLogSqlFragment[] => {
+export const buildWhereClauses = (
+ table: LogsTableName,
+ filters: Filters,
+ filterTemplates: Record<string, SqlFilterEntry> = SQL_FILTER_TEMPLATES[table],
+ resolveUnknownClause: (
+ dotKey: string,
+ value: unknown
+ ) => SafeLogSqlFragment | null = defaultResolveUnknownClause
+): SafeLogSqlFragment[] => {
const keys = Object.keys(filters)
- const filterTemplates = SQL_FILTER_TEMPLATES[table]
const _resolveTemplateToStatement = (dotKey: string): SafeLogSqlFragment | null => {
const template = filterTemplates[dotKey]
const value = get(filters, dotKey)
if (template === undefined) {
- // Unknown filter from a filter override — generic equality predicate; drop on bad input.
- if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') {
- return null
- }
- try {
- return safeSql`${quotedIdent(dotKey)} = ${analyticsLiteral(value)}`
- } catch {
- return null
- }
+ return resolveUnknownClause(dotKey, value)
}
if (typeof template === 'function') {
@@ -141,8 +142,13 @@ const buildWhereClauses = (table: LogsTableName, filters: Filters): SafeLogSqlFr
.filter((s) => s !== null)
}
-const genWhereStatement = (table: LogsTableName, filters: Filters): SafeLogSqlFragment => {
- const clauses = buildWhereClauses(table, filters)
+export const genWhereStatement = (
+ table: LogsTableName,
+ filters: Filters,
+ filterTemplates?: Record<string, SqlFilterEntry>,
+ resolveUnknownClause?: (dotKey: string, value: unknown) => SafeLogSqlFragment | null
+): SafeLogSqlFragment => {
+ const clauses = buildWhereClauses(table, filters, filterTemplates, resolveUnknownClause)
return clauses.length > 0 ? safeSql`where ${joinSqlFragments(clauses, ' and ')}` : safeSql``
}
diff --git a/apps/studio/data/logs/otel-inspection.utils.ts b/apps/studio/data/logs/otel-inspection.utils.ts
index 125d9ebb512..8963d25997b 100644
--- a/apps/studio/data/logs/otel-inspection.utils.ts
+++ b/apps/studio/data/logs/otel-inspection.utils.ts
@@ -12,6 +12,17 @@ export type OtelLogRow = {
log_attributes?: Record<string, any>
}
+// OTEL timestamps arrive as an ISO string (sometimes space-separated, no zone)
+// or as numeric microseconds. Strings have a `T` or `-`; anything else is micros.
+export function parseOtelTimestamp(timestamp: unknown): Date {
+ const ts = String(timestamp ?? '')
+ const looksLikeIso = /[T-]/.test(ts)
+ if (!looksLikeIso) return new Date(Number(ts) / 1000)
+ const withT = ts.includes(' ') ? ts.replace(' ', 'T') : ts
+ const hasTz = /Z$|[+-]\d{2}:?\d{2}$/.test(withT)
+ return new Date(hasTz ? withT : `${withT}Z`)
+}
+
const httpStatusToLevel = (status: number): Level => {
if (status >= 500) return 'error'
if (status >= 400) return 'warning'
diff --git a/apps/studio/data/logs/unified-logs-infinite-query.ts b/apps/studio/data/logs/unified-logs-infinite-query.ts
index edf5d868175..61af3c1b476 100644
--- a/apps/studio/data/logs/unified-logs-infinite-query.ts
+++ b/apps/studio/data/logs/unified-logs-infinite-query.ts
@@ -4,6 +4,7 @@ import { useFlag } from 'common'
import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'
+import { parseOtelTimestamp } from './otel-inspection.utils'
import { analyticsLiteral, safeSql } from './safe-analytics-sql'
import { extractLogMetadata } from './unified-logs.utils'
import { getUnifiedLogsQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
@@ -124,16 +125,7 @@ export async function getUnifiedLogs(
const resultData = data?.result ?? []
const result = resultData.map((row: any) => {
- // 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)
+ const date = parseOtelTimestamp(row.timestamp)
const { status, method, pathname } = extractLogMetadata(row)
diff --git a/apps/studio/hooks/analytics/useLogsPreview.tsx b/apps/studio/hooks/analytics/useLogsPreview.tsx
index 6dc632f4f14..4521066f0aa 100644
--- a/apps/studio/hooks/analytics/useLogsPreview.tsx
+++ b/apps/studio/hooks/analytics/useLogsPreview.tsx
@@ -1,4 +1,5 @@
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
+import { useFlag } from 'common'
import dayjs from 'dayjs'
import { useCallback, useMemo, useState } from 'react'
@@ -24,7 +25,14 @@ import {
genCountQuery,
genDefaultQuery,
} from '@/components/interfaces/Settings/Logs/Logs.utils'
+import {
+ genChartQueryOtel,
+ genCountQueryOtel,
+ genDefaultQueryOtel,
+ mapOtelPreviewRow,
+} from '@/components/interfaces/Settings/Logs/Logs.utils.otel'
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
+import { logsAllEndpointUrl, pickLogsQueryBuilder } from '@/data/logs/logs-endpoint'
interface LogsPreviewHook {
logData: LogData[]
@@ -55,6 +63,10 @@ function useLogsPreview({
const defaultHelper = getDefaultHelper(PREVIEWER_DATEPICKER_HELPERS)
const [latestRefresh, setLatestRefresh] = useState(new Date().toISOString())
+ // When on, query the OTEL endpoint instead of BigQuery. logsAllEndpointUrl is
+ // inlined in each queryFn (not hoisted) so useOtel stays the only query dep.
+ const useOtel = useFlag('otelLegacyLogs')
+
const {
timestampStart: urlTimestampStart,
timestampEnd: urlTimestampEnd,
@@ -81,8 +93,13 @@ function useLogsPreview({
)
const defaultSql = useMemo(
- () => genDefaultQuery(table, mergedFilters, limit),
- [table, mergedFilters, limit]
+ () =>
+ pickLogsQueryBuilder(useOtel, genDefaultQueryOtel, genDefaultQuery)(
+ table,
+ mergedFilters,
+ limit
+ ),
+ [useOtel, table, mergedFilters, limit]
)
const params: LogsEndpointParams = useMemo(
@@ -95,8 +112,8 @@ function useLogsPreview({
)
const queryKey = useMemo(
- () => ['projects', projectRef, 'logs', params, defaultSql],
- [projectRef, params, defaultSql]
+ () => ['projects', projectRef, 'logs', params, defaultSql, { otel: useOtel }],
+ [projectRef, params, defaultSql, useOtel]
)
const {
@@ -113,14 +130,18 @@ function useLogsPreview({
queryFn: async ({ signal, pageParam }) => {
const data = await executeAnalyticsSql({
projectRef,
- endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
+ endpoint: logsAllEndpointUrl(useOtel),
sql: defaultSql,
iso_timestamp_start: params.iso_timestamp_start ?? '',
iso_timestamp_end: (pageParam || params.iso_timestamp_end) ?? '',
method: 'get',
signal,
})
- return data as unknown as Logs
+ const logs = data as unknown as Logs
+ if (useOtel && logs?.result) {
+ return { ...logs, result: logs.result.map(mapOtelPreviewRow) } as Logs
+ }
+ return logs
},
refetchOnWindowFocus: false,
initialPageParam: undefined as string | undefined,
@@ -153,7 +174,10 @@ function useLogsPreview({
return { logData, error, oldestTimestamp }
}, [data?.pages])
- const countQuerySql = useMemo(() => genCountQuery(table, mergedFilters), [table, mergedFilters])
+ const countQuerySql = useMemo(
+ () => pickLogsQueryBuilder(useOtel, genCountQueryOtel, genCountQuery)(table, mergedFilters),
+ [useOtel, table, mergedFilters]
+ )
const countQueryKey = useMemo(
() => [
'projects',
@@ -166,9 +190,10 @@ function useLogsPreview({
iso_timestamp_end: timestampEnd,
table,
mergedFilters,
+ otel: useOtel,
},
],
- [projectRef, countQuerySql, latestRefresh, timestampEnd, table, mergedFilters]
+ [projectRef, countQuerySql, latestRefresh, timestampEnd, table, mergedFilters, useOtel]
)
const { data: countData } = useQuery({
@@ -176,7 +201,7 @@ function useLogsPreview({
queryFn: async ({ signal }) => {
const data = await executeAnalyticsSql({
projectRef,
- endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
+ endpoint: logsAllEndpointUrl(useOtel),
sql: countQuerySql,
iso_timestamp_start: latestRefresh,
iso_timestamp_end: timestampEnd ?? '',
@@ -193,8 +218,9 @@ function useLogsPreview({
const newCount = countData?.result?.[0]?.count ?? 0
const chartQuery = useMemo(
- () => genChartQuery(table, params, mergedFilters),
- [table, params, mergedFilters]
+ () =>
+ pickLogsQueryBuilder(useOtel, genChartQueryOtel, genChartQuery)(table, params, mergedFilters),
+ [useOtel, table, params, mergedFilters]
)
const chartQueryKey = useMemo(
() => [
@@ -206,9 +232,10 @@ function useLogsPreview({
sql: chartQuery,
iso_timestamp_start: timestampStart,
iso_timestamp_end: timestampEnd,
+ otel: useOtel,
},
],
- [projectRef, chartQuery, timestampStart, timestampEnd]
+ [projectRef, chartQuery, timestampStart, timestampEnd, useOtel]
)
const { data: eventChartResponse, refetch: refreshEventChart } = useQuery({
@@ -216,7 +243,7 @@ function useLogsPreview({
queryFn: async ({ signal }) => {
const data = await executeAnalyticsSql({
projectRef,
- endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
+ endpoint: logsAllEndpointUrl(useOtel),
sql: chartQuery,
iso_timestamp_start: timestampStart,
iso_timestamp_end: timestampEnd ?? '',
diff --git a/apps/studio/hooks/analytics/useSingleLog.tsx b/apps/studio/hooks/analytics/useSingleLog.tsx
index 136194cee68..1ea50e7b2d2 100644
--- a/apps/studio/hooks/analytics/useSingleLog.tsx
+++ b/apps/studio/hooks/analytics/useSingleLog.tsx
@@ -1,4 +1,6 @@
import { useQuery } from '@tanstack/react-query'
+import { useFlag } from 'common'
+import { useMemo } from 'react'
import { LOGS_TABLES } from '@/components/interfaces/Settings/Logs/Logs.constants'
import type {
@@ -8,7 +10,12 @@ import type {
QueryType,
} from '@/components/interfaces/Settings/Logs/Logs.types'
import { genSingleLogQuery } from '@/components/interfaces/Settings/Logs/Logs.utils'
+import {
+ genSingleLogQueryOtel,
+ mapOtelSingleLogToLegacy,
+} from '@/components/interfaces/Settings/Logs/Logs.utils.otel'
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
+import { logsAllEndpointUrl } from '@/data/logs/logs-endpoint'
import { safeSql } from '@/data/logs/safe-analytics-sql'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
@@ -32,7 +39,23 @@ function useSingleLog({
paramsToMerge,
}: SingleLogParams): SingleLogHook {
const table = queryType ? LOGS_TABLES[queryType] : undefined
- const sql = id && table ? genSingleLogQuery(table, id) : safeSql``
+
+ // When on, fetch the log from the OTEL endpoint instead of BigQuery.
+ const useOtel = useFlag('otelLegacyLogs')
+ const endpoint = logsAllEndpointUrl(useOtel)
+
+ const sql = useMemo(() => {
+ if (!id || !table) return safeSql``
+ if (useOtel) {
+ try {
+ return genSingleLogQueryOtel(id)
+ } catch {
+ // Malformed (non-uuid) id — emit nothing rather than throwing in render.
+ return safeSql``
+ }
+ }
+ return genSingleLogQuery(table, id)
+ }, [id, table, useOtel])
const enabled = Boolean(id && table)
@@ -56,11 +79,12 @@ function useSingleLog({
queryType,
paramsToMerge?.iso_timestamp_start,
paramsToMerge?.iso_timestamp_end,
+ { otel: useOtel },
],
queryFn: async ({ signal }) => {
const data = await executeAnalyticsSql({
projectRef,
- endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
+ endpoint,
sql,
iso_timestamp_start: paramsToMerge?.iso_timestamp_start ?? '',
iso_timestamp_end: paramsToMerge?.iso_timestamp_end ?? '',
@@ -76,7 +100,8 @@ function useSingleLog({
})
let error: null | string | object = rcError ? (rcError as any).message : null
- const result = data?.result ? data.result[0] : undefined
+ const rawResult = data?.result ? data.result[0] : undefined
+ const result = rawResult && useOtel ? mapOtelSingleLogToLegacy(rawResult, queryType) : rawResult
return {
data: !!result