Files
kemal.earth ff9ff456cd feat(design-system): updated trace ui components (#54389)
## Problem

The `Trace` primitives in `ui-patterns` only supported panning and
zooming through the minimap, and had no way to show summary facts about
a trace. Using them for a real trace view in Unified Logs needs direct
manipulation on the waterfall itself, a resizable span column for long
span names, and a place to put trace-level stats.

Part 1 of 3. This PR is design-system only — nothing in Studio consumes
it yet.

## Solution

**Direct manipulation on the waterfall**

- Drag across the timeline to pan, wheel to zoom around the pointer.
Panning only starts past a small threshold, so a drag never selects the
row it ends on.
- `Trace.Scrubber` follows the pointer with the offset from the trace
start.
- The span column is resizable by a focusable separator: drag it, nudge
with the arrow keys, or jump to its bounds with Home and End. The
timeline is kept above a minimum width so the tree can't be dragged over
it.

**Minimap**

- Dragging outside the brush now selects a new window. Dragging the
brush still pans and dragging an edge still resizes; a plain click
outside still recentres.

**New parts**

- `Trace.Summary` / `Trace.SummaryItem`, with `Trace.TotalDuration`,
`Trace.SpanCount` and `Trace.Depth` as values.

**Visual**

- Leaf rows get an elbow connecting them to their parent's guide, so the
tree reads as a tree rather than floating dots.
- Minor ruler ticks between the labelled ones, and a shorter ruler,
which also shortens the span header.

**One breaking change:** `Trace.Minimap` is now passed to
`Trace.Waterfall`'s `minimap` prop instead of being a child, so it sits
outside the area that handles pointer gestures. Both demos and the docs
page are updated to match; these are the only consumers in the repo.

## Review instructions

1. Open the design-system preview and go to **UI Patterns → Tracing**.
2. On the first demo, drag left and right across the timeline to pan,
and scroll to zoom — the zoom should stay anchored under the pointer.
3. Drag a span bar and release on it. It should pan, not select the row.
4. Drag the divider between the span column and the timeline. Then tab
to it and use the arrow keys, Home and End — the handle should move from
where it appears, and End should reach the far edge.
5. Move the pointer across the timeline and confirm the scrubber line
tracks it with a readable offset.
6. Check the summary strip above the waterfall shows Duration, Depth and
Total Spans.
7. On the minimap: drag the brush to pan, drag an edge to resize, drag
outside the brush to select a new window, and click outside it to
recentre. Zoom all the way in and confirm the brush stays wide enough to
grab.
8. Scroll the docs page and confirm the code sample matches what the
demos actually do.
2026-12-02 14:24:52 +00:00

101 lines
6.5 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.Summary>
<Trace.SummaryItem label="Duration">
<Trace.TotalDuration />
</Trace.SummaryItem>
<Trace.SummaryItem label="Depth">
<Trace.Depth />
</Trace.SummaryItem>
<Trace.SummaryItem label="Total Spans">
<Trace.SpanCount />
</Trace.SummaryItem>
</Trace.Summary>
<Trace.Waterfall minimap={<Trace.Minimap />}>
<Trace.Ruler>
<Trace.CollapseAll />
<Trace.ResetZoom />
</Trace.Ruler>
<Trace.Grid />
<Trace.Cursor />
<Trace.Scrubber />
<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.
## Summary
`Trace.Summary` is a divided strip of facts about the whole trace, meant to sit between the header and the waterfall. Each fact is a `Trace.SummaryItem` with a `label`, and its children are the value. `Trace.TotalDuration`, `Trace.SpanCount` and `Trace.Depth` read the trace and render the total duration, the number of spans, and how many levels deep the tree goes. Anything else can be a value too, so you can mix in facts the primitives don't cover.
## 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 and the timeline. `treeWidth` sets the tree's starting width and `minTreeWidth` its lower bound. Drag the divider between the columns to resize the tree; it is a focusable separator, so the left and right arrow keys nudge it and Home and End jump to its bounds. The timeline column always keeps a minimum width, so the tree can never be dragged over it.
Pass the minimap to the `minimap` prop rather than as a child, so it sits above the ruler and outside the area that handles pointer gestures.
`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. `Trace.Scrubber` follows the pointer across the timeline with a line and a label reading the offset from the trace start. Place `Trace.Grid`, `Trace.Cursor` and `Trace.Scrubber` 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()`.
## Panning and zooming
Drag anywhere across the timeline to pan the window, and use the wheel over it to zoom around the pointer. Panning starts only once the pointer has moved a few pixels, so a drag that ends on a row never selects it.
`Trace.Minimap` compresses the whole trace into a full-width strip above the ruler, with one row per depth level, colored by span status. Pass it to the waterfall's `minimap` prop. The brush shows the visible window. Drag the brush to pan and drag either edge to resize it. Outside the brush, drag to select a new window, or click to recenter the current one. 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 />