Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

O11Y-3733: opentelemetry-dart: Implement Improvements to Timer in BatchSpanProcessor #137

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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