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 } )