# Distributed tracing

## What it is

Distributed tracing follows what happens to an operation as it crosses process
or service boundaries. A trace groups related spans. A span records one piece of
work, such as accepting an HTTP request, querying a database or calling an
inventory service. It generally has a start and end time and can hold selected
attributes and status. A parent span explains one causal relationship. Span
links can represent relationships that do not fit a simple parent and child
tree, such as a batch assembled from several requests.

Trace context lets instruments associate work in different processes. Over HTTP,
the W3C Trace Context specification describes headers including traceparent,
which carries identifiers and flags. The receiving component can use this
information to start its own span in the right trace. A queue can also carry
context in message metadata when its instrumentation supports the transport.
Trace context is an operational correlation mechanism, and the trust placed in
incoming data still depends on the service boundary.

In a checkout, a web service might request a quote and reserve inventory before
responding. The trace can show that the calls ran in parallel and that a wait
for inventory dominated the response. A later worker can link its own span to
the operation that scheduled it. The resulting view helps someone ask which work
delayed this transaction. Aggregate metrics help answer how often the problem
happens and how much of the workload it affects.

An investigation can also use the absence of an expected span as a clue. First
establish how that span would have been recorded and whether the trace passed
through sampling and export. A healthy test transaction is useful as a positive
control. Trigger the same application route and verify that its expected spans
reach the backend. If they do, compare the affected route's flags, dependencies
and time range with the control before drawing conclusions about the missing
work.

## Why it matters

Timing each component separately can obscure how they combine from the caller's
point of view. A service that appears fast on average can hold up a particular
request during a retry or long queue wait. A trace gives evidence about the
particular route, its dependencies and the relationship between two delays.
Compare it with latency distributions and error metrics so an isolated slow
sample is interpreted in the context of the rest of the workload.

Context propagation is especially useful when a job moves between teams. The
request handler can record that a job was queued. The worker then handles the
job under another process and may fail after the original request succeeded. If
the context crosses the queue boundary, support staff can relate the activity
without guessing from timestamps. Define which identifiers have an operational
purpose and how someone finds them without exposing payloads or personal data.

Instrumentation adds processing, storage and operational costs. A sampling
strategy chooses how much data to collect or retain. A decision near the start
can reduce work early. A collector that decides near the end can retain traces
based on observed error or latency, with different buffering and capacity
requirements. Set the sampling policy according to the questions the team
actually needs to answer. Also measure the loss that can happen before the
decision, including unavailable collectors and partial instrumentation.

## What it looks like in practice

Choose one transaction with a clear outcome and instrument it from the service
entry to its important dependencies. In a test, trigger normal traffic, a slow
provider response and a broker redelivery. For each trace, inspect the span
names, timestamps and relationship to the calling operation. Confirm that a
developer can tell whether time was spent waiting for capacity or executing the
work. Try a service interruption and examine what still arrives at the
collector.

Review which attributes the instrumentation sends. A trace can contain the route
template, an outcome category and the component name. It rarely needs the
original email address, a request body or an authentication header. Test trace
lookup after removing such data and check the policy at the collector as well as
the client. If operators need to find a specific transaction, choose an opaque
identifier with a justified retention period and document who can use it.

Our
[case study on the company's first observability dashboard](/portfolio/built-the-companys-first-observability-dashboard/)
describes collecting infrastructure and container measurements and later using
Grafana and Prometheus. Such measurements give context when a trace shows delay
during a deployment or resource spike. The checkout scenario illustrates how
tracing can add information about the path of one request. The dashboard case
supplies the aggregate monitoring example.

## Where it goes wrong

Missing propagation can split one request into unrelated trace records. Verify
both ends of a process boundary and follow the actual transport. Check that a
proxy forwards the relevant fields and that instrumentation does not create a
new root span by accident. Include asynchronous work in the test. A visible
server span confirms that one component was instrumented, while the relationship
to its producer needs its own check.

A detailed trace can still represent an incomplete or biased sample. Services
can drop spans at different points, clocks can disagree and long-running work
can finish after a collector gives up waiting. Document how instrumentation and
sampling affect what appears. When a backend reports a missing time window, look
for span loss and clock problems before concluding that the operation had no
corresponding work.

Attributes with a unique value for almost every request can become costly to
index. Use stable span names and dimensions suited to the questions each backend
answers. Restrict who can search and export trace data, especially when it
crosses organisation boundaries. Test the storage and ingest cost during bursts.
The telemetry path should tolerate delay or loss according to a documented
policy while the application continues to serve its primary purpose.

## Further reading

- [OpenTelemetry traces](https://opentelemetry.io/docs/concepts/signals/traces/)
  explains spans, relationships and trace context.
- [W3C Trace Context](https://www.w3.org/TR/trace-context/) specifies HTTP
  headers for interoperable trace propagation.
- [OpenTelemetry sampling](https://opentelemetry.io/docs/concepts/sampling/)
  describes head and tail sampling and the evidence each can use.

<https://engineer.company/glossary/distributed-tracing/>
