Distributed Tracing for Beginners
In a single program, finding out why something is slow is relatively easy. You run a profiler or read a stack trace and see exactly which function took the time. In a system made of many services, a single user action may touch a web server, an authentication service, a product service, a database and a payment provider. When the page loads slowly, which of those parts is to blame?
Distributed tracing exists to answer that question. It records the journey of a request through all the components it touches, with timing for each step, so you can see the whole path in one view. This guide explains the core ideas in plain terms.
The Core Concepts
Trace
A trace represents the complete journey of one request or transaction through your system. It begins when the request enters, for example when a browser calls your API, and ends when the response is complete. Each trace has a unique trace ID.
Span
A trace is made of spans. A span represents one piece of work, such as handling an HTTP request, running a database query or calling another service. Every span records:
- A name describing the operation.
- A start time and a duration.
- The trace ID it belongs to and its own span ID.
- The ID of its parent span, if it has one.
- Optional attributes, such as the HTTP method, status code or database table.
- Events and error information.
The parent and child relationships form a tree. The first span is the root, and the calls it makes become children. When displayed on a timeline, this looks like a waterfall chart, where you can see which operations ran in sequence, which ran in parallel and which took the longest.
Context Propagation
The hardest part of tracing across services is passing the identity of the trace along. When service A calls service B, A must send the trace ID and the current span ID so that B can attach its own spans to the same trace. This is called context propagation.
It is usually done through headers on HTTP requests or metadata on messages. The W3C Trace Context standard defines a common header, named traceparent, so that different tools and languages can interoperate.
traceparent: 00-<trace-id>-<parent-span-id>-01
If one service in the chain forgets to forward the context, the trace breaks into separate pieces and you lose the full picture.
What Tracing Helps You Do
Tracing is especially good at problems that cross boundaries:
- Find latency bottlenecks. See which call in the chain consumes most of the time.
- Locate the source of errors. Identify which downstream service actually failed, rather than only seeing the symptom at the front.
- Understand dependencies. Discover which services call which, including surprising or forgotten connections.
- Spot wasteful patterns. For example, a loop that makes dozens of separate database queries where one would do.
- Compare behavior. Look at slow requests next to fast ones to find what is different.
How Instrumentation Works
To produce spans, your code must be instrumented. There are two broad approaches.
Automatic instrumentation uses agents or libraries that wrap common frameworks, HTTP clients and database drivers, creating spans without you writing code. It is the fastest way to get started and covers a lot of ground.
Manual instrumentation means adding spans yourself around important business operations, such as "calculate shipping" or "generate invoice". It gives more meaningful traces and lets you attach attributes that matter for your domain.
Most teams begin with automatic instrumentation and add manual spans where the default view is not detailed enough. OpenTelemetry is the widely used open standard for this work, offering libraries for many languages and a vendor-neutral data format.
Where the Data Goes
Spans are sent from your services to a collector or agent, which can batch, filter and forward them to a backend that stores traces and provides a search interface. Common open source and commercial backends exist, and because the format is standardized, you can switch later without re-instrumenting everything.
Keep the overhead in mind. Tracing adds a small amount of processing and network traffic per request. Batch exports and sensible settings keep this minor for most applications.
Sampling: You Cannot Keep Everything
At high traffic, storing every trace is expensive and rarely necessary. Sampling decides which traces to keep.
- Head-based sampling decides at the start of a request, often by keeping a fixed fraction. It is simple and cheap but may miss rare problems.
- Tail-based sampling decides after the trace completes, so it can keep all traces with errors or unusually long durations. It is more powerful but requires more infrastructure.
A reasonable beginner strategy is to sample a modest portion of normal traffic and make sure errors and slow requests are retained where possible.
Linking Traces With Logs and Metrics
Tracing is most helpful when connected to other signals. Add the trace ID to every log line so you can jump from a slow trace to the logs written during that request. Many metric systems can also link a spike to example traces. This correlation turns three separate tools into one investigation path.
Getting Started Step by Step
- Pick a request path that matters, such as checkout or login.
- Enable automatic instrumentation in the services on that path.
- Make sure context is propagated across every network hop, including queues and background jobs.
- Send data to a backend and view a few traces by hand.
- Add attributes such as user tier or region, avoiding sensitive personal data.
- Set a sampling rule and review its cost after a few weeks.
- Teach the team to open a trace first when someone reports a slow request.
Common pitfalls include broken propagation through message queues, inconsistent service names, putting secrets or personal data into span attributes and creating so many tiny spans that traces become unreadable.
Frequently Asked Questions
Is distributed tracing only useful for microservices?
It is most valuable there, but it can help any system where a request passes through several components, including a monolith that calls databases, caches and external APIs. Even a single service benefits from seeing time spent on each outbound call.
Does tracing slow my application down?
It adds some overhead, but with batching and sampling the impact is typically small. You can measure it by comparing latency with tracing on and off in a test environment. If overhead is a concern, reduce the number of spans or lower the sampling rate.
What is the difference between a trace ID and a request ID?
A request ID is a general identifier you may add to logs for one request, often generated by a single service. A trace ID is part of a tracing standard and is passed along to every service, tied to a structured set of spans. In practice, you can use the trace ID as your request ID for logs.