Skip to content
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

Add memory usage to contexts #2133

Merged
merged 19 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Capture total frames, frames delay, slow & frozen frames and attach to spans ([#2106](https://github.com/getsentry/sentry-dart/pull/2106))
- Support WebAssembly compilation (dart2wasm) ([#2113](https://github.com/getsentry/sentry-dart/pull/2113))
- Add memory usage to contexts ([#2133](https://github.com/getsentry/sentry-dart/pull/2133))
denrase marked this conversation as resolved.
Show resolved Hide resolved
denrase marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
);

contexts['dart_context'] = _getDartContext();
contexts['process_info'] = <String, dynamic>{
'currentResidentSetSize':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add currentRss to app_memory

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does currentRss compare to what the native side sets? Does it override what the native side sets?

Copy link
Collaborator Author

@denrase denrase Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code and we only set these values if there are no native SDKs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think android sets it, I know iOS for sure does @denrase

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated so we set appMemory to ProcessInfo.currentRss, it it's not already set, regardless of native integration. The other values are merged, so this is additional information.

On Linux/Windows we now also add free and total system memory, if possible.

_bytesToHumanReadableFileSize(ProcessInfo.currentRss),
'maxResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.maxRss),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any alternatives for web?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this, but there's not much documentation: https://api.dart.dev/stable/2.18.0/dart-html/MemoryInfo/usedJSHeapSize.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is based on this https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory

it's deprecated and not all browsers support it, dunno if it's worth the effort to add this if we have to remove it again at some point

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i removed it after reading the docs and added some info.

};

return event.copyWith(
contexts: contexts,
Expand Down Expand Up @@ -116,4 +121,34 @@
timezone: culture?.timezone ?? DateTime.now().timeZoneName,
);
}

// Reference:
// https://github.com/erdbeerschnitzel/filesize.dart/blob/4f7c54dc06647b8368078f6febb83149494698c1/lib/filesize.dart
String _bytesToHumanReadableFileSize(num size) {
const List<String> affixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];

int round = 2;
num divider = 1024;

num runningDivider = divider;
num runningPreviousDivider = 0;
int affix = 0;

while (size >= runningDivider && affix < affixes.length - 1) {
runningPreviousDivider = runningDivider;
runningDivider *= divider;
affix++;
}

String result =
(runningPreviousDivider == 0 ? size : size / runningPreviousDivider)
.toStringAsFixed(round);

// Remove trailing zeros if needed
if (result.endsWith("0" * round)) {
result = result.substring(0, result.length - round - 1);

Check warning on line 149 in dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart

View check run for this annotation

Codecov / codecov/patch

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

Added line #L149 was not covered by tests
}

return "$result ${affixes[affix]}";
}
}
Loading