Distributed Tracing with OpenTelemetry
Trace requests as they flow across processes and services using OpenTelemetry, turning scattered logs into a single connected timeline.
Distributed Tracing with OpenTelemetry is a free Elixir & Phoenix: Scalable Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elixir & Phoenix: Scalable Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Distributed Debugging Problem
In a system spread across services and processes, one user request touches many components. Plain logs cannot show how they connect. Distributed tracing stitches the whole journey into one timeline.
Traces and Spans
A trace represents one end-to-end request. It is made of spans — timed units of work like a DB query or an HTTP call. Spans nest to form a tree.
What is OpenTelemetry?
OpenTelemetry (OTel) is a vendor-neutral standard for traces, metrics, and logs. Elixir has first-class support via the opentelemetry packages, exporting to Jaeger, Honeycomb, Datadog, and more.
Adding the Dependencies
Add the API, SDK, and an exporter to your deps.
defp deps do
[
{:opentelemetry, "~> 1.3"},
{:opentelemetry_exporter, "~> 1.6"},
{:opentelemetry_phoenix, "~> 1.1"}
]
endAuto-Instrumenting Phoenix
The opentelemetry_phoenix package hooks into Telemetry events so each request becomes a trace automatically. Set it up in your application start.
def start(_type, _args) do
OpentelemetryPhoenix.setup()
OpentelemetryEcto.setup([:my_app, :repo])
Supervisor.start_link(children, opts)
endCreating a Manual Span
For custom work, wrap it in a span with Tracer.with_span/2. It records start and end times automatically.
require OpenTelemetry.Tracer, as: Tracer
Tracer.with_span "charge_payment" do
PaymentGateway.charge(order)
endAdding Span Attributes
Attributes attach searchable context to a span, like the user id or order total.
Tracer.set_attributes([{"order.id", order.id}, {"order.total", order.total}])Recording Errors
When something fails, mark the span so it stands out in your tracing UI.
Tracer.set_status(:error, "payment declined")
Tracer.record_exception(exception)Context Propagation
The magic of tracing is context propagation: the trace id travels with the request across service boundaries via HTTP headers, so spans from different services join the same trace.
Exporting to a Backend
Configure an exporter to ship spans to your observability backend over OTLP.
config :opentelemetry,
span_processor: :batch,
traces_exporter: :otlp
config :opentelemetry_exporter,
otlp_endpoint: "http://collector:4318"Reading a Trace
In a tracing UI you see a waterfall of spans. Long bars reveal slow steps; gaps reveal waiting. This pinpoints latency far faster than scanning logs.
Quick Check
Test your tracing knowledge.
Recap
You learned distributed tracing with OpenTelemetry:
- Traces are built from nested spans
- Auto-instrument Phoenix and Ecto via Telemetry
- Create manual spans with
with_spanand add attributes - Record errors on spans for visibility
- Context propagation connects spans across services
Tracing turns multi-service requests into one readable timeline.
Frequently asked questions
Is the “Distributed Tracing with OpenTelemetry” lesson free?
Yes — the full text of “Distributed Tracing with OpenTelemetry” is free to read here on the web, and the Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elixir & Phoenix: Scalable Backend Development course, upgrade to CoddyKit PRO.
What will I learn in “Distributed Tracing with OpenTelemetry”?
Trace requests as they flow across processes and services using OpenTelemetry, turning scattered logs into a single connected timeline. You practise Elixir & Phoenix: Scalable Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Elixir & Phoenix: Scalable Backend Development?
No prior experience is required. Elixir & Phoenix: Scalable Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Distributed Tracing with OpenTelemetry” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Elixir & Phoenix: Scalable Backend Development lesson?
Yes. Every Elixir & Phoenix: Scalable Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Benchmarking and Profiling Elixir
- Monitoring with Telemetry and Metrics
- Error Handling and Structured Logging
- Distributed Tracing with OpenTelemetry