Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
name: instrumentation description: >- Add OpenTelemetry tracing to a new flow or hot path, frontend or backend. Use when building a user-facing flow, a cross-service call, a queue or background job, or code on a latency-sensitive path; when adding spans, span attributes, or trace propagation; or when a trace in Tempo/Datadog is missing, disconnected, or opaque.
Instrumentation
New flows and hot paths ship instrumented, in the same change. The standard is the observability rule: one connected trace from the user's action through every service, with phases timed and the entity id on the spans. This skill is how to meet it.
Pick the shape first
Before writing code, sketch the trace you want to read during an incident:
navigation (browser interaction)
└─ view.<route> (caller-owned loading flow)
├─ document.load / block.data
│ └─ doc.load → doc.load.context (HTTP → DSS)
├─ doc.snapshot.*
└─ doc.editor
└─ doc.editor.initialize (initial sync → editor ready)
One root span per user action, one child per phase a reader would ask about (network, queue wait, processing, each external call), and the id you'd search by on the root.
Frontend and Cloudflare Workers
Use the @macro-inc/observability facade (packages/observability); never
import OpenTelemetry directly.
Telemetry.span(name, fn)for a phase;span.span(name, fn)for a child.Telemetry.span(name, { kind: 'client' })for an outbound call, thenspan.injectTraceHeaders(headers).Telemetry.span(name, { sampling: 'perf' })puts a trace in a sampling class; without one it is always kept. See What gets exported.span.setAttr(key, value)for ids and counts;span.error(message)on failure; manual spans mustend()on every path.- The browser uses a synchronous
StackContextManager, so after anawaitcallparent.span(...), or wrap later dispatch inparent.run(...); see the rule forTelemetry.captureContext(). - Share a caller-owned span through flow context and explicit client
parameters (
parentSpan). Never keep entity-keyed span registries or end another owner's span. - Transports without headers carry
span.traceparent()another way, such as a WebSocket query parameter. - Workers wrap their handler with
instrument()andcreateWorkerTraceConfigfrom@macro-inc/observability/worker, which adopts the incomingtraceparent.
Frontend view loading
Use @app/observability/flow for user-visible loading. A flow is a span that
stays open while anything under it is loading and ends one paint after the last
hold releases.
- Views are roots already. Router commits start a
navigationinteraction and every routed view is aview.<route id><TraceFlow>keyed on its params (RouteViewinsplit-router/solid/outlet.tsx). Don't add one per route; add phases inside it. - Phases:
usePending(name, accessor), right after creating the query, e.g.usePending('channel.messages', () => query.isLoading). While the accessor is true it holds the flow as a child span; it can start again each time the flow is open. Already false when the flow reaches it (a cache hit or an earlier view's load) records a<name>.readyevent and<name>.waited=falseinstead of a span; a real wait sets<name>.waited=trueon the parent. Read status only: reading pendingquery.datasuspends the enclosing shell. Track initial loading, not background refreshes or a disabled query that cannot complete. - Nested flows:
<TraceFlow name="doc.editor">around an independently loaded subtree, placed inside its Suspense boundary (outside it the flow can settle while the fallback shows). A view can hold several. Passkeyto restart it when the subtree shows a different entity in place; pending phases move to the new run. Unmounting before it settles ends it, and its open phases, asflow.outcome=cancelled. - Async phases:
useFlow()during component setup, thenflow.task('block.data', async (span) => ...)for imperative loading. It holds the flow until the promise settles and runs the synchronous start in the child's context. A span alone does not hold the flow; give that work ausePendingortask. - Requests in the flow: urql and TanStack queries created inside a flow
already send in its trace. Start any other request inside
flow.run(() => ...), or handuseFlowTrace()to a transport that sends later. - No Zone across
await. The browser'sStackContextManagerkeeps context only synchronously. After an await callspan.span(...), or wrap later work inspan.run(...); captureTelemetry.captureContext()at synchronous admission (before auth or queueing) and.run(...)the send in it. - User actions:
beginInteraction('channel.tab')for an in-view action that mounts content without navigation: update state, then.end()in afinally. The window is synchronous; flows created inside it join it. UsestartUiOperationdirectly only for a measured action that mounts no flow (a cell commit, a submit), callingafterPaint()once the UI updated. - Sampling is per trace; see What gets exported.
flow.kind,route.id,flow.root,flow.outcome,<phase>.waited,macro.sampling.*and theui.*timing fields come from the owners; never set them per call site. Keep names static and attributes to opaque ids, categories and counts. No flow per message, row or cell; measure the containing view.
Flows never schedule application work; preloading and warm-up own their own gates.
Example: a view with two phases
The database block holds its view with two phases, the table's metadata and
its first rows (block-database/component/Block.tsx, DatabaseGrid.tsx):
function DatabaseDetail(props: { databaseId: string }) {
const detailQuery = useDatabaseDetailQuery(() => props.databaseId);
usePending('database.detail', () => detailQuery.isLoading);
return (
<Suspense fallback={<GridSkeleton />}>
<DatabaseGrid detail={detailQuery.data} />
</Suspense>
);
}
function DatabaseGrid(props: { detail: DatabaseDetail | undefined }) {
const source = useTableRows(() => props.detail);
usePending('database.rows', source.loading);
// ...
}
Opened from a list this exports navigation → view.<route id> → {database.detail, database.rows}, with the HTTP and server spans under each
phase. In Datadog, the root is service:web-app @macro.sampling.class:perf resource_name:navigation @route.id:<route id> @ui.outcome:success; read
@ui.elapsed_ms (input to paint) and split by @flow.kind to compare entry
points. On the view span, @database.rows.waited:false marks rows served from
the cache; phase durations are resource_name:database.rows @flow.outcome:success, with @flow.root naming the root. The
perf-investigation skill has the tested query shapes, and the
observability walkthrough
lists every root and attribute.
What gets exported
Four layers decide whether a span reaches Datadog:
The global gate. Built apps (browser and desktop) export only where the
enable-browser-otelPostHog flag is on; the Vite dev server exports whenever a collector is configured, andVITE_ENABLE_BROWSER_OTELoverrides both (observability/browser.ts). Spans are buffered from boot and exported once enablement resolves; nothing is sent before it.Unclassed spans: always exported once the gate is open. A
Telemetry.span(name)with nosamplingoption. Use this for requests, errors, mutations, background work and anything a backend trace hangs off:safeFetchHTTP spans,graphql.mutation_queue.attempt,channel.send.Sampling classes, declared by the app in
apps/web/src/observability/sampling.tswith their rates:perf(5% of production users, every user elsewhere): performance measurement. Flows,beginInteraction,startUiOperation,app.boot,input.typing_burst,app.runtime.sample,urql.reconcile. This is the default for<TraceFlow>,beginInteractionandstartUiOperation.always(every user): a span that must survive a droppedperftrace under a class of its own.
Each class is decided once per user per class at enablement, from the user id (the
frontend_instance_idbefore sign-in) salted with the week, so the cohort rotates weekly;Telemetry.config.sampled('perf')reads it, and explicit native recordings keep every class. Exported spans in a classed trace carrymacro.sampling.classandmacro.sampling.rate(weight counts by1 / rate).Nesting: a span joins its parent's trace when that trace is exported. Otherwise a phase (
span.span,usePending,flow.task) is dropped with its root, and any other span starts its own trace when its own class is kept (an unclassed span always is) and is dropped when it is not. So requests started inside an unsampled perf trace still export as their own traces, and their backend spans are never lost to perf sampling.Backend. Rust services and workers do not sample themselves: they follow the incoming
traceparent's sampled flag (OpenTelemetry's default parent-based sampler) and export the rest. Datadog then indexes a subset of ingested spans; the perf retention filter ininfra/stacks/monitoring/dashboards/web-performance.tskeeps exported perf traces searchable.
Picking a class. Timing a view, phase or user action to rank or compare
latency: use a flow or interaction (perf). Something that must be there to
debug one user's failure, or that a backend trace joins: leave it unclassed. A
new class belongs in sampling.ts with a rate, and only for a new kind of
telemetry with its own volume budget.
Rust services
- HTTP handlers already get spans from
macro_tower_layers; outbound HTTP callsmacro_tower_layers::inject_trace_headers. #[tracing::instrument(skip_all, fields(document_id = %id), err)]on domain operations and outbound adapters (CS-22:erronly onResult, nolevel = "info").- Kafka:
kafka_utilinjects trace headers on publish; consumers openkafka_util::consumer_spanand callrecord_span_erroron failure. - Agent/ACP messages:
genai_telemetry::propagation::inject/set_parentcarry context in_meta; GenAI spans follow CS-55. - Spawned tasks take the span with them (
.instrument(span)) or link to it. - Datadog files a client span that carries an HTTP method as operation
http.client.requestwith resource<METHOD> <url.template>, whatever the span is called; setoperation.nameon it to keep it findable by name (outlook.graph_requestdoes). Internal spans keep their name asresource_name. - Record numeric span fields as
i64:tracing-opentelemetryhas no unsigned type and exportsu16/u32/u64/usizevalues as strings. - Background loops start one root span per claimed unit of work, not per poll, so idle polling exports nothing.
Worked examples
Document open lifecycle (web → DSS → sync-service).
BlockLoader and FileDetailLoadGate own loading tasks and pass their span to
Markdown loaders. The loader times doc.load → doc.load.context and passes
context to its sync source. The editor and collaboration adapter use the
nearest flow context; collaboration reports ready without ending the caller's
span. No doc.open root or document-id span registry is created.
packages/collaboration/src/collab/telemetry.ts stamps
document.id and session.id on doc.seed, doc.sync.catchup, and
edit.apply. The sync WebSocket carries traceparent as a query parameter,
which sync-service reads with worker_rs_otel::traceparent_from_request.
Agent editing (web or Rust → ai-editing-worker → sync-service).
The web client (apps/web/src/lib/service-clients/ai-editing-worker/client.ts)
opens http POST /edit under the document's span; Rust callers
(crates/documents/src/outbound/editing_worker_client.rs) inject headers from
an instrumented method. The worker
(services/ai-editing-worker/src/endpoints/edit.ts) roots an edit.session
with outcome fields (edit.mode, edit.ops_total, edit.blocked,
edit.aborted), then a child per phase: edit.sync_init, edit.hydrate,
edit.interpret, edit.supervise.turn, edit.dispatch, edit.run_code,
edit.flush. On the agent side, crates/agent_session/src/domain/session/telemetry.rs
emits invoke_agent and execute_tool and propagates into the ACP agent.
GraphQL mutation queue (web → DSS graphql.execute → resolver).
apps/web/src/lib/graphql-cache/exchange/mutation-queue-telemetry.ts opens a
graphql.mutation_queue.attempt span per claimed attempt that actually sends
(mutation_queue.item_id, operation name, attempt count, queue depth, oldest
item age, auth/online/websocket state, mutation_queue.outcome,
mutation_queue.failure, graphql.error.code). Ages come from the durable
enqueue time (enqueuedAtMs), so they survive reloads; when it is missing or
skewed, mutation_queue.age_known is false and the age is omitted rather than
zero. The runner sends inside it, so
dssGraphqlFetch injects traceparent and the DSS graphql.execute span and
the instrumented resolvers (crates/graphql_entity_mutation,
graphql_properties, graphql_notification) join the same trace. A loop that
can repeat forever reports counters instead of lines: each item logs a failure
class once, and graphql.mutation_queue.summary flushes counters every five
minutes only when something non-routine happened.
Reading production
To find slow flows in Datadog from these spans, use the perf-investigation
skill.
Verify
Run the flow on the local stack and find its trace (see the live-debug
skill): one trace from browser to the last service, each phase visible with its
duration, findable by the entity id, and failures marked as errors. Report the
trace as verified or say it wasn't.