BatchSpanProcessor constructor

BatchSpanProcessor(
  1. SpanExporter exporter, [
  2. BatchSpanProcessorConfig? config
])

Creates a new BatchSpanProcessor with the specified exporter and configuration.

The BatchSpanProcessor collects finished spans in a queue and exports them in batches at regular intervals. This improves efficiency compared to exporting each span individually.

A timer is started when this processor is created based on the config's scheduleDelay. The timer triggers periodic batch exports of completed spans to the configured exporter.

When the maximum queue size is reached, new spans will be dropped and not exported.

This processor does not modify spans on start or when their names are updated, it only processes spans when they end.

If an error occurs during export, it will be logged but not propagated.

exporter The SpanExporter to use for exporting batches of spans config Optional configuration for the batch processor

Implementation

BatchSpanProcessor(this.exporter, [BatchSpanProcessorConfig? config])
    : _config = config ?? const BatchSpanProcessorConfig() {
  _timer = Timer.periodic(_config.scheduleDelay, (_) async {
    try {
      await _exportBatch();
      // Exporter errors are caught inside _exportSingleBatch; this is a
      // last-resort guard so an unexpected failure can never become an
      // unhandled async error inside a periodic timer.
      // coverage:ignore-start
    } catch (e) {
      if (OTelLog.isError()) OTelLog.error('Error in batch export timer: $e');
    }
    // coverage:ignore-end
  });
}