Slide 1

Slide 1 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 1 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. OpenTelemetry Collector internals Yoshi Yamaguchi (@ymotongpoo) J A P A N C O M M U N I T Y D A Y A T K U B E C O N + C L O U D N A T I V E C O N J A P A N 2 0 2 5 Senior Developer Advocate Amazon Web Services Japan, G.K.

Slide 2

Slide 2 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 2 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾃⼰紹介 ⼭⼝ 能迪(やまぐち よしふみ) アマゾンウェブサービスジャパン合同会社 シニアデベロッパーアドボケイト 専⾨領域 • オブザーバビリティ • SRE全般 @ymotongpoo

Slide 3

Slide 3 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3 OpenTelemetry meetup

Slide 4

Slide 4 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4 ここにいる全員を OpenTelemetry の コントリビューターにする 今⽇の⽬標

Slide 5

Slide 5 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 5 Disclaimer 本セッションの解説は以下のソースコードに基づいています • レポジトリ: open-telemetry/opentelemetry-collector • バージョン: v1.34.0 (rev: ab80fb4)

Slide 6

Slide 6 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 6 • OpenTelemetry についておさらい • OpenTelemetry Collector の設計のおさらい • OpenTelemetry Collector の内部データモデル • データ変換プロセス アジェンダ

Slide 7

Slide 7 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 7 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. OpenTelemetry についておさらい

Slide 8

Slide 8 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 8 OpenTelemetry についておさらい オブザーバビリティの中でも計装、⽣成、送信に注⼒ システム ⼊⼒ 出⼒ 計装 (インスツルメンテーション) 送信 保存 収集 可視化

Slide 9

Slide 9 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 9 OpenTelemetry のスコープ コンポーネント ⼊⼒ 出⼒ API & SDK • OTLP • OTel Arrow Collector

Slide 10

Slide 10 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10 OpenTelemetry のスコープ コンポーネント ⼊⼒ 出⼒ API & SDK Collector

Slide 11

Slide 11 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 11 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. OpenTelemetry Collector の 設計のおさらい

Slide 12

Slide 12 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 12 3つの主要コンポーネント コンポーネントが独⽴ • Receiver • Processor • Exporter • Connector Receiver Processor Exporter

Slide 13

Slide 13 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 13 各コンポーネントの中⾝とその間の通信は︖ Receiver Processor Exporter この中⾝は何してるの︖ ここはどういうデータ型なの︖

Slide 14

Slide 14 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 14 各コンポーネントの中⾝とその間の通信は︖ Receiver Processor Exporter ここはどういうデータ型なの︖

Slide 15

Slide 15 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. OpenTelemetry Collector の 内部データモデル

Slide 16

Slide 16 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 16 事前知識 • OpenTelemetry Collector は Go で実装されている • コアな機能は open-telemetry/opentelemetry-collector にある • OpenTelemetry には標準的なテレメトリー送信プロトコルとして OTLP がある

Slide 17

Slide 17 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 17 内部通信も OTLP なのでは︖ pdata は OTLP をラップした上で追加のデータ型を保持している pdata ⽣成された Go コード OTLP Protobuf protoc-gen-gogo pdatagen 例: ptrace type Span struct { // protobuf構造体への直接参照 orig *otlptrace.Span state *internal.State }

Slide 18

Slide 18 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 18 pdata: パイプラインデータ型 pdata ptrace pmetric plog pcommon pprofile ※ protobuf から⽣成された型は pdata/internal にあるので pdata 内部からしか触れない

Slide 19

Slide 19 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 19 pcommon: パイプライン内共通データ型 テンプレートより⾃動⽣成した共通データ型 • 各種プリミティブ型の値 (Value) • 各種プリミティブ型のスライス (FooSlice) • マップ • Timestamp • SpanID、TraceID、TaceState • リソース

Slide 20

Slide 20 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 20 ptrace: パイプライン内トレース型 Traces ResourceSpans ScopeSpans Span Status SpanEvent SpanLink 1 n n n n n 1 1 1 1 トレース全体 同⼀のリソースからのスパン(例: サービス、ホスト、コンテナ) 同⼀のスコープからのスパン(例: ライブラリ、SDK)

Slide 21

Slide 21 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 21 ptrace: 簡単なコード例 traces := ptrace.NewTraces() // サービスAのResourceSpans rs := traces.ResourceSpans().AppendEmpty() rs.Resource().Attributes().PutStr("service.name", "service-a") // サービスAのResourceSpansのScopeSpan ss := rs.ScopeSpans().AppendEmpty() scope := scopeSpans.Scope() // InstrumentationScopeの設定 scope.SetName("go.opentelemetry.io/otel/instrumentation/net/http/otelhttp") scope.SetVersion("0.46.0") scope.Attributes().PutStr("instrumentation.type", "automatic")

Slide 22

Slide 22 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 22 ptrace: 簡単なコード例 span := scope.Spans().AppendEmpty() span.SetName("HTTP GET /api/users") span.SetKind(ptrace.SpanKindClient) span.SetTraceID(pcommon.TraceID(traceID)) span.SetSpanID(pcommon.SpanID(spanID)) span.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now())) span.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(100*time.M illisecond))) span.Attributes().PutStr("http.method", "GET") span.Attributes().PutStr("http.url", "https://api.example.com/users") span.Attributes().PutInt("http.status_code", 200) span.Status().SetCode(ptrace.StatusCodeOk)

Slide 23

Slide 23 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 23 pmetric: パイプライン内メトリクス型 Metrics ResourceMetrics ScopeMetrics 1 n n n 1 1 1 1 1 1 Metric Histogram Gauge Sum HistogramDataPoint NumberDataPoint 1 n n Exemplar 1 1 同⼀のリソースからのメトリクス(例: サービス、ホスト、コンテナ) 同⼀のスコープからのメトリクス(例: ライブラリ、SDK) メトリクス(名前、単位、メトリクス種類で定義) トレースとメトリクスの紐づけ n 1 1 メトリクス全体

Slide 24

Slide 24 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24 pmetric: 簡単なコード例 metrics := pmetric.NewMetrics() // サービスAのResourceMetrics rm := metrics.ResourceMetrics().AppendEmpty() rm.Resource().Attributes().PutStr("service.name", "payment-service") // HTTPライブラリからのメトリクス sm := resourceMetrics.ScopeMetrics().AppendEmpty() // スコープの設定 scope := sm.Scope() scope.SetName("go.opentelemetry.io/otel/instrumentation/net/http/otelhttp") scope.SetVersion("0.46.0") scope.Attributes().PutStr("instrumentation.type", "automatic")

Slide 25

Slide 25 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 25 pmetric: 簡単なコード例 // HTTP リクエスト数メトリック(Sum) metric := sm.Metrics().AppendEmpty() metric.SetName("http_requests_total") metric.SetDescription("Total number of HTTP requests") metric.SetUnit("1") // Sum タイプとして設定 sum := metric.SetEmptySum() sum.SetIsMonotonic(true) sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)

Slide 26

Slide 26 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 26 pmetric: 簡単なコード例 // データポイントを追加 dp := sum.DataPoints().AppendEmpty() now := pcommon.NewTimestampFromTime(time.Now()) dp.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(-1*time.Minute))) dp.SetTimestamp(now) dp.SetIntValue(1500) // 1500リクエスト // 属性を設定 dp.Attributes().PutStr("method", "GET") dp.Attributes().PutStr("endpoint", "/api/users") dp.Attributes().PutInt("status_code", 200)

Slide 27

Slide 27 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 27 plog: パイプライン内トレース型 Logs ResourceLogs ScopeLogs LogRecord SeverityNumber LogRecordFlag 1 n n n 1 1 1 1 1 ログ全体 同⼀のリソースからのログ(例: サービス、ホスト、コンテナ) 同⼀のスコープからのログ(例: ライブラリ、SDK) ログ⾏(TraceID、SpanID⽤のフィールドもある)

Slide 28

Slide 28 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 28 pprofile: パイプライン内プロファイル型(ベータ) Profiles ResourceProfiles ScopeProfiles Profile 1 n n n 1 1 1 プロファイル全体 同⼀のリソースからのプロファイル(例: サービス、ホスト、コンテナ) 同⼀のスコープからのプロファイル(例: ライブラリ、SDK) プロファイル Sample Label n n 1 サンプル ラベル(ここでTraceIDやSpanIDを相関させる)

Slide 29

Slide 29 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 29 コンポーネントは何をしてるの︖ Receiver Processor Exporter この中⾝は何してるの︖

Slide 30

Slide 30 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 30 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. データ変換プロセス

Slide 31

Slide 31 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 31 各コンポーネントの実装 • Receiver: 外部テレメトリー形式を pdata に変換 • Processor: pdata を別の pdata に変換 • Exporter: pdata を外部テレメトリー形式に変換 • Connector: パイプライン同⼠の接続 • Extension: Collectorの補助的な機能 すべての道は pdata に通じる やったー︕簡単じゃん︕ ...本当に簡単か︖ 今⽇は説明しない

Slide 32

Slide 32 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 32 config.yaml receivers: otlp: grpc: processors: batch: memory_limiter: exporters: otlp: endpoint: "your-endpoint:4317" service: pipelines: traces: receivers: [otlp] processors: [memory_limiter, batch] exporters: [otlp]

Slide 33

Slide 33 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 33 config.yaml から実際のパイプラインへ config.yaml otlp mem batch otlp 概念上のパイプライン

Slide 34

Slide 34 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 34 疑問 1. どうやって config.yaml から各コンポーネントの依存関係を理解しているのか 2. 各コンポーネントはどうやって pdata の送信先を把握しているのか

Slide 35

Slide 35 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 35 コンポーネントの依存関係の構築 service/internal/graph/graph.go で有向⾮巡回グラフを作成 1.どうやって config.yaml から各コンポーネントの依存関係を理解しているのか otlp mem batch otlp otlp mem batch otlp 概念上のパイプライン 実際のパイプライン これは何︖

Slide 36

Slide 36 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 36 CapabilitiesNode と FanOutNode 最適化のためにパイプラインには仮想的なコンポーネントが存在する • CapabilitiesNode: Receiver が必ず接続するノード • FanOutNode: Processor と Exporter の間に必ず挟まれるノード otlp mem batch otlp CapabilitiesNode FanOutNode

Slide 37

Slide 37 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 37 参考: パイプライン⽤構造体の実装 // A node-based representation of a pipeline configuration. type pipelineNodes struct { // Use map to assist with deduplication of connector instances. receivers map[int64]graph.Node // The node to which receivers emit. Passes through to processors. // Easily accessible as the first node in a pipeline. *capabilitiesNode // The order of processors is very important. Therefore use a slice for processors. processors []graph.Node // Emits to exporters. *fanOutNode // Use map to assist with deduplication of connector instances. exporters map[int64]graph.Node }

Slide 38

Slide 38 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 38 Consumer 各コンポーネントを実装するとき、次のコンポーネントのことを知らないでも良い ように設計されている 2. 各コンポーネントはどうやって pdata の送信先を把握しているのか otlp mem batch otlp otlp 実際のパイプライン Receiver 以外は全部 Consumer

Slide 39

Slide 39 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 39 参考: 各コンポーネントの定義(例: トレース) type Traces interface { component.Component } Receiver (receiver/receiver.go) type Traces interface { component.Component consumer.Traces } Processor (processor/processor.go) Exporter (exporter/exporter.go) type Traces interface { component.Component consumer.Traces } type capabilitiesNode struct { …略… baseConsumer consumer.ConsumeTracesFunc …略… } CapabilitiesNode (service/internal/graph/capabilities.go) FanOutNode (service/internal/graph/fanout.go) type fanOutNode struct { …略… baseConsumer } Consumer は ConsumeXXX() を呼べる実装となっている

Slide 40

Slide 40 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 40 どうやって Consumer に pdata を渡すのか 各コンポーネント内にある nextConsumer の consumer.ConsumeXXX を呼ぶ。 これによって次のコンポーネントを知らなくて良くなる。 func CreateTraces( ctx context.Context, set Settings, cfg component.Config, next consumer.Traces ) (Traces, error) Receiver、Processor の実装に必要な Factory インターフェイスの CreateXXX メソッド コンポーネントの実装の本体で pdata を渡す next.ConsumeTrace(ctx, traces)

Slide 41

Slide 41 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 41 どうやって Consumer に pdata を渡すのか Exporter の実装に必要な Factory インターフェイスの CreateXXX メソッドには nextConsumer はない type Factory interface { component.Factory // CreateTraces creates a Traces exporter based on this config. // If the exporter type does not support tracing, // this function returns the error [pipeline.ErrSignalNotSupported]. CreateTraces( ctx context.Context, set Settings, cfg component.Config ) (Traces, error) // ...略... Exporter はパイプラインの最後で 次に Consumer はいないから

Slide 42

Slide 42 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 42 渡された pdata からのデータの取り出し⽅ Factory を実装する際に前のコンポーネントから呼び出される consumer.ConsumeXXXX を実装する必要があり、そこで pdata を直接触れる Processor の場合 Processor ⾃体の ConsumeXXX の実装内で nextConsumer.ConsumeXXX を呼び出して 連鎖させる Exporter の場合 Processor ⾃体の ConsumeXXX の実装内で 外部データ形式に変換して送信する 例: トレース

Slide 43

Slide 43 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 43 サンプルコード(概念) func (p *processor) ConsumeTraces(ctx context.Context, td ptrace.Traces) error { newTd := doSomthingWith(td) return next.ConsumeTraces(ctx, newTd) } 前のコンポーネントから呼び出される ConsumeXXX 次のコンポーネントの ConsumeXXX を呼び出す

Slide 44

Slide 44 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 44 今⽇話さなかったこと • 各コンポーネントの実際の作り⽅ • コンポーネント作成の規約、Factory の作り⽅ • mdatagen というツールを使います • Extension と Connector • CapabilitiesNode と FanOutNode が何を最適化してるか • Feature Gate https://zenn.dev/ymotongpoo/articles/20231225-metrics-receiver

Slide 45

Slide 45 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 45 まとめ • OpenTelemetry Collector 内部ではデータはすべて pdata になってる • 各コンポーネントは Receiver 以外は Consumer として振る舞う • ConsumeXXX のチェーンで pdata を渡している

Slide 46

Slide 46 text

© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 46 Thank you! © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Yoshi Yamaguchi @ymotongpoo Any questions?