-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Workiva/console-reporter
O11Y-993: OTEL Dart: Implement OTLP Console Reporter
- Loading branch information
Showing
22 changed files
with
477 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export 'src/sdk/trace/exporters/console_exporter.dart' | ||
show | ||
ConsoleExporter; | ||
export 'src/sdk/trace/span_processors/batch_processor.dart' | ||
show | ||
BatchSpanProcessor; | ||
export 'src/sdk/trace/span_processors/simple_processor.dart' | ||
show | ||
SimpleSpanProcessor; | ||
export 'src/sdk/trace/tracer_provider.dart' | ||
show | ||
TracerProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import '../../../api/trace/span.dart'; | ||
|
||
import 'span_exporter.dart'; | ||
|
||
class ConsoleExporter implements SpanExporter { | ||
var _isShutdown = false; | ||
|
||
void _printSpans(List<Span> spans) { | ||
for (var i=0; i < spans.length; i++) { | ||
final span = spans[i]; | ||
print({ | ||
'traceId': span.spanContext.traceId, | ||
'parentId': span.parentSpanId, | ||
'name': span.name, | ||
'id': span.spanContext.spanId, | ||
'timestamp': span.startTime, | ||
'duration': span.endTime - span.startTime, | ||
'status': span.status.code | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
void export(List<Span> spans) { | ||
if (_isShutdown) { | ||
return; | ||
} | ||
|
||
_printSpans(spans); | ||
} | ||
|
||
@override | ||
void shutdown() { | ||
_isShutdown = true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import '../../../api/trace/span.dart'; | ||
|
||
abstract class SpanExporter { | ||
void export(List<Span> spans); | ||
|
||
void shutdown(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import 'dart:async'; | ||
import 'dart:math'; | ||
|
||
import 'package:logging/logging.dart'; | ||
|
||
import '../../../api/trace/span.dart'; | ||
import '../exporters/span_exporter.dart'; | ||
import 'span_processor.dart'; | ||
|
||
class BatchSpanProcessor implements SpanProcessor { | ||
final _log = Logger('opentelemetry.BatchSpanProcessor'); | ||
|
||
final SpanExporter _exporter; | ||
bool _isShutdown = false; | ||
final List<Span> _spanBuffer = []; | ||
Timer _timer; | ||
|
||
int _maxExportBatchSize = 512; | ||
final int _maxQueueSize = 2048; | ||
int _scheduledDelay = 5000; | ||
|
||
BatchSpanProcessor(this._exporter, {int maxExportBatchSize, int scheduledDelay}) { | ||
_maxExportBatchSize = maxExportBatchSize; | ||
_scheduledDelay = scheduledDelay; | ||
} | ||
|
||
@override | ||
void forceFlush() { | ||
if (_isShutdown) { | ||
return; | ||
} | ||
while (_spanBuffer.isNotEmpty) { | ||
_flushBatch(); | ||
} | ||
} | ||
|
||
@override | ||
void onEnd(Span span) { | ||
if (_isShutdown) { | ||
return; | ||
} | ||
_addToBuffer(span); | ||
} | ||
|
||
@override | ||
void onStart() {} | ||
|
||
@override | ||
void shutdown() { | ||
forceFlush(); | ||
_isShutdown = true; | ||
_clearTimer(); | ||
_exporter.shutdown(); | ||
} | ||
|
||
void _addToBuffer(Span span) { | ||
if (_spanBuffer.length >= _maxQueueSize) { | ||
// Buffer is full, drop span. | ||
_log.warning('Max queue size exceeded. Dropping ${_spanBuffer.length} spans.'); | ||
return; | ||
} | ||
|
||
_spanBuffer.add(span); | ||
_startTimer(); | ||
} | ||
|
||
void _startTimer() { | ||
if (_timer != null) { | ||
// _timer already defined. | ||
return; | ||
} | ||
|
||
_timer = Timer(Duration(milliseconds: _scheduledDelay), () { | ||
_flushBatch(); | ||
if (_spanBuffer.isNotEmpty) { | ||
_clearTimer(); | ||
_startTimer(); | ||
} | ||
}); | ||
} | ||
|
||
void _clearTimer() { | ||
if (_timer == null) { | ||
// _timer not set. | ||
return; | ||
} | ||
|
||
_timer.cancel(); | ||
_timer = null; | ||
} | ||
|
||
void _flushBatch() { | ||
_clearTimer(); | ||
if (_spanBuffer.isEmpty) { | ||
return; | ||
} | ||
|
||
final batchSize = min(_spanBuffer.length, _maxExportBatchSize); | ||
final batch = _spanBuffer.sublist(0, batchSize); | ||
_spanBuffer.removeRange(0, batchSize); | ||
|
||
_exporter.export(batch); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import '../../../api/trace/span.dart'; | ||
import '../exporters/span_exporter.dart'; | ||
import 'span_processor.dart'; | ||
|
||
class SimpleSpanProcessor implements SpanProcessor { | ||
final SpanExporter _exporter; | ||
bool _isShutdown = false; | ||
|
||
SimpleSpanProcessor(this._exporter); | ||
|
||
@override | ||
void forceFlush() {} | ||
|
||
@override | ||
void onEnd(Span span) { | ||
if (_isShutdown) { | ||
return; | ||
} | ||
|
||
_exporter.export([span]); | ||
} | ||
|
||
@override | ||
void onStart() {} | ||
|
||
@override | ||
void shutdown() { | ||
forceFlush(); | ||
_isShutdown = true; | ||
_exporter.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import '../../../api/trace/span.dart'; | ||
|
||
abstract class SpanProcessor { | ||
void onStart(); | ||
|
||
void onEnd(Span span); | ||
|
||
void shutdown(); | ||
|
||
void forceFlush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
import '../../../src/api/trace/tracer_provider.dart' as api; | ||
import '../../api/trace/tracer_provider.dart' as api; | ||
import 'exporters/console_exporter.dart'; | ||
import 'span_processors/simple_processor.dart'; | ||
import 'span_processors/span_processor.dart'; | ||
import 'tracer.dart'; | ||
|
||
/// A registry for creating named [Tracer]s. | ||
class TracerProvider implements api.TracerProvider { | ||
final Map<String, Tracer> _tracers = {}; | ||
List<SpanProcessor> _processors; | ||
|
||
TracerProvider({List<SpanProcessor> processors}) { | ||
if (processors == null) { | ||
_processors = [SimpleSpanProcessor(ConsoleExporter())]; | ||
} else { | ||
_processors = processors; | ||
} | ||
} | ||
|
||
List<SpanProcessor> get spanProcessors => _processors; | ||
|
||
@override | ||
Tracer getTracer(String name, {String version = ''}) { | ||
final key = '$name@$version'; | ||
return _tracers.putIfAbsent(key, () => Tracer()); | ||
return _tracers.putIfAbsent(key, () => Tracer(_processors)); | ||
} | ||
|
||
@override | ||
void forceFlush() { | ||
for (var i = 0; i < _processors.length; i++) { | ||
_processors[i].forceFlush(); | ||
} | ||
} | ||
|
||
@override | ||
void shutdown() { | ||
for (var i = 0; i < _processors.length; i++) { | ||
_processors[i].shutdown(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.