-
-
Notifications
You must be signed in to change notification settings - Fork 284
Move TelemetryProcessor from span-first branch and replace LogBatcher
#3448
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1a4b78b
Add TelemetryProcessor for span and log buffering
buenaflor 3897122
Remove SpanV2 and TraceLifecycle dependencies
buenaflor 2c617bb
Remove span-related tests from sentry_client_test
buenaflor 6ef8c3c
Remove span-related processor tests
buenaflor 3ca4c08
Remove span import from Flutter mocks
buenaflor 9b34042
Fix wiring up
buenaflor e0b564c
Update
buenaflor 6da49c8
Update
buenaflor 1b97198
Update CHANGELOG
buenaflor 82a4374
Update
buenaflor 58c9c92
Remove logbatcher
buenaflor 3630966
Polish
buenaflor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,13 @@ | ||
| import 'dart:async'; | ||
|
|
||
| /// A buffer that batches telemetry items for efficient transmission to Sentry. | ||
| /// | ||
| /// Collects items of type [T] and sends them in batches rather than | ||
| /// individually, reducing network overhead. | ||
| abstract class TelemetryBuffer<T> { | ||
| /// Adds an item to the buffer. | ||
| void add(T item); | ||
|
|
||
| /// When executed immediately sends all buffered items to Sentry and clears the buffer. | ||
| FutureOr<void> flush(); | ||
| } |
15 changes: 15 additions & 0 deletions
15
packages/dart/lib/src/telemetry/processing/buffer_config.dart
This file contains hidden or 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,15 @@ | ||
| final class TelemetryBufferConfig { | ||
| final Duration flushTimeout; | ||
| final int maxBufferSizeBytes; | ||
| final int maxItemCount; | ||
|
|
||
| const TelemetryBufferConfig({ | ||
| this.flushTimeout = defaultFlushTimeout, | ||
| this.maxBufferSizeBytes = defaultMaxBufferSizeBytes, | ||
| this.maxItemCount = defaultMaxItemCount, | ||
| }); | ||
|
|
||
| static const Duration defaultFlushTimeout = Duration(seconds: 5); | ||
| static const int defaultMaxBufferSizeBytes = 1024 * 1024; | ||
| static const int defaultMaxItemCount = 100; | ||
| } |
143 changes: 143 additions & 0 deletions
143
packages/dart/lib/src/telemetry/processing/in_memory_buffer.dart
This file contains hidden or 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,143 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import '../../utils/internal_logger.dart'; | ||
| import 'buffer.dart'; | ||
| import 'buffer_config.dart'; | ||
|
|
||
| /// Callback invoked when the buffer is flushed with the accumulated data. | ||
| typedef OnFlushCallback<T> = FutureOr<void> Function(T data); | ||
|
|
||
| /// Encodes an item of type [T] into bytes. | ||
| typedef ItemEncoder<T> = List<int> Function(T item); | ||
|
|
||
| /// Base class for in-memory telemetry buffers. | ||
| /// | ||
| /// Buffers telemetry items in memory and flushes them when either the | ||
| /// configured size limit, item count limit, or flush timeout is reached. | ||
| abstract base class _BaseInMemoryTelemetryBuffer<T, S> | ||
| implements TelemetryBuffer<T> { | ||
| final TelemetryBufferConfig _config; | ||
| final ItemEncoder<T> _encoder; | ||
| final OnFlushCallback<S> _onFlush; | ||
|
|
||
| S _storage; | ||
| int _bufferSize = 0; | ||
| int _itemCount = 0; | ||
| Timer? _flushTimer; | ||
|
|
||
| _BaseInMemoryTelemetryBuffer({ | ||
| required ItemEncoder<T> encoder, | ||
| required OnFlushCallback<S> onFlush, | ||
| required S initialStorage, | ||
| TelemetryBufferConfig config = const TelemetryBufferConfig(), | ||
| }) : _encoder = encoder, | ||
| _onFlush = onFlush, | ||
| _storage = initialStorage, | ||
| _config = config; | ||
|
|
||
| S _createEmptyStorage(); | ||
| void _store(List<int> encoded, T item); | ||
| bool get _isEmpty; | ||
|
|
||
| bool get _isBufferFull => | ||
| _bufferSize >= _config.maxBufferSizeBytes || | ||
| _itemCount >= _config.maxItemCount; | ||
|
|
||
| @override | ||
| void add(T item) { | ||
| final List<int> encoded; | ||
| try { | ||
| encoded = _encoder(item); | ||
| } catch (exception, stackTrace) { | ||
| internalLogger.error( | ||
| '$runtimeType: Failed to encode item, dropping', | ||
| error: exception, | ||
| stackTrace: stackTrace, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (encoded.length > _config.maxBufferSizeBytes) { | ||
| internalLogger.warning( | ||
| '$runtimeType: Item size ${encoded.length} exceeds buffer limit ${_config.maxBufferSizeBytes}, dropping', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| _store(encoded, item); | ||
| _bufferSize += encoded.length; | ||
| _itemCount++; | ||
|
|
||
| if (_isBufferFull) { | ||
| internalLogger.debug( | ||
| '$runtimeType: Buffer full, flushing $_itemCount items', | ||
| ); | ||
| flush(); | ||
| } else { | ||
| _flushTimer ??= Timer(_config.flushTimeout, flush); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| FutureOr<void> flush() { | ||
| _flushTimer?.cancel(); | ||
| _flushTimer = null; | ||
|
|
||
| if (_isEmpty) return null; | ||
|
|
||
| final toFlush = _storage; | ||
| final flushedCount = _itemCount; | ||
| final flushedSize = _bufferSize; | ||
| _storage = _createEmptyStorage(); | ||
| _bufferSize = 0; | ||
| _itemCount = 0; | ||
|
|
||
| final successMessage = | ||
| '$runtimeType: Flushed $flushedCount items ($flushedSize bytes)'; | ||
| final errorMessage = | ||
| '$runtimeType: Flush failed for $flushedCount items ($flushedSize bytes)'; | ||
|
|
||
| try { | ||
| final result = _onFlush(toFlush); | ||
| if (result is Future) { | ||
| return result.then( | ||
| (_) => internalLogger.debug(successMessage), | ||
| onError: (exception, stackTrace) => internalLogger.warning( | ||
| errorMessage, | ||
| error: exception, | ||
| stackTrace: stackTrace, | ||
| ), | ||
| ); | ||
| } | ||
| internalLogger.debug(successMessage); | ||
| } catch (exception, stackTrace) { | ||
| internalLogger.warning( | ||
| errorMessage, | ||
| error: exception, | ||
| stackTrace: stackTrace, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// In-memory buffer that collects telemetry items as a flat list. | ||
| /// | ||
| /// Items are encoded and stored in insertion order. On flush, the entire | ||
| /// list of encoded items is passed to the [OnFlushCallback]. | ||
| final class InMemoryTelemetryBuffer<T> | ||
| extends _BaseInMemoryTelemetryBuffer<T, List<List<int>>> { | ||
| InMemoryTelemetryBuffer({ | ||
| required super.encoder, | ||
| required super.onFlush, | ||
| super.config, | ||
| }) : super(initialStorage: []); | ||
|
|
||
| @override | ||
| List<List<int>> _createEmptyStorage() => []; | ||
|
|
||
| @override | ||
| void _store(List<int> encoded, T item) => _storage.add(encoded); | ||
|
|
||
| @override | ||
| bool get _isEmpty => _storage.isEmpty; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.