From 1ef66de793bb0f9444ddacdfd58cbfd0d30228b2 Mon Sep 17 00:00:00 2001 From: "kemal.earth" <606977+kemaldotearth@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:38:33 +0000 Subject: [PATCH] feat(studio): add trace span route query (#54391) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem The Unified Logs trace tab needs the spans for a request's trace. Nothing in Studio fetches them yet. Part 2 of 3. Data layer only — the tab that consumes this lands in part 3. ## Solution Adds `unifiedLogTraceSpansQueryOptions`, fetching `GET /v2/projects/{ref}/analytics/traces/{trace_id}`, plus the `traceSpans` query key. Two choices worth flagging: **Fetched and parsed directly, not through the typed client.** The route is live in the v2 OpenAPI spec, but it isn't in the committed `api-types` — and `get()` is typed off the generated `paths`, so it can't address a route that isn't there. Getting it there means regenerating `api-types`, which currently drags in a large amount of unrelated production drift and breaks Studio's typecheck in three places. That's worth doing on its own, not as a side effect of this. So the query goes through `fetchHandler` and parses the response with zod, following the `platform-apps/platform-app-query.ts` precedent. The parse likely stays even after a regen: the spec declares no `additionalProperties` on a span's `attributes`, so the generated type would describe only the seven known keys and none of the OTel attributes the trace view actually reads (`http.route`, `url.full`, `db.*`, and whatever else a service reports). `.passthrough()` keeps them. It also means the parse is load-bearing — it's the only thing validating the response shape at runtime, so a backend change surfaces as an error at the boundary rather than a build failure. **A temporary knip entry.** Nothing imports the query until part 3, so knip reports it as an unused file and fails the build. Per `apps/studio/AGENTS.md`, it's listed in `knip.jsonc` with a `used from #NNN` comment; part 3 deletes that entry when it adds the importer. ## Review instructions 1. Check the zod schema against the live spec — it should match field for field: ```bash curl -s https://api.supabase.com/api/v2-json \ | jq '.components.schemas.ListSpansResponse_Output' ``` 2. Confirm the knip entry in `knip.jsonc` is marked temporary and points at part 3. 3. Run `pnpm knip --workspace apps/studio` and confirm it passes. ## Checklist Check all before review: - [ ] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) --- apps/studio/data/logs/keys.ts | 2 + .../logs/unified-log-trace-spans-query.ts | 64 +++++++++++++++++++ knip.jsonc | 2 + 3 files changed, 68 insertions(+) create mode 100644 apps/studio/data/logs/unified-log-trace-spans-query.ts diff --git a/apps/studio/data/logs/keys.ts b/apps/studio/data/logs/keys.ts index 11cd833acbf..86b0a170a95 100644 --- a/apps/studio/data/logs/keys.ts +++ b/apps/studio/data/logs/keys.ts @@ -97,4 +97,6 @@ export const logsKeys = { logId, ...(searchParams ? [searchParams].filter(Boolean) : []), ] as const, + traceSpans: (projectRef: string | undefined, traceId: string | undefined) => + ['projects', projectRef, 'unified-logs', 'trace-spans', traceId] as const, } diff --git a/apps/studio/data/logs/unified-log-trace-spans-query.ts b/apps/studio/data/logs/unified-log-trace-spans-query.ts new file mode 100644 index 00000000000..94dfc236f82 --- /dev/null +++ b/apps/studio/data/logs/unified-log-trace-spans-query.ts @@ -0,0 +1,64 @@ +import { logsKeys } from './keys' +import { UNIFIED_LOGS_QUERY_OPTIONS } from './unified-logs-infinite-query' +import { get, handleError } from '@/data/fetchers' +import { grantedQueryOptions, type GrantedQueryCallerOptions } from '@/data/granted-query-options' +import { IS_PLATFORM } from '@/lib/constants' +import type { StudioError } from '@/lib/errors/studio-errors' +import type { GrantFor } from '@/lib/grants' + +export type UnifiedLogTraceSpansGrant = GrantFor< + 'get', + '/v2/projects/{ref}/analytics/traces/{trace_id}' +> + +export type UnifiedLogTraceSpansVariables = { + projectRef?: string + traceId?: string + grant?: UnifiedLogTraceSpansGrant +} + +async function getUnifiedLogTraceSpans( + { + projectRef, + traceId, + grant, + }: { projectRef: string; traceId: string; grant: UnifiedLogTraceSpansGrant }, + signal?: AbortSignal +) { + const { data, error } = await get('/v2/projects/{ref}/analytics/traces/{trace_id}', { + params: { path: { ref: projectRef, trace_id: traceId } }, + signal, + grant, + }) + + if (error) return handleError(error) + return data +} + +export type UnifiedLogTraceSpansData = Awaited> +export type UnifiedLogTraceSpansError = StudioError + +// The spec declares a span's `attributes` without `additionalProperties`, so the generated type is +// closed to the seven keys below. Spans carry further OTel attributes (`http.route`, `url.full`, +// `db.*`) that consumers widen where they read them, until the spec describes them. +export type TraceSpan = UnifiedLogTraceSpansData['data'][number] + +export const unifiedLogTraceSpansQueryOptions = ( + { projectRef, traceId, grant }: UnifiedLogTraceSpansVariables, + options?: GrantedQueryCallerOptions< + UnifiedLogTraceSpansData, + UnifiedLogTraceSpansError, + TData, + ReturnType + > +) => + grantedQueryOptions( + { + queryKey: logsKeys.traceSpans(projectRef, traceId), + queryFn: + projectRef && traceId && grant + ? ({ signal }) => getUnifiedLogTraceSpans({ projectRef, traceId, grant }, signal) + : undefined, + }, + { enabled: IS_PLATFORM, ...UNIFIED_LOGS_QUERY_OPTIONS, ...options } + ) diff --git a/knip.jsonc b/knip.jsonc index 6bc0d2030d7..be2baeb66d8 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -41,6 +41,8 @@ "data/__templates/**", // evals are run from a GitHub action, not from the app "evals/**", + // used from #54393 — delete this entry there + "data/logs/unified-log-trace-spans-query.ts", ], // Narrowly scoped suppressions: one issue type, one path. Preferred over // `ignore` (which drops the file from the project entirely, hiding the