Files
kemal.earth 9ef93f1f19 feat(design-system): trace header and composition (#52169)
## Problem

We're adding tracing UI primitives to the design system as building
blocks for future trace waterfalls. This is PR 7, the last in a stack of
small, component-by-component PRs. The earlier PRs built the waterfall
itself. This one adds the header controls, a live demo and a docs page
that reads as one story. It also gives the zoom a way back, since
`resetWindow()` from PR 5 had no button until now.

Part of
[DEBUG-243](https://linear.app/supabase/issue/DEBUG-243/tracing-ui-primitives-stacked-prs)

## Solution

Adds the header parts, a live demo and a docs restructure.

**Header parts** (`Trace.Header`, `Trace.Title`, `Trace.Status`,
`Trace.CollapseAll`, `Trace.ResetZoom`)
- `Trace.Header` is a layout container for the controls above the
waterfall, and takes a `className`. The other parts take no props.
- `Trace.Title` shows the name of the first root span, falling back to
"Trace". There's no new field on the trace data.
- `Trace.Status` is a badge showing Live, Done or Failed. Failed means
the root span has an error status. It also renders a visually hidden
`role="status"` element with `aria-live="polite"` that announces the
same text, and has no footprint where it isn't rendered.
- `Trace.CollapseAll` toggles every span that has children, and its
label flips between "Collapse all" and "Expand all". It's disabled when
no span has children. It's cheap on large traces because it only walks
spans that have children. This uses `toggleAllCollapsed` and
`areAllCollapsed` in `utils.ts` and one bulk `toggleCollapseAll()` on
the view context.
- `Trace.ResetZoom` calls `resetWindow()`. It's disabled unless the view
is zoomed, and after a reset the window follows a growing live trace
again.

**Live demo**
- A new docs example with a ticking live trace. Spans appear and finish
as the clock advances, and it includes an error span and event markers.
- It starts paused at 0ms, with a Play/Pause button in the header. The
clock is an accumulated base value plus the start of the current play
session, so pausing freezes the elapsed time exactly and resuming
continues without a jump. It uses `minRangeMs` so the axis isn't
degenerate at the start. When it reaches the end, the badge flips from
Live to Done, and Play after that restarts from 0.
- Both demos now use Supabase request paths (such as `GET
/rest/v1/orders` and `POST /functions/v1/process-order`) as sample data.

**Docs**
- The main example is now a single composed waterfall: header, minimap,
ruler, rows, bars and inspector. The page runs from the intro to a
Composition section with that example and a snippet, then short sections
for Header, Waterfall and ruler, Rows and bars, Minimap and zoom,
Inspector, and Live traces. The static live example from PR 2 was
dropped from the composed example.

## Additional context

This is PR 7 of a stack, so it builds on
`kemal/debug-243-6-trace-inspector-popover`. The earlier exploration is
in the draft #51184, which can be closed once the stack lands.

Known and left as is:

- While a live trace grows unzoomed, finished bars and markers jump
while the ruler ticks slide. A shared `transition-[left,right,width]`
across the bar primitives would fix it, and it's a follow-up.
- The Failed state is only exercised in a test fixture, not in a docs
example. A root error in the live demo would mean Done never shows.

Out of scope: `Trace.Filter` and search, controlled `selectedId` props,
mouse-wheel or pinch zoom, zoom-to-span, row enter and exit motion,
kind-based tinting, responsive rows, and a per-span icon slot.

## Review instructions

1. Open the preview link and find the Tracing page under UI Patterns.
Check the page reads in order from intro to composition to the
individual sections.
2. In the composed example, check the title shows the root span's name
and the status badge shows Done.
3. Zoom in with the minimap. Check "Reset zoom" is disabled before you
zoom, enables after, and restores the full range when clicked.
4. Click "Collapse all". The tree should collapse to the root rows and
the label should change to "Expand all". Click again and everything
should expand.
5. Check the inspector, minimap and arrow-key navigation still work in
the composed example.
6. In the live demo:
   1. It starts paused at 0ms with a Live badge.
   2. Click Play and check time advances and bars grow.
3. Click Pause and check the elapsed time stays frozen. Wait a few
seconds, then resume and check it continues without a jump.
4. Zoom in and check the window stays fixed while the trace grows. Click
"Reset zoom" and check it follows growth again.
5. Let it run to completion and check the badge flips to Done. Play
after that should restart from 0.
7. With a screen reader or by inspecting the DOM, check the visually
hidden `role="status"` text matches the badge.
8. Switch between light and dark mode and check the header, controls and
demo are legible in both.
9. Read `Header.tsx`, the collapse helpers in `utils.ts` and the live
demo's clock logic. Run the Trace unit tests in `packages/ui-patterns`,
which now cover the collapse-all helpers, and run typecheck and
Prettier.
2026-11-12 17:16:20 +00:00

80 lines
4.9 KiB
Plaintext

---
title: Tracing
description: Composable primitives for distributed-trace views built on the shared design system
---
```tsx
import { Trace } from 'ui-patterns/Trace'
```
`Trace.Root` indexes a list of OpenTelemetry-style spans and shares the result with every part inside it. The other parts are small primitives you compose into the view you need.
## Composition
<ComponentPreview name="trace-waterfall-demo" peekCode wide squared />
```tsx
<Trace.Root trace={trace}>
<Trace.Header>
<Trace.Title />
<Trace.Status />
</Trace.Header>
<Trace.Waterfall>
<Trace.Minimap />
<Trace.Ruler>
<Trace.CollapseAll />
<Trace.ResetZoom />
</Trace.Ruler>
<Trace.Grid />
<Trace.Cursor />
<Trace.Rows>
<Trace.Cell />
<Trace.Lane>
<Trace.Bar />
<Trace.Timing />
<Trace.CollapsedChildren />
<Trace.Markers />
</Trace.Lane>
</Trace.Rows>
</Trace.Waterfall>
<Trace.Inspector />
</Trace.Root>
```
## Header
`Trace.Header` is a layout container for the controls above the waterfall. `Trace.Title` shows the name of the trace's first root span. `Trace.Status` shows Live while any span is still running, Failed when the root span has an error status, and Done otherwise. It also announces changes to screen readers.
`Trace.CollapseAll` and `Trace.ResetZoom` are icon buttons with tooltips that go at the right end of the ruler's tree cell, as children of `Trace.Ruler`. `Trace.CollapseAll` collapses every span that has children, and expands them all again when they are already collapsed. `Trace.ResetZoom` restores the full window and is disabled until you zoom.
## Waterfall and ruler
`Trace.Waterfall` is the layout container. It measures its width so time can be mapped to pixels, and has two columns: the tree (`treeWidth`) and the timeline. `Trace.Ruler` draws the time axis, adapts its ticks to the visible range, and renders its children at the right end of its tree cell, next to the "Span" label. `Trace.Grid` draws a vertical gridline at every tick below it. While a trace is live, `Trace.Cursor` draws a line at `nowMs` from the top of the ruler to the bottom of the waterfall. Pass `nowMs` on the trace to pin it, and `minRangeMs` to keep a minimum visible duration while live. Place `Trace.Grid` and `Trace.Cursor` directly after the ruler.
## Rows and bars
`Trace.Rows` is a virtualized, scrollable list with one row per visible span. Its children are the row template and render once per row. `Trace.Cell` is the tree column: indent by depth, an expand and collapse toggle, the span name, and a spinner while the span is running. `Trace.Lane` is the timeline column, a positioned container for the primitives below. Compose the primitives in this order so markers draw over the bar:
- `Trace.Bar` draws the span from its start to its end, tinted by status. A running span extends to `nowMs`.
- `Trace.Timing` shows the span's duration just after the bar, or inside the bar's end when there is no room after it.
- `Trace.CollapsedChildren` draws a tick at each direct child's midpoint when a span is collapsed.
- `Trace.Markers` places a marker at each of the span's event timestamps.
Rows follow the tree pattern. Use the arrow keys to move between rows, Right and Left to expand and collapse (Left on a collapsed row moves to its parent), and Enter or Space to select. Read the selection and collapse state with `useView()`.
## Minimap and zoom
`Trace.Minimap` compresses the whole trace into a full-width strip above the ruler, with one row per depth level, colored by span status. The brush shows the visible window. Drag the brush to pan, drag either edge to resize it, or click outside the brush to recenter it. With the brush focused, the left and right arrow keys pan, and Shift with an arrow key moves the end edge.
Until the window is changed, it follows the trace as it grows. Once it is zoomed it stays fixed, and ruler labels still read as offsets from the trace start. Use `Trace.ResetZoom` to follow the trace again, or read and control the window yourself with `useView()`: `window`, `setWindow(start, end)` and `resetWindow()`.
## Inspector
Place `<Trace.Inspector />` once inside `Trace.Root`. When a span is selected, it opens a popover next to the span's bar with its name, status, start offset, duration, attributes and events. Arrow keys keep moving between rows while it is open, and clicking another row moves it to that span. Press Escape or click outside the waterfall to close it and clear the selection. If the selected row scrolls out of view, the popover hides and comes back when the row does.
## Live traces
Spans without an end are running. Update the trace and `nowMs` as time passes and the running bars grow, the ruler follows, and `Trace.Status` changes from Live to Done when the last span finishes. Press Play to run this example.
<ComponentPreview name="trace-waterfall-live-demo" peekCode wide squared />