Files
supabase/.agents/skills/clickhouse-logs-queries/references/codebase-integration.md

100 lines
4.9 KiB
Markdown

# 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](../../safe-sql-execution/SKILL.md) (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 stats
- `useFlag('otelReports')` — report charts under `data/reports/**` and
`routes/project/$ref/observability/**`
- `useUnifiedLogsBackend()` (`otelUnifiedLogs`, defaults `true`) — 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)` — returns `logs.all.otel` when `useOtel`,
otherwise legacy `logs.all`.
- `pickLogsQueryBuilder(useOtel, otel, bq)` — selects the OTEL or BigQuery
builder while preserving the type.
```ts
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-specific `log_attributes[...]` lookups aliased to the leaf
names renderers expect.
- `mapOtelPreviewRow`, `mapOtelSingleLogToLegacy`, `otelTimestampToMicros` — JS
normalization layer. `timestamp` must end up as a microsecond number for the
pagination cursor and renderers, so reuse `otelTimestampToMicros` rather 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 with `useFlag('otelLegacyLogs')` (keeping BigQuery SQL
when `otelLegacyLogs` is 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.ts` sanitizers (see `safe-sql-execution`), never string concatenation.
- [ ] The query filters by `source` (when scoping to a service) and includes a `LIMIT`.
- [ ] Numeric `log_attributes` values are wrapped in `toInt32OrZero`.
- [ ] Endpoint and builder stay paired via `logsAllEndpointUrl` / `pickLogsQueryBuilder` off the surface's ConfigCat flag (`otelLegacyLogs` or `otelReports`), or `logsAllEndpointUrl(true)` for OTEL-only queries.
- [ ] The React Query key distinguishes OTEL and BigQuery paths.
- [ ] `timestamp` is normalized to micros for table/cursor rows, and migrated report charts verify axis timestamp units plus companion "Open in Logs Explorer" SQL gated on `useFlag('otelLegacyLogs')`.
- [ ] Unit tests assert the generated SQL string (see `Logs.utils.otel.test.ts` and `safe-analytics-sql.test.ts`).