使用 OpenTelemetry 进行分布式追踪
使用 OpenTelemetry 追踪请求在进程和服务之间的流转,将分散的日志整合为一条连贯的时间线。
使用 OpenTelemetry 进行分布式追踪 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
用 AI 导师学习 Elixir — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「使用 OpenTelemetry 进行分布式追踪」课时是免费的吗?
是的 — 「使用 OpenTelemetry 进行分布式追踪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。
「使用 OpenTelemetry 进行分布式追踪」这节课中我会学到什么?
使用 OpenTelemetry 追踪请求在进程和服务之间的流转,将分散的日志整合为一条连贯的时间线。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 OpenTelemetry 进行分布式追踪」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?
能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Elixir 基准测试与性能分析
- 使用 Telemetry 与指标进行监控
- 错误处理与结构化日志记录
- 使用 OpenTelemetry 进行分布式追踪