これはOpenTelemetry Advent Calendar 2025 2日目のエントリです。2週間に1回欠かさずOpenTelemetery Collectorのアップデートを追っている身として、「意外と知られていないのでは?」と個人的に思っているネタを取り上げます。
Batch Processorの利用推奨について
OpenTelemetry Collectorをお使いのみなさん。Batch Processorを利用していますか?
Batch Processorは複数のテレメトリーデータを1つのbatchにまとめる役割を果たします。受け取った・生成されたテレメトリーを即時にexportする手法と比較して、オブザーバビリティバックエンドにデータを送信するときの接続数が減らせたり、データの圧縮がより効くようになったりして、結果データ転送が最適化されます。
さて、OpenTelemetry Collectorをある程度の期間使っている人は、configファイルに以下のようにBatch Processorを仕込んでいることでしょう:
receivers: otlp: protocols: grpc: http: processors: batch: exporters: otlp: endpoint: your-observability-backend.example:4317 service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [otlp]
なぜなら、Batch Processorを使用することが以下のように推奨されてきており、ドキュメントでもBatch Processorをパイプラインに含めた設定例が掲載されてきたからです。
It is highly recommended to configure the batch processor on every collector.
しかしこれはかつての話であり、現在ではBatch Processorの使用の推奨が取り止められていることをご存知でしょうか?
exporterのsending_queue設定
Batch Processorの利用が推奨されなくなったからといって、exportの最適化のためにテレメトリーデータをまとめるニーズがなくなったわけでもありません。この目的を満たす代わりに利用できるExporterの設定があります。
OTLP ExporterやOTLP HTTP Exporterにはsending_queueという設定があり、この中でbatchを有効化することができます。
簡単には、以下のようにsending_queue: batch: {}という設定を加えるだけです。デフォルトでbatchが有効化されているわけではないことに注意してください。
processors: - batch: exporters: otlp: endpoint: your-observability-backend.example:4317 + sending_queue: + batch:
元々Batch Processorでバッチサイズやタイムアウト時間を調整して使っていた場合には、例えば以下のように詳細に記述すると良いでしょう。
processors: - batch: - timeout: 1s - send_batch_size: 4096 - send_batch_max_size: 4096 exporters: otlp: endpoint: your-observability-backend.example:4317 + sending_queue: + batch: + flush_timeout: 1s + min_size: 4096 + max_size: 4096
あとはserviceのpipelines設定から不要になったbatch processorを外してあげれば移行完了です!
最新のドキュメントのOpenTelemetry Collector設定例を見ても、otlp exporterのconfig部分にsending_queue: batch:と書かれており、パイプラインにbatch processorが含まれていないことがわかります。
exporters: otlp: endpoint: otelcol:4317 sending_queue: batch: service: pipelines: traces: receivers: [otlp] exporters: [otlp]
🔗 https://opentelemetry.io/docs/collector/configuration/#basics
また、推奨されるProcessorやその適用順序が書かれているドキュメントでも、「Batch Processorの代わりにExporterに付随するバッチ設定を利用するのがおすすめです」という内容の記述が書かれています。
batch, although prefer using the exporter's batching capabilities
🔗 https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor#recommended-processors
Mackerel OTLP Exporterのデフォルト設定
公式のOTLP Exporterに限らず、go.opentelemetry.io/collector/exporter/exporterhelperが提供するQueueBatchのヘルパーを利用して実装されているExporterでは同様の設定が利用できるはずです。
先日、Mackerel向けのOpenTelemetry CollectorのディストリビューションならびにMackerel OTLP Exporterを公開しました。
Mackerel OTLP Exporterもexporterhelperの設定定義や関数を利用しており、sending_queueによりexporterのbatchを設定できます。
Mackerelへのトレース投稿時にはリクエストサイズを6MB以下に抑えなければならないという制約があります。Mackerel OTLP Exporterが提供するデフォルト設定をそのまま使うと、何も書かずにbatchが有効になるだけでなく、この制約に合わせてbatchサイズを調整してくれるようになっています。
queueConfig := exporterhelper.NewDefaultQueueConfig() // overrides default exporter queue batch config // because Vaxila endpoint does not accept requests larger than 6MB. queueBatchConfig := queueConfig.Batch.GetOrInsertDefault() queueBatchConfig.Sizer = exporterhelper.RequestSizerTypeBytes queueBatchConfig.MaxSize = defaultBatchMaxSizeBytes queueConfig.Batch = configoptional.Some(*queueBatchConfig)