OpenTelemetryによる分散トレーシング
OpenTelemetryを使って、プロセスやサービスをまたいで流れるリクエストを追跡し、分散したログを1つのつながったタイムラインにまとめます。
「OpenTelemetryによる分散トレーシング」はCoddyKit上の無料Elixir & Phoenix: Scalable Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「OpenTelemetryによる分散トレーシング」レッスンは無料ですか?
はい。「OpenTelemetryによる分散トレーシング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Elixir & Phoenix: Scalable Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Elixir & Phoenix: Scalable Backend Developmentコースには全4レッスンが含まれています。
「OpenTelemetryによる分散トレーシング」で何を学びますか?
OpenTelemetryを使って、プロセスやサービスをまたいで流れるリクエストを追跡し、分散したログを1つのつながったタイムラインにまとめます。 ブラウザで直接実行するハンズオンコードでElixir & Phoenix: Scalable Backend Developmentを演習し、24時間対応の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による分散トレーシング