# Trace context

## What it is

Trace context carries the information needed to relate parts of a distributed
trace as work moves between components. An application can start a trace for a
request and record a span for one operation. When that operation calls another
service, it can pass context that lets the receiver identify the trace and
relate its work to the operation that made the call.

The W3C Trace Context standard defines an HTTP representation. Its traceparent
field carries a version, trace identifier, parent identifier and flags. The
related tracestate field can carry additional vendor-specific context within the
format's rules. An application can extract incoming context and inject
appropriate outgoing context. The receiver uses that information when it decides
how to record or continue its own observation.

The context travels during work. Exported spans can reach the collector later
and in a different order. Keeping the context consistently available allows the
trace store to connect spans after they arrive. A trace identifier relates work
within a trace. An order identifier, message identifier or user identifier
serves a separate purpose. Those values have their own lifecycle and privacy
requirements.

## Why it matters

A user can experience a slow operation across a frontend, API and storage
component even if each team's log makes sense on its own. Correlation by time is
difficult when many requests overlap. Propagating context lets a maintainer
follow the relevant operations and measure where the delay was introduced. It
also helps show which work the application attempted before an error or a retry.

Context is useful at more than one kind of boundary. A browser request can call
an API. That service can enqueue a task which another process handles later. The
appropriate relationship depends on how the system describes the work. A worker
may continue one observation or record a separate trace with a link to the
initiating span. Choose a relationship that makes sense for the queue's timing
and for the problem an operator needs to diagnose.

The design also needs a trust policy. Trace headers can arrive from systems the
application does not control. Validate what is used and decide where to start a
new internal trace. Avoid placing customer identifiers or confidential data into
propagation fields. Use an agreed policy for outgoing connections so trace
context is shared only where it has a clear operational purpose.

## What it looks like in practice

Imagine a request that starts an export. The entry point accepts or starts a
trace, records the request span and injects context into its call to the export
service. That service can record its own operation with a parent relationship.
When it reserves work for a queue, it can preserve selected correlation
information so the worker can link back later. The product's durable job
identifier remains the way a user queries the export's status.

Test the real request path. Send a controlled request through the frontend to
the API and watch for its span and the expected parent-child relationships.
Introduce an error in the downstream service and check which span records the
failure. Send a request with no incoming context and verify that the system
follows its policy for starting a trace. Then observe a queued retry and check
that the traces still describe what happened.

Engineer's
[first-party tracing case study](/portfolio/built-first-party-error-monitoring-and-tracing/)
documents using OpenTelemetry to follow requests across the frontend, API and
database. The export exercise applies the same idea to a request with deferred
work. A useful trace makes the join between components visible and shows the
path that actually ran, including the work performed after a request returns to
the user.

Also inspect where automatic instrumentation ends. A library might propagate
context across ordinary client calls while a custom message publisher needs
explicit handling. Record which boundary supplied the context at that step. Use
a controlled fixture that records both the outgoing and incoming identifiers.
Verify the parent relationships directly and check that the viewer groups each
operation with the correct trace.

## Where it goes wrong

A trace can be fragmented when one component drops context, creates a new root
accidentally or reuses context from an earlier request. Inspect the actual
propagation path and check that request scopes are closed at the end of their
work. Test concurrent requests so a missing scope boundary does not silently mix
their spans.

Another problem is expecting a trace from every propagated context. Sampling and
exporter decisions influence what reaches the store, and some dependencies
decline incoming context. Use a trace as evidence about the spans actually
retained. A broken link between two important services may require an
independent measurement to explain which instrumentation or sampling step was
involved.

A test can also check a round trip through an internal proxy. The proxy may
forward headers faithfully while an application overwrites the context for a
later outgoing request. Inspect what each component extracted and injected. Keep
the fixture small enough to follow the precise expected sequence. Compare trace
identity, parent identity and the operation names together. A shared trace
identifier alone can group the work without describing the causal relationship
that helps a maintainer identify delay.

Finally, keep the propagated information small and documented. Update the trust
policy when work crosses a new boundary and test the behaviour with invalid or
absent fields. Give operators enough context to diagnose real requests without
carrying unnecessary detail between systems. A well-understood trace context
makes relationships visible while leaving the product's own identifiers and user
outcomes clear.

## Further reading

- [W3C Trace Context](https://www.w3.org/TR/trace-context/). Specifies
  traceparent and tracestate for HTTP propagation.
- [OpenTelemetry context propagation](https://opentelemetry.io/docs/concepts/context-propagation/).
  Explains correlation across components and process boundaries.
- [OpenTelemetry propagator APIs](https://opentelemetry.io/docs/specs/otel/context/api-propagators/).
  Defines how instrumentation injects and extracts context.

<https://engineer.company/glossary/trace-context/>
