0PricingLogin
FastAPI Backend Development Bootcamp · Lesson

Distributed Tracing with OpenTelemetry

Auto-instrument FastAPI and propagate trace context through downstream HTTP and database calls.

Why Distributed Tracing?

In a microservice or even a single-service backend that talks to other HTTP APIs and a database, a single user request fans out into many operations. When something is slow or fails, logs alone can't show you the causal chain across process boundaries.

Distributed tracing solves this by giving every request a shared trace_id and breaking the work into nested spans:

  • A trace = the whole journey of one request.
  • A span = one timed unit of work (an HTTP handler, a DB query, an outbound call).
  • Spans carry a parent_span_id, forming a tree.

OpenTelemetry (OTel) is the vendor-neutral standard and SDK we use to produce these traces from FastAPI and ship them to a backend like Jaeger, Tempo or an OTLP collector.

The OpenTelemetry Data Model

Before wiring anything up, understand the core objects you'll configure in code:

  • TracerProvider — the factory that creates tracers; you configure it once at startup.
  • Tracer — obtained from the provider, used to start spans.
  • Span — has a name, start/end time, attributes (key/value tags), events, and a status.
  • SpanProcessor — batches finished spans (use BatchSpanProcessor in production).
  • Exporter — serializes spans and sends them out (OTLP over gRPC/HTTP).
  • Context — the thread-/task-local carrier that holds the currently active span.

The flow is: TracerProvider → Tracer → Span → SpanProcessor → Exporter → backend.

All lessons in this course

  1. Structured JSON Logging and Correlation IDs
  2. Distributed Tracing with OpenTelemetry
  3. Prometheus Metrics and RED/USE Dashboards
  4. Alerting on SLOs and Error Budgets
← Back to FastAPI Backend Development Bootcamp