mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 23:49:03 +00:00
## Problem Unified Logs can show the logs correlated with a request, but not the trace behind it. Where time actually went across services isn't visible anywhere in Studio. Part 3 of 3, and the one that ships the feature. Builds on the `Trace` primitives from part 1 and the spans query from part 2. ## Solution Adds a **Trace** tab to the log detail panel, composed entirely from the shared `Trace` primitives — header, summary strip, waterfall, inspector — and ships it as a feature preview. **Resolving the trace id.** The selected log's own trace id wins. A related log's id is used only as a fallback, and only when there's exactly one candidate — picking between several would be a guess. The ids come from the correlated logs the Timeline tab already fetches, so the two tabs share a cache entry instead of each making a request. **Mapping spans.** `RequestTrace.utils.ts` turns the endpoint's spans into `Trace`'s `Span`/`TraceData` shape. Three things worth noting: - Span names come from OTel attributes where the raw name isn't useful on its own — a span named `GET` becomes `GET /rest/v1/orders` from `http.route`, falling back to the path from `url.full` for outbound fetches that have no server-matched route. - `Date.parse` only resolves whole milliseconds, so two spans less than a millisecond apart would round to the same instant and render stacked. The fractional part of the ISO string is added back. - Spans with a missing or invalid start time are counted and reported rather than dropped silently. **States.** The tab is always present when the Timeline is, so a request with no trace shows an empty state rather than a tab that appears and disappears as the trace resolves. Separate states cover: no trace id, a trace with no spans, spans that exist but can't be placed, and a failed fetch. `meta.partial` surfaces as a badge, since a trace can be incomplete while spans are still being collected. **Feature preview.** Listed under Observability as "Request traces", following the same pattern as the Explorer and Storage versioning previews. Two gates gives the kill switch and the opt-in independently: - `enableTracesWaterfall` (ConfigCat) controls whether the preview is listed at all - The user's opt-in controls the tab, stored in local storage and preserved across sign-out It's not default opt-in, so nobody gets the tab without choosing it — the endpoint is still alpha. Also removes the temporary `knip.jsonc` entry added in part 2, now that the query has an importer. ## Review instructions 1. Open the feature preview modal and confirm **Request traces** is listed under Observability, with a panel describing the feature. Enable it. 2. Open **Logs** and select a request log that has a trace id. 3. Confirm the **Trace** tab appears beside Timeline, with the header, the summary strip (trace start, duration, services, depth, span count) and the waterfall. 4. Select a span and confirm the inspector opens with its attributes and events. 5. Select a request with no trace id and confirm the tab is still there, showing an empty state. 6. Turn the preview off and confirm the tab disappears while Timeline is unaffected. 7. `pnpm --filter studio test components/interfaces/UnifiedLogs/components/RequestTrace.utils.test.ts components/interfaces/UnifiedLogs/components/RequestTimeline.platform.test.tsx` Note the traces endpoint is in alpha: a request only has a trace when its logs carry a trace ID, so landing on the empty state in step 2 isn't necessarily this PR misbehaving. The component tests cover every state against mocked responses.
212 lines
8.2 KiB
TypeScript
212 lines
8.2 KiB
TypeScript
import { useRef, useState } from 'react'
|
|
import {
|
|
ResizableHandle,
|
|
ResizablePanel,
|
|
Tabs,
|
|
TabsContent,
|
|
TabsIndicator,
|
|
TabsList,
|
|
TabsTrigger,
|
|
} from 'ui'
|
|
|
|
import { LogDetail } from './components/LogDetail'
|
|
import { LogStepSummary } from './components/LogTimeline'
|
|
import { LogUser } from './components/LogUser'
|
|
import { LogTimestampHeader, RequestTimeline } from './components/RequestTimeline'
|
|
import { RequestTrace } from './components/RequestTrace'
|
|
import { useLogEnrichment } from './components/useLogEnrichment'
|
|
import { LogSelectionActions } from './LogSelectionActions'
|
|
import {
|
|
LogPanelCloseButton,
|
|
LogPanelNavigation,
|
|
} from './ServiceFlow/components/ServiceFlowPanelControls'
|
|
import { getLogDataForMetadataVisibility } from './ServiceFlowPanel.utils'
|
|
import { ColumnSchema } from './UnifiedLogs.schema'
|
|
import { QuerySearchParamsType } from './UnifiedLogs.types'
|
|
import { getEventMessageDisplay, getRawLogData } from './UnifiedLogs.utils'
|
|
import { useIsTracesEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
|
|
import { JsonCodeBlock } from '@/components/ui/JsonCodeBlock'
|
|
import { ShortcutBadge } from '@/components/ui/ShortcutBadge'
|
|
import { useUnifiedLogsBackend } from '@/data/logs/unified-logs-infinite-query'
|
|
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
|
|
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
|
|
|
|
const TAB_TRIGGER_CLASS_NAME = 'py-2.5 data-[state=active]:shadow-none'
|
|
|
|
interface ServiceFlowPanelProps {
|
|
dock: 'bottom' | 'right'
|
|
setDock: (value: 'bottom' | 'right') => void
|
|
selectedRows: ColumnSchema[]
|
|
searchParameters: QuerySearchParamsType
|
|
}
|
|
|
|
export function ServiceFlowPanel({
|
|
dock,
|
|
setDock,
|
|
selectedRows,
|
|
searchParameters,
|
|
}: ServiceFlowPanelProps) {
|
|
const [activeTab, setActiveTab] = useState('overview')
|
|
const overviewTabRef = useRef<HTMLButtonElement>(null)
|
|
|
|
const [openedLog, setOpenedLog] = useState<{ fromId: string; log: ColumnSchema } | null>(null)
|
|
const isRequestTimelineEnabled = useUnifiedLogsBackend(searchParameters)
|
|
const isTracesEnabled = useIsTracesEnabled()
|
|
const hasMultiple = selectedRows.length > 1
|
|
const selectedRow = selectedRows[0]
|
|
const activeLog =
|
|
(openedLog && openedLog.fromId === selectedRow?.id ? openedLog.log : undefined) ?? selectedRow
|
|
const isProcessLog = selectedRow?.log_type === 'compute' || selectedRow?.log_type === 'pipelines'
|
|
const showTimeline = isRequestTimelineEnabled && !isProcessLog
|
|
// Always present when the Timeline is: a request with no trace shows an empty state rather
|
|
// than a tab that appears and disappears as the trace resolves.
|
|
const showTrace = showTimeline && isTracesEnabled
|
|
const showUser = !isProcessLog
|
|
|
|
const tabs = [
|
|
'overview',
|
|
'raw-json',
|
|
...(showTimeline ? ['timeline'] : []),
|
|
...(showTrace ? ['trace'] : []),
|
|
...(showUser ? ['user'] : []),
|
|
]
|
|
const currentTab = tabs.includes(activeTab) ? activeTab : 'overview'
|
|
const title = hasMultiple
|
|
? `${selectedRows.length} logs selected`
|
|
: getEventMessageDisplay(activeLog?.log_type ?? '', activeLog?.event_message).message ||
|
|
activeLog?.id
|
|
|
|
const handleSelectTimelineLog = (log: ColumnSchema) => {
|
|
if (!selectedRow) return
|
|
setOpenedLog(log.id === selectedRow.id ? null : { fromId: selectedRow.id, log })
|
|
setActiveTab('overview')
|
|
overviewTabRef.current?.focus()
|
|
}
|
|
const { logsMetadata } = useIsFeatureEnabled(['logs:metadata'])
|
|
const selectedJson = selectedRows.map((row) =>
|
|
getLogDataForMetadataVisibility(getRawLogData(row), logsMetadata)
|
|
)
|
|
|
|
return (
|
|
<>
|
|
<ResizableHandle withHandle />
|
|
<ResizablePanel
|
|
id="log-sidepanel"
|
|
defaultSize={400}
|
|
minSize={dock === 'bottom' ? 300 : 400}
|
|
className="bg-card"
|
|
>
|
|
<div className="flex h-full flex-col overflow-hidden">
|
|
{/* Matches the table header height so the title lines up with the column labels */}
|
|
<div className="flex h-9 min-w-0 shrink-0 items-center gap-3 px-4">
|
|
<div className="flex min-w-0 flex-1" role="status" title={title}>
|
|
{!hasMultiple && activeLog ? (
|
|
<OpenLogSummary log={activeLog} searchParameters={searchParameters} />
|
|
) : (
|
|
<span className="truncate text-sm font-medium leading-6 text-foreground">
|
|
{title}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<div className="-mr-1 flex h-6 shrink-0 items-center gap-1">
|
|
<LogSelectionActions rows={hasMultiple || !activeLog ? selectedRows : [activeLog]} />
|
|
<LogPanelCloseButton />
|
|
</div>
|
|
</div>
|
|
{hasMultiple ? (
|
|
<div
|
|
className="min-h-0 flex-1 overflow-auto"
|
|
role="region"
|
|
aria-label="Selected logs JSON"
|
|
>
|
|
<JsonCodeBlock>{JSON.stringify(selectedJson, null, 2)}</JsonCodeBlock>
|
|
</div>
|
|
) : (
|
|
<Tabs
|
|
value={currentTab}
|
|
onValueChange={setActiveTab}
|
|
className="flex min-h-0 flex-1 flex-col"
|
|
>
|
|
<TabsList className="shrink-0 gap-x-5 px-4">
|
|
<TabsTrigger
|
|
ref={overviewTabRef}
|
|
value="overview"
|
|
className={TAB_TRIGGER_CLASS_NAME}
|
|
>
|
|
Overview
|
|
</TabsTrigger>
|
|
<TabsTrigger value="raw-json" className={TAB_TRIGGER_CLASS_NAME}>
|
|
Raw JSON
|
|
</TabsTrigger>
|
|
{showTimeline && (
|
|
<TabsTrigger value="timeline" className={TAB_TRIGGER_CLASS_NAME}>
|
|
Timeline
|
|
</TabsTrigger>
|
|
)}
|
|
{showTrace && (
|
|
<TabsTrigger value="trace" className={TAB_TRIGGER_CLASS_NAME}>
|
|
Trace
|
|
</TabsTrigger>
|
|
)}
|
|
{showUser && (
|
|
<TabsTrigger value="user" className={TAB_TRIGGER_CLASS_NAME}>
|
|
User
|
|
</TabsTrigger>
|
|
)}
|
|
<TabsIndicator />
|
|
</TabsList>
|
|
{tabs.map((tab) => (
|
|
<TabsContent
|
|
key={`${tab}-${selectedRow?.id}`}
|
|
value={tab}
|
|
className="mt-0 min-h-0 flex-1 overflow-auto"
|
|
>
|
|
{tab === 'timeline' && selectedRow && activeLog && (
|
|
<RequestTimeline
|
|
row={selectedRow}
|
|
activeLog={activeLog}
|
|
onSelectLog={handleSelectTimelineLog}
|
|
/>
|
|
)}
|
|
{tab === 'trace' && selectedRow && <RequestTrace row={selectedRow} />}
|
|
{tab === 'user' && activeLog && (
|
|
<LogUser row={activeLog} searchParameters={searchParameters} />
|
|
)}
|
|
{(tab === 'overview' || tab === 'raw-json') && activeLog && (
|
|
<LogDetail
|
|
row={activeLog}
|
|
tab={tab}
|
|
searchParameters={searchParameters}
|
|
overviewHeader={<LogTimestampHeader log={activeLog} />}
|
|
/>
|
|
)}
|
|
</TabsContent>
|
|
))}
|
|
</Tabs>
|
|
)}
|
|
<div className="flex shrink-0 items-center gap-2 border-t py-1.5 pl-4 pr-3 text-xs text-foreground-lighter">
|
|
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-2">
|
|
<ShortcutBadge shortcutId={SHORTCUT_IDS.UNIFIED_LOGS_EXTEND_PREV_ROW} />
|
|
<ShortcutBadge shortcutId={SHORTCUT_IDS.UNIFIED_LOGS_EXTEND_NEXT_ROW} />
|
|
<span>Extend selection</span>
|
|
</div>
|
|
<LogPanelNavigation dock={dock} setDock={setDock} />
|
|
</div>
|
|
</div>
|
|
</ResizablePanel>
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** Shows a spinner in place of the log type icon while the open log's details load */
|
|
function OpenLogSummary({
|
|
log,
|
|
searchParameters,
|
|
}: {
|
|
log: ColumnSchema
|
|
searchParameters: QuerySearchParamsType
|
|
}) {
|
|
const { isLoading } = useLogEnrichment({ row: log, searchParameters, shouldFetch: false })
|
|
return <LogStepSummary log={log} isLoading={isLoading} />
|
|
}
|