ストーリー
あなたの嘆きに、高橋アーキテクトが言った。
OpenTelemetryとは
OpenTelemetry(OTel)は、CNCF(Cloud Native Computing Foundation)が推進する、オブザーバビリティデータの収集・送信のためのオープンスタンダードです。
┌──────────────────────────────────────────┐
│ Application Code │
│ │
│ ┌──────────────────────────────────┐ │
│ │ OpenTelemetry SDK │ │
│ │ ┌────────┬─────────┬────────┐ │ │
│ │ │ Traces │ Metrics │ Logs │ │ │
│ │ └────────┴─────────┴────────┘ │ │
│ └──────────────┬───────────────┘ │
└─────────────────┼────────────────────┘
│ OTLP (OpenTelemetry Protocol)
▼
┌──────────────────┐
│ OTel Collector │
│ (収集・変換・送信) │
└───────┬──────────┘
│
┌───────────┼───────────┐
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│ Jaeger │ │Promethe│ │Elastic │
│ │ │ us │ │ search │
└────────┘ └────────┘ └────────┘
OTelの主要コンポーネント
1. API(インターフェース定義)
import { trace, metrics, context } from '@opentelemetry/api';
// トレーサーの取得
const tracer = trace.getTracer('my-service', '1.0.0');
// メーターの取得
const meter = metrics.getMeter('my-service', '1.0.0');
// Spanの作成
const span = tracer.startSpan('processOrder');
span.setAttribute('order.id', orderId);
span.end();
// メトリクスの記録
const requestCounter = meter.createCounter('http_requests_total');
requestCounter.add(1, { method: 'GET', status: '200' });
2. SDK(実装)
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({
url: 'http://otel-collector:4318/v1/traces',
}),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: 'http://otel-collector:4318/v1/metrics',
}),
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
3. Collector(収集・変換・送信)
# otel-collector-config.yaml
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
batch:
timeout: 5s
send_batch_size: 1000
exporters:
jaeger:
endpoint: jaeger:14250
prometheus:
endpoint: 0.0.0.0:8889
elasticsearch:
endpoints: ["http://elasticsearch:9200"]
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [jaeger]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheus]
logs:
receivers: [otlp]
processors: [batch]
exporters: [elasticsearch]
自動計装 vs 手動計装
| 方式 | 説明 | 用途 |
|---|---|---|
| 自動計装 | HTTP, DB, gRPC等のライブラリを自動的にトレース | 基本的なデータ収集 |
| 手動計装 | ビジネスロジックに合わせたカスタムSpan/メトリクス | 業務固有の情報 |
// 自動計装:HTTPリクエストは自動的にSpanが作られる
// 手動計装:ビジネスロジック固有の情報を追加
async function processOrder(orderId: string): Promise<OrderResult> {
return tracer.startActiveSpan('processOrder', async (span) => {
span.setAttribute('order.id', orderId);
const order = await orderRepository.findById(orderId);
span.setAttribute('order.total', order.total);
span.setAttribute('order.items_count', order.items.length);
try {
const result = await paymentService.charge(order);
span.setStatus({ code: SpanStatusCode.OK });
return result;
} catch (error) {
span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
span.recordException(error);
throw error;
} finally {
span.end();
}
});
}
OTelの利点
| 利点 | 説明 |
|---|---|
| ベンダー中立 | バックエンドを自由に切り替え可能 |
| 統一API | ログ・メトリクス・トレースを同じ方法で扱える |
| 自動計装 | 既存コードへの変更を最小限にできる |
| コミュニティ | CNCFの主要プロジェクトとして活発に開発 |
| エコシステム | 多数の言語・フレームワーク対応 |
まとめ
| ポイント | 内容 |
|---|---|
| OTelとは | CNCFが推進するオブザーバビリティのオープンスタンダード |
| 構成要素 | API + SDK + Collector |
| プロトコル | OTLP(OpenTelemetry Protocol) |
| 計装方式 | 自動計装(基本)+ 手動計装(業務固有) |
| 最大の利点 | ベンダーロックインの回避と統一的なデータ収集 |
チェックリスト
- OpenTelemetryの役割と位置づけを説明できる
- API、SDK、Collectorの3つのコンポーネントを理解した
- OTLPによるデータ送信の流れを把握した
- 自動計装と手動計装の使い分けを理解した
次のステップへ
次は「SLI/SLO/SLAの定義」を学びます。オブザーバビリティデータを活用して、サービスの信頼性を定量的に管理する方法を見ていきましょう。
推定読了時間: 25分