Skip to content

Commit

Permalink
O11Y-3733: opentelemetry-dart: Implement Improvements to Timer in Bat…
Browse files Browse the repository at this point in the history
…chSpanProcessor
  • Loading branch information
michaelyeager-wf committed Nov 22, 2023
1 parent 322f988 commit 3fce3fc
Showing 1 changed file with 9 additions and 32 deletions.
41 changes: 9 additions & 32 deletions lib/src/sdk/trace/span_processors/batch_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ class BatchSpanProcessor implements sdk.SpanProcessor {
final Logger _log = Logger('opentelemetry.BatchSpanProcessor');
final int _maxExportBatchSize;
final int _maxQueueSize;
final int _scheduledDelayMillis;
final List<sdk.ReadOnlySpan> _spanBuffer = [];

bool _isShutdown = false;
late final Timer _timer;

Timer? _timer;
bool _isShutdown = false;

BatchSpanProcessor(this._exporter,
{int maxExportBatchSize = _DEFAULT_MAXIMUM_BATCH_SIZE,
int scheduledDelayMillis = _DEFAULT_EXPORT_DELAY})
: _maxExportBatchSize = maxExportBatchSize,
_maxQueueSize = _DEFAULT_MAXIMUM_QUEUE_SIZE,
_scheduledDelayMillis = scheduledDelayMillis;
_maxQueueSize = _DEFAULT_MAXIMUM_QUEUE_SIZE {
_timer = Timer.periodic(
Duration(milliseconds: scheduledDelayMillis), _exportBatch);
}

@override
void forceFlush() {
if (_isShutdown) {
return;
}
while (_spanBuffer.isNotEmpty) {
_flushBatch();
_exportBatch(_timer);
}
_exporter.forceFlush();
}
Expand All @@ -58,7 +59,7 @@ class BatchSpanProcessor implements sdk.SpanProcessor {
void shutdown() {
forceFlush();
_isShutdown = true;
_clearTimer();
_timer.cancel();
_exporter.shutdown();
}

Expand All @@ -71,33 +72,9 @@ class BatchSpanProcessor implements sdk.SpanProcessor {
}

_spanBuffer.add(span);
_startTimer();
}

void _startTimer() {
if (_timer != null) {
// _timer already defined.
return;
}

_timer = Timer(Duration(milliseconds: _scheduledDelayMillis), () {
_flushBatch();
if (_spanBuffer.isNotEmpty) {
_clearTimer();
_startTimer();
}
});
}

void _clearTimer() {
if (_timer != null) {
_timer?.cancel();
_timer = null;
}
}

void _flushBatch() {
_clearTimer();
void _exportBatch(Timer timer) {
if (_spanBuffer.isEmpty) {
return;
}
Expand Down

0 comments on commit 3fce3fc

Please sign in to comment.