# Distributed Tracing

Distributed tracing is a method used to profile and monitor applications, especially those built using a microservices architecture. It helps pinpoint where failures occur and what causes poor performance.

## 1. The Problem: Where did the request go?

In a monolithic application, a request is processed within a single process. If it's slow, you look at the stack trace.

In a microservices architecture, a single user request might trigger a chain reaction of calls across 10 different services. If the request is slow, which service is the bottleneck? Is it the network? The database?

## 2. Key Concepts

### Trace
A **Trace** represents the entire journey of a request as it moves through a distributed system. It is a collection of Spans.

### Span
A **Span** represents a single unit of work. It has a start time, an end time, and metadata (tags/logs).
*   **Root Span:** The first span in a trace (usually the entry point).
*   **Child Span:** Spans triggered by a parent span.

### Context Propagation
To connect spans across different services, a **Trace ID** must be passed along with every request (usually in HTTP headers). This allows the tracing system to stitch the separate spans back together into a single trace.

## 3. How it Works

1.  **Instrumentation:** Code is instrumented (manually or automatically) to generate spans.
2.  **Collection:** Spans are sent to a collector.
3.  **Storage:** Spans are stored in a database (e.g., Elasticsearch, Cassandra).
4.  **Visualization:** A UI (like Jaeger) queries the data to visualize the trace as a Gantt chart.

## 4. OpenTelemetry

OpenTelemetry (OTel) is the industry standard for generating and collecting telemetry data (traces, metrics, logs). It provides a vendor-neutral way to instrument your application.

## 5. Common Tools

*   **Jaeger:** Open source, end-to-end distributed tracing.
*   **Zipkin:** Another popular open-source tracing system.
*   **Datadog / Honeycomb / New Relic:** Commercial observability platforms with strong tracing capabilities.

[[programming/observability-vs-monitoring]]
[[programming/microservices-architecture]]
[[programming/debugging-techniques]]