-
-
Notifications
You must be signed in to change notification settings - Fork 276
Improve SentryLogBatcher flush logic
#3211
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
7 commits
Select commit
Hold shift + click to select a range
41e88a9
Update log batcher
denrase 425f782
add cl entry
denrase b3cdf53
rename internal m
denrase 9eb6181
Merge branch 'main' into enha/update-log-flushing
denrase 9ea6204
use bytesbuilder
denrase 5008b71
format
denrase 586b87a
Merge branch 'main' into enha/update-log-flushing
denrase 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 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 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 |
|---|---|---|
| @@ -1,52 +1,97 @@ | ||
| import 'dart:async'; | ||
| import 'sentry_envelope.dart'; | ||
| import 'sentry_options.dart'; | ||
| import 'protocol/sentry_log.dart'; | ||
| import 'protocol/sentry_level.dart'; | ||
| import 'sentry_envelope.dart'; | ||
| import 'utils.dart'; | ||
| import 'package:meta/meta.dart'; | ||
|
|
||
| @internal | ||
| class SentryLogBatcher { | ||
| SentryLogBatcher(this._options, {Duration? flushTimeout, int? maxBufferSize}) | ||
| : _flushTimeout = flushTimeout ?? Duration(seconds: 5), | ||
| _maxBufferSize = maxBufferSize ?? 100; | ||
| SentryLogBatcher( | ||
| this._options, { | ||
| Duration? flushTimeout, | ||
| int? maxBufferSizeBytes, | ||
| }) : _flushTimeout = flushTimeout ?? Duration(seconds: 5), | ||
| _maxBufferSizeBytes = maxBufferSizeBytes ?? | ||
| 1024 * 1024; // 1MB default per BatchProcessor spec | ||
|
|
||
| final SentryOptions _options; | ||
| final Duration _flushTimeout; | ||
| final int _maxBufferSize; | ||
| final int _maxBufferSizeBytes; | ||
|
|
||
| final _logBuffer = <SentryLog>[]; | ||
| // Store encoded log data instead of raw logs to avoid re-serialization | ||
| final List<List<int>> _encodedLogs = []; | ||
| int _encodedLogsSize = 0; | ||
|
|
||
| Timer? _flushTimer; | ||
|
|
||
| /// Adds a log to the buffer. | ||
| void addLog(SentryLog log) { | ||
| _logBuffer.add(log); | ||
| try { | ||
| final encodedLog = utf8JsonEncoder.convert(log.toJson()); | ||
|
|
||
| _flushTimer?.cancel(); | ||
| _encodedLogs.add(encodedLog); | ||
| _encodedLogsSize += encodedLog.length; | ||
|
|
||
| if (_logBuffer.length >= _maxBufferSize) { | ||
| return flush(); | ||
| } else { | ||
| _flushTimer = Timer(_flushTimeout, flush); | ||
| // Flush if size threshold is reached | ||
| if (_encodedLogsSize >= _maxBufferSizeBytes) { | ||
| // Buffer size exceeded, flush immediately | ||
| _performFlushLogs(); | ||
| } else if (_flushTimer == null) { | ||
| // Start timeout only when first item is added | ||
| _startTimer(); | ||
| } | ||
| // Note: We don't restart the timer on subsequent additions per spec | ||
| } catch (error) { | ||
| _options.log( | ||
| SentryLevel.error, | ||
| 'Failed to encode log: $error', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Flushes the buffer immediately, sending all buffered logs. | ||
| void flush() { | ||
| _performFlushLogs(); | ||
| } | ||
|
|
||
| void _startTimer() { | ||
| _flushTimer = Timer(_flushTimeout, () { | ||
| _options.log( | ||
| SentryLevel.debug, | ||
| 'SentryLogBatcher: Timer fired, calling performCaptureLogs().', | ||
| ); | ||
| _performFlushLogs(); | ||
| }); | ||
| } | ||
|
|
||
| void _performFlushLogs() { | ||
| // Reset timer state first | ||
| _flushTimer?.cancel(); | ||
| _flushTimer = null; | ||
|
|
||
| final logs = List<SentryLog>.from(_logBuffer); | ||
| _logBuffer.clear(); | ||
| // Reset buffer on function exit | ||
| final logsToSend = List<List<int>>.from(_encodedLogs); | ||
| _encodedLogs.clear(); | ||
| _encodedLogsSize = 0; | ||
|
|
||
| if (logs.isEmpty) { | ||
| if (logsToSend.isEmpty) { | ||
| _options.log( | ||
| SentryLevel.debug, | ||
| 'SentryLogBatcher: No logs to flush.', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| final envelope = SentryEnvelope.fromLogs( | ||
| logs, | ||
| _options.sdk, | ||
| ); | ||
|
|
||
| // TODO: Make sure the Android SDK understands the log envelope type. | ||
| _options.transport.send(envelope); | ||
| try { | ||
| final envelope = SentryEnvelope.fromLogsData(logsToSend, _options.sdk); | ||
| _options.transport.send(envelope); | ||
| } catch (error) { | ||
| _options.log( | ||
| SentryLevel.error, | ||
| 'Failed to send batched logs: $error', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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
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.