|
1 | 1 | import 'dart:io'; |
| 2 | +import 'dart:math'; |
2 | 3 |
|
3 | 4 | import '../../../sentry.dart'; |
4 | 5 | import 'enricher_event_processor.dart'; |
@@ -40,6 +41,10 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { |
40 | 41 | ); |
41 | 42 |
|
42 | 43 | contexts['dart_context'] = _getDartContext(); |
| 44 | + contexts['process_info'] = <String, dynamic>{ |
| 45 | + 'currentResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.currentRss), |
| 46 | + 'maxResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.maxRss), |
| 47 | + }; |
43 | 48 |
|
44 | 49 | return event.copyWith( |
45 | 50 | contexts: contexts, |
@@ -116,4 +121,30 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { |
116 | 121 | timezone: culture?.timezone ?? DateTime.now().timeZoneName, |
117 | 122 | ); |
118 | 123 | } |
| 124 | + |
| 125 | + // Reference: |
| 126 | + // https://github.com/erdbeerschnitzel/filesize.dart/blob/4f7c54dc06647b8368078f6febb83149494698c1/lib/filesize.dart |
| 127 | + String _bytesToHumanReadableFileSize(num size) { |
| 128 | + const List<String> affixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; |
| 129 | + |
| 130 | + int round = 2; |
| 131 | + num divider = 1024; |
| 132 | + |
| 133 | + num runningDivider = divider; |
| 134 | + num runningPreviousDivider = 0; |
| 135 | + int affix = 0; |
| 136 | + |
| 137 | + while (size >= runningDivider && affix < affixes.length - 1) { |
| 138 | + runningPreviousDivider = runningDivider; |
| 139 | + runningDivider *= divider; |
| 140 | + affix++; |
| 141 | + } |
| 142 | + |
| 143 | + String result = (runningPreviousDivider == 0 ? size : size / runningPreviousDivider).toStringAsFixed(round); |
| 144 | + |
| 145 | + // Remove trailing zeros if needed |
| 146 | + if (result.endsWith("0" * round)) result = result.substring(0, result.length - round - 1); |
| 147 | + |
| 148 | + return "$result ${affixes[affix]}"; |
| 149 | + } |
119 | 150 | } |
0 commit comments