Skip to content

Commit cf5df45

Browse files
committed
Add memory usage to contexts
1 parent e4d5aa8 commit cf5df45

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:io';
2+
import 'dart:math';
23

34
import '../../../sentry.dart';
45
import 'enricher_event_processor.dart';
@@ -40,6 +41,10 @@ class IoEnricherEventProcessor implements EnricherEventProcessor {
4041
);
4142

4243
contexts['dart_context'] = _getDartContext();
44+
contexts['process_info'] = <String, dynamic>{
45+
'currentResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.currentRss),
46+
'maxResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.maxRss),
47+
};
4348

4449
return event.copyWith(
4550
contexts: contexts,
@@ -116,4 +121,30 @@ class IoEnricherEventProcessor implements EnricherEventProcessor {
116121
timezone: culture?.timezone ?? DateTime.now().timeZoneName,
117122
);
118123
}
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+
}
119150
}

0 commit comments

Comments
 (0)