4.9 KiB
Wiring a logs query in the Studio codebase
This covers wiring analytics log SQL inside apps/studio so it is branded,
routed to the right endpoint, and consistent with existing OTEL builders. Read
this when editing TypeScript that builds or runs a logs query, not when writing
a standalone query in the Logs Explorer UI.
Branded SQL & execution boundary
All analytics log SQL must be a SafeLogSqlFragment built with
apps/studio/data/logs/safe-analytics-sql.ts and executed through
executeAnalyticsSql (apps/studio/data/logs/execute-analytics-sql.ts). See
safe-sql-execution (Analytics SQL section)
for the complete helper reference (safeSql, analyticsLiteral, quotedIdent,
keyword, joinSqlFragments), ESLint checks, and acceptUntrustedLogsSql
security rules.
Pick the endpoint and builder by flag
The ClickHouse path is gated by a ConfigCat flag (useFlag(...) from 'common'
— ConfigCat, not PostHog) depending on the surface:
useFlag('otelLegacyLogs')— Logs Explorer, Logs Previewer, Single Log, Auth Logs, SQL Editor Logs, Edge Functions statsuseFlag('otelReports')— report charts underdata/reports/**androutes/project/$ref/observability/**useUnifiedLogsBackend()(otelUnifiedLogs, defaultstrue) — Unified Logs
Keep the BigQuery path working when the flag is off. Two helpers in
apps/studio/data/logs/logs-endpoint.ts keep the endpoint and SQL dialect
paired:
logsAllEndpointUrl(useOtel)— returnslogs.all.otelwhenuseOtel, otherwise legacylogs.all.pickLogsQueryBuilder(useOtel, otel, bq)— selects the OTEL or BigQuery builder while preserving the type.
import { useFlag } from 'common'
import { LogsTableName } from '@/components/interfaces/Settings/Logs/Logs.constants'
import { genDefaultQuery } from '@/components/interfaces/Settings/Logs/Logs.utils'
import { genDefaultQueryOtel } from '@/components/interfaces/Settings/Logs/Logs.utils.otel'
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from '@/data/logs/logs-endpoint'
const useOtel = useFlag('otelLegacyLogs') // or 'otelReports' for report charts
const builder = pickLogsQueryBuilder(useOtel, genDefaultQueryOtel, genDefaultQuery)
const endpoint = logsAllEndpointUrl(useOtel)
// Include { otel: useOtel } in the React Query key so the two paths cache separately
const sql = builder(LogsTableName.EDGE, filters, 100)
await executeAnalyticsSql({
projectRef,
endpoint,
sql,
iso_timestamp_start,
iso_timestamp_end,
})
For OTEL-only queries that intentionally run ClickHouse SQL directly (no
BigQuery fallback), pin endpoint: logsAllEndpointUrl(true) (see
apps/studio/data/logs/otel-log-keys-query.ts).
Follow the existing OTEL builders & chart migrations
When you need a new query shape, mirror the generators in
apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.ts rather than
inventing a parallel style:
genDefaultQueryOtel,genCountQueryOtel,genChartQueryOtel,genSingleLogQueryOtel— row/count/chart/single-log builders selecting real columns plus source-specificlog_attributes[...]lookups aliased to the leaf names renderers expect.mapOtelPreviewRow,mapOtelSingleLogToLegacy,otelTimestampToMicros— JS normalization layer.timestampmust end up as a microsecond number for the pagination cursor and renderers, so reuseotelTimestampToMicrosrather than parsing ISO strings by hand.- Migrating report charts: gate report queries with
useFlag('otelReports')and verify timestamp units on chart axes. Select companion "Open in Logs Explorer" deep-link SQL withuseFlag('otelLegacyLogs')(keeping BigQuery SQL whenotelLegacyLogsis off) so reports and Logs Explorer never send the wrong dialect when the two flags differ.
Read apps/studio/data/logs/otel-log-keys-query.ts (fetchOtelLogKeys /
useOtelLogKeysQuery) as the canonical example of a small, correctly branded
OTEL query.
Checklist
- Every dynamic value goes through
safe-analytics-sql.tssanitizers (seesafe-sql-execution), never string concatenation. - The query filters by
source(when scoping to a service) and includes aLIMIT. - Numeric
log_attributesvalues are wrapped intoInt32OrZero. - Endpoint and builder stay paired via
logsAllEndpointUrl/pickLogsQueryBuilderoff the surface's ConfigCat flag (otelLegacyLogsorotelReports), orlogsAllEndpointUrl(true)for OTEL-only queries. - The React Query key distinguishes OTEL and BigQuery paths.
timestampis normalized to micros for table/cursor rows, and migrated report charts verify axis timestamp units plus companion "Open in Logs Explorer" SQL gated onuseFlag('otelLegacyLogs').- Unit tests assert the generated SQL string (see
Logs.utils.otel.test.tsandsafe-analytics-sql.test.ts).