From cf5df45c9940787d1a77386840d3375c2d483c3f Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Jun 2024 16:00:46 +0200 Subject: [PATCH 01/18] Add memory usage to contexts --- .../enricher/io_enricher_event_processor.dart | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index 52243a8572..aed70a7d71 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'dart:math'; import '../../../sentry.dart'; import 'enricher_event_processor.dart'; @@ -40,6 +41,10 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { ); contexts['dart_context'] = _getDartContext(); + contexts['process_info'] = { + 'currentResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.currentRss), + 'maxResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.maxRss), + }; return event.copyWith( contexts: contexts, @@ -116,4 +121,30 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { timezone: culture?.timezone ?? DateTime.now().timeZoneName, ); } + + // Reference: + // https://github.com/erdbeerschnitzel/filesize.dart/blob/4f7c54dc06647b8368078f6febb83149494698c1/lib/filesize.dart + String _bytesToHumanReadableFileSize(num size) { + const List 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); + + return "$result ${affixes[affix]}"; + } } From eeb797feb91e37b575bf7babff0f064d095cc006 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Jun 2024 16:16:26 +0200 Subject: [PATCH 02/18] add cl entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f40482e232..de57069ede 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) ### Deprecated From 7c05a02ba34c420f1aa959df2dffc4f636726270 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Jun 2024 16:31:44 +0200 Subject: [PATCH 03/18] format --- .../enricher/io_enricher_event_processor.dart | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index aed70a7d71..0e4279cb40 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -42,7 +42,8 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { contexts['dart_context'] = _getDartContext(); contexts['process_info'] = { - 'currentResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.currentRss), + 'currentResidentSetSize': + _bytesToHumanReadableFileSize(ProcessInfo.currentRss), 'maxResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.maxRss), }; @@ -140,10 +141,13 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { affix++; } - String result = (runningPreviousDivider == 0 ? size : size / runningPreviousDivider).toStringAsFixed(round); + 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); + if (result.endsWith("0" * round)) + result = result.substring(0, result.length - round - 1); return "$result ${affixes[affix]}"; } From d3302e8324ab4fb2aeff53f97bf88615b94d402e Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Jun 2024 16:32:50 +0200 Subject: [PATCH 04/18] run dart fix --- .../event_processor/enricher/io_enricher_event_processor.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index 0e4279cb40..6833e5ada1 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -1,5 +1,4 @@ import 'dart:io'; -import 'dart:math'; import '../../../sentry.dart'; import 'enricher_event_processor.dart'; @@ -146,8 +145,9 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { .toStringAsFixed(round); // Remove trailing zeros if needed - if (result.endsWith("0" * round)) + if (result.endsWith("0" * round)) { result = result.substring(0, result.length - round - 1); + } return "$result ${affixes[affix]}"; } From b210da57356151a0972775fe8c5f11b296a8a271 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 10:49:27 +0200 Subject: [PATCH 05/18] Set ProcessInfo.currentRss as appMemory --- .../enricher/io_enricher_event_processor.dart | 52 +++++-------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index 6833e5ada1..79da3f0833 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -20,31 +20,31 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { // If there's a native integration available, it probably has better // information available than Flutter. + final device = _options.platformChecker.hasNativeIntegration + ? null + : _getDevice(event.contexts.device); + final os = _options.platformChecker.hasNativeIntegration ? null : _getOperatingSystem(event.contexts.operatingSystem); - final device = _options.platformChecker.hasNativeIntegration + final app = _options.platformChecker.hasNativeIntegration ? null - : _getDevice(event.contexts.device); + : _getApp(event.contexts.app); final culture = _options.platformChecker.hasNativeIntegration ? null : _getSentryCulture(event.contexts.culture); final contexts = event.contexts.copyWith( - operatingSystem: os, device: device, + operatingSystem: os, runtimes: _getRuntimes(event.contexts.runtimes), + app: app, culture: culture, ); contexts['dart_context'] = _getDartContext(); - contexts['process_info'] = { - 'currentResidentSetSize': - _bytesToHumanReadableFileSize(ProcessInfo.currentRss), - 'maxResidentSetSize': _bytesToHumanReadableFileSize(ProcessInfo.maxRss), - }; return event.copyWith( contexts: contexts, @@ -108,6 +108,12 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { ); } + SentryApp _getApp(SentryApp? app) { + return (app ?? SentryApp()).copyWith( + appMemory: ProcessInfo.currentRss, + ); + } + SentryOperatingSystem _getOperatingSystem(SentryOperatingSystem? os) { return (os ?? SentryOperatingSystem()).copyWith( name: os?.name ?? Platform.operatingSystem, @@ -121,34 +127,4 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { timezone: culture?.timezone ?? DateTime.now().timeZoneName, ); } - - // Reference: - // https://github.com/erdbeerschnitzel/filesize.dart/blob/4f7c54dc06647b8368078f6febb83149494698c1/lib/filesize.dart - String _bytesToHumanReadableFileSize(num size) { - const List 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); - } - - return "$result ${affixes[affix]}"; - } } From 37ea59ca7e9689dceb1ef81a4ac9fab04e8a6d12 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 11:44:58 +0200 Subject: [PATCH 06/18] read memoryInfo.usedJSHeapSize for html app_memory --- .../enricher/html_enricher_event_processor.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart index e51cff4b71..7df6f0b57a 100644 --- a/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart @@ -26,6 +26,7 @@ class WebEnricherEventProcessor implements EnricherEventProcessor { final contexts = event.contexts.copyWith( device: _getDevice(event.contexts.device), + app: _getApp(event.contexts.app), culture: _getSentryCulture(event.contexts.culture), ); @@ -54,6 +55,16 @@ class WebEnricherEventProcessor implements EnricherEventProcessor { .sanitized(); } + SentryApp? _getApp(SentryApp? app) { + final memoryInfo = html.window.performance.memory; + if (memoryInfo == null) { + return app; + } + return (app ?? SentryApp()).copyWith( + appMemory: memoryInfo.usedJSHeapSize, + ); + } + SentryDevice _getDevice(SentryDevice? device) { return (device ?? SentryDevice()).copyWith( online: device?.online ?? _window.navigator.onLine, From d10dbbe5704cca1480354eb3135dfc6d70294e0c Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 11:47:39 +0200 Subject: [PATCH 07/18] remove memoty info enrichment on web --- .../enricher/html_enricher_event_processor.dart | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart index 7df6f0b57a..e51cff4b71 100644 --- a/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/html_enricher_event_processor.dart @@ -26,7 +26,6 @@ class WebEnricherEventProcessor implements EnricherEventProcessor { final contexts = event.contexts.copyWith( device: _getDevice(event.contexts.device), - app: _getApp(event.contexts.app), culture: _getSentryCulture(event.contexts.culture), ); @@ -55,16 +54,6 @@ class WebEnricherEventProcessor implements EnricherEventProcessor { .sanitized(); } - SentryApp? _getApp(SentryApp? app) { - final memoryInfo = html.window.performance.memory; - if (memoryInfo == null) { - return app; - } - return (app ?? SentryApp()).copyWith( - appMemory: memoryInfo.usedJSHeapSize, - ); - } - SentryDevice _getDevice(SentryDevice? device) { return (device ?? SentryDevice()).copyWith( online: device?.online ?? _window.navigator.onLine, From 6088536bf80f4f30acba51cb2e751f7ad5a5c5d7 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 11:51:31 +0200 Subject: [PATCH 08/18] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de57069ede..6106ba0477 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - 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)) + - Only for Linux/Windows applications, as iOS/Android use native SDKs + - No web support, more info: https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory#usedjsheapsize ### Deprecated From 89657ee44307e2469b0b2b3a48fba84929a4cd2b Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 11:56:14 +0200 Subject: [PATCH 09/18] set usable memory --- .../event_processor/enricher/io_enricher_event_processor.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index 79da3f0833..82f7d570ff 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -105,6 +105,7 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { return (device ?? SentryDevice()).copyWith( name: device?.name ?? Platform.localHostname, processorCount: device?.processorCount ?? Platform.numberOfProcessors, + usableMemory: ProcessInfo.maxRss, ); } From ba8a048820da45c086144e535b20d49fc53d7cf8 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 15:41:27 +0200 Subject: [PATCH 10/18] read total & free memory on linux/windows --- .../enricher/io_enricher_event_processor.dart | 5 +- .../enricher/io_platform_memory.dart | 107 ++++++++++++++++++ .../enricher/io_platform_memory_test.dart | 54 +++++++++ 3 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 dart/lib/src/event_processor/enricher/io_platform_memory.dart create mode 100644 dart/test/event_processor/enricher/io_platform_memory_test.dart diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index 82f7d570ff..ea47a038e2 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -2,6 +2,7 @@ import 'dart:io'; import '../../../sentry.dart'; import 'enricher_event_processor.dart'; +import 'io_platform_memory.dart'; EnricherEventProcessor enricherEventProcessor(SentryOptions options) { return IoEnricherEventProcessor(options); @@ -102,10 +103,12 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { } SentryDevice _getDevice(SentryDevice? device) { + final platformMemory = PlatformMemory(Platform.operatingSystem); return (device ?? SentryDevice()).copyWith( name: device?.name ?? Platform.localHostname, processorCount: device?.processorCount ?? Platform.numberOfProcessors, - usableMemory: ProcessInfo.maxRss, + memorySize: platformMemory.getTotalPhysicalMemory() ?? device?.memorySize, + freeMemory: platformMemory.getFreePhysicalMemory() ?? device?.freeMemory, ); } diff --git a/dart/lib/src/event_processor/enricher/io_platform_memory.dart b/dart/lib/src/event_processor/enricher/io_platform_memory.dart new file mode 100644 index 0000000000..3df187ba49 --- /dev/null +++ b/dart/lib/src/event_processor/enricher/io_platform_memory.dart @@ -0,0 +1,107 @@ +import 'dart:io'; + +// Get total & free platform memory (in bytes) for linux and windows operating systems. +// Source: https://github.com/onepub-dev/system_info/blob/8a9bf6b8eb7c86a09b3c3df4bf6d7fa5a6b50732/lib/src/platform/memory.dart +class PlatformMemory { + PlatformMemory(this.operatingSystem); + + final String operatingSystem; + + int? getTotalPhysicalMemory() { + switch (operatingSystem) { + case 'linux': + return _getLinuxMemInfoValue('MemTotal'); + case 'windows': + return _getWindowsWmicValue('TotalPhysicalMemory'); + default: + return null; + } + } + + int? getFreePhysicalMemory() { + switch (operatingSystem) { + case 'linux': + return _getLinuxMemInfoValue('MemFree'); + case 'windows': + return _getWindowsWmicValue('FreePhysicalMemory'); + default: + return null; + } + } + + int? _getWindowsWmicValue(String key) { + final os = _wmicGetValueAsMap('OS', [key]); + final totalPhysicalMemoryValue = os?[key]; + if (totalPhysicalMemoryValue == null) { + return null; + } + final size = int.tryParse(totalPhysicalMemoryValue); + if (size == null) { + return null; + } + return size; + } + + int? _getLinuxMemInfoValue(String key) { + final meminfoList = _exec('cat', ['/proc/meminfo']) + ?.trim() + .replaceAll('\r\n', '\n') + .split('\n') ?? + []; + + final meminfoMap = _listToMap(meminfoList, ':'); + final memsizeResults = meminfoMap[key]?.split(' ') ?? []; + + if (memsizeResults.isEmpty) { + return null; + } + final memsizeResult = memsizeResults.first; + + final memsize = int.tryParse(memsizeResult); + if (memsize == null) { + return null; + } + return memsize; + } + + String? _exec(String executable, List arguments, + {bool runInShell = false}) { + try { + final result = + Process.runSync(executable, arguments, runInShell: runInShell); + if (result.exitCode == 0) { + return result.stdout.toString(); + } + } catch (e) { + // + } + return null; + } + + Map? _wmicGetValueAsMap(String section, List fields) { + final arguments = [section]; + arguments + ..add('get') + ..addAll(fields.join(', ').split(' ')) + ..add('/VALUE'); + + final list = + _exec('wmic', arguments)?.trim().replaceAll('\r\n', '\n').split('\n') ?? + []; + + return _listToMap(list, '='); + } + + Map _listToMap(List list, String separator) { + final map = {}; + for (final string in list) { + final index = string.indexOf(separator); + if (index != -1) { + final key = string.substring(0, index).trim(); + final value = string.substring(index + 1).trim(); + map[key] = value; + } + } + return map; + } +} diff --git a/dart/test/event_processor/enricher/io_platform_memory_test.dart b/dart/test/event_processor/enricher/io_platform_memory_test.dart new file mode 100644 index 0000000000..b7f6e53618 --- /dev/null +++ b/dart/test/event_processor/enricher/io_platform_memory_test.dart @@ -0,0 +1,54 @@ +import 'dart:io'; + +import 'package:sentry/src/event_processor/enricher/io_platform_memory.dart'; +import 'package:test/test.dart'; + +void main() { + late Fixture fixture; + + setUp(() { + fixture = Fixture(); + }); + + test('total physical memory', () { + final sut = fixture.getSut(); + final totalPhysicalMemory = sut.getTotalPhysicalMemory(); + + switch (Platform.operatingSystem) { + case 'linux': + expect(totalPhysicalMemory, isNotNull); + expect(totalPhysicalMemory! > 0, true); + break; + case 'windows': + expect(totalPhysicalMemory, isNotNull); + expect(totalPhysicalMemory! > 0, true); + break; + default: + expect(totalPhysicalMemory, isNull); + } + }); + + test('free physical memory', () { + final sut = fixture.getSut(); + final freePhysicalMemory = sut.getTotalPhysicalMemory(); + + switch (Platform.operatingSystem) { + case 'linux': + expect(freePhysicalMemory, isNotNull); + expect(freePhysicalMemory! > 0, true); + break; + case 'windows': + expect(freePhysicalMemory, isNotNull); + expect(freePhysicalMemory! > 0, true); + break; + default: + expect(freePhysicalMemory, isNull); + } + }); +} + +class Fixture { + PlatformMemory getSut() { + return PlatformMemory(Platform.operatingSystem); + } +} From e8d88802f430b58beb1a2a7e26ff04fbd429582b Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 15:52:09 +0200 Subject: [PATCH 11/18] only run on vm --- .../test/event_processor/enricher/io_platform_memory_test.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dart/test/event_processor/enricher/io_platform_memory_test.dart b/dart/test/event_processor/enricher/io_platform_memory_test.dart index b7f6e53618..1c5d7c9f54 100644 --- a/dart/test/event_processor/enricher/io_platform_memory_test.dart +++ b/dart/test/event_processor/enricher/io_platform_memory_test.dart @@ -1,3 +1,6 @@ +@TestOn('vm') +library dart_test; + import 'dart:io'; import 'package:sentry/src/event_processor/enricher/io_platform_memory.dart'; From fcbcade25e4eab758022d227579186fdf8041e89 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 1 Jul 2024 16:30:37 +0200 Subject: [PATCH 12/18] use correct section for windows commands --- .../src/event_processor/enricher/io_platform_memory.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_platform_memory.dart b/dart/lib/src/event_processor/enricher/io_platform_memory.dart index 3df187ba49..a74e5be41c 100644 --- a/dart/lib/src/event_processor/enricher/io_platform_memory.dart +++ b/dart/lib/src/event_processor/enricher/io_platform_memory.dart @@ -12,7 +12,7 @@ class PlatformMemory { case 'linux': return _getLinuxMemInfoValue('MemTotal'); case 'windows': - return _getWindowsWmicValue('TotalPhysicalMemory'); + return _getWindowsWmicValue('ComputerSystem', 'TotalPhysicalMemory'); default: return null; } @@ -23,14 +23,14 @@ class PlatformMemory { case 'linux': return _getLinuxMemInfoValue('MemFree'); case 'windows': - return _getWindowsWmicValue('FreePhysicalMemory'); + return _getWindowsWmicValue('OS', 'FreePhysicalMemory'); default: return null; } } - int? _getWindowsWmicValue(String key) { - final os = _wmicGetValueAsMap('OS', [key]); + int? _getWindowsWmicValue(String section, String key) { + final os = _wmicGetValueAsMap(section, [key]); final totalPhysicalMemoryValue = os?[key]; if (totalPhysicalMemoryValue == null) { return null; From eae3175b07115b3942c8be52a82819b4d4b1a73e Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 2 Jul 2024 09:40:44 +0200 Subject: [PATCH 13/18] update changelog --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6106ba0477..b671559e47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,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)) - - Only for Linux/Windows applications, as iOS/Android use native SDKs - - No web support, more info: https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory#usedjsheapsize + - Only for Linux/Windows applications, as iOS/Android/macOS use native SDKs ### Deprecated From e2d07f0ced19b27cacb766abe8b19307337047c0 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 2 Jul 2024 10:27:03 +0200 Subject: [PATCH 14/18] log error --- .../enricher/io_enricher_event_processor.dart | 2 +- .../src/event_processor/enricher/io_platform_memory.dart | 8 ++++++-- .../event_processor/enricher/io_platform_memory_test.dart | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index ea47a038e2..b0d92e59a4 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -103,7 +103,7 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { } SentryDevice _getDevice(SentryDevice? device) { - final platformMemory = PlatformMemory(Platform.operatingSystem); + final platformMemory = PlatformMemory(Platform.operatingSystem, _options); return (device ?? SentryDevice()).copyWith( name: device?.name ?? Platform.localHostname, processorCount: device?.processorCount ?? Platform.numberOfProcessors, diff --git a/dart/lib/src/event_processor/enricher/io_platform_memory.dart b/dart/lib/src/event_processor/enricher/io_platform_memory.dart index a74e5be41c..f2fe1a0519 100644 --- a/dart/lib/src/event_processor/enricher/io_platform_memory.dart +++ b/dart/lib/src/event_processor/enricher/io_platform_memory.dart @@ -1,11 +1,15 @@ import 'dart:io'; +import '../../protocol.dart'; +import '../../sentry_options.dart'; + // Get total & free platform memory (in bytes) for linux and windows operating systems. // Source: https://github.com/onepub-dev/system_info/blob/8a9bf6b8eb7c86a09b3c3df4bf6d7fa5a6b50732/lib/src/platform/memory.dart class PlatformMemory { - PlatformMemory(this.operatingSystem); + PlatformMemory(this.operatingSystem, this.options); final String operatingSystem; + final SentryOptions options; int? getTotalPhysicalMemory() { switch (operatingSystem) { @@ -73,7 +77,7 @@ class PlatformMemory { return result.stdout.toString(); } } catch (e) { - // + options.logger(SentryLevel.warning, "Failed to run process: $e"); } return null; } diff --git a/dart/test/event_processor/enricher/io_platform_memory_test.dart b/dart/test/event_processor/enricher/io_platform_memory_test.dart index 1c5d7c9f54..4c2d26530e 100644 --- a/dart/test/event_processor/enricher/io_platform_memory_test.dart +++ b/dart/test/event_processor/enricher/io_platform_memory_test.dart @@ -3,6 +3,7 @@ library dart_test; import 'dart:io'; +import 'package:sentry/sentry.dart'; import 'package:sentry/src/event_processor/enricher/io_platform_memory.dart'; import 'package:test/test.dart'; @@ -51,7 +52,9 @@ void main() { } class Fixture { + var options = SentryOptions(); + PlatformMemory getSut() { - return PlatformMemory(Platform.operatingSystem); + return PlatformMemory(Platform.operatingSystem, options); } } From 1ab1f7e23301037d8eae2e0a806f9e6c4074fadd Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 2 Jul 2024 11:01:44 +0200 Subject: [PATCH 15/18] use platform checker --- .../enricher/io_enricher_event_processor.dart | 6 ++-- .../enricher/io_platform_memory.dart | 29 +++++++++---------- .../enricher/io_platform_memory_test.dart | 2 +- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index b0d92e59a4..e87a8ab8b1 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -103,12 +103,12 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { } SentryDevice _getDevice(SentryDevice? device) { - final platformMemory = PlatformMemory(Platform.operatingSystem, _options); + final platformMemory = PlatformMemory(_options); return (device ?? SentryDevice()).copyWith( name: device?.name ?? Platform.localHostname, processorCount: device?.processorCount ?? Platform.numberOfProcessors, - memorySize: platformMemory.getTotalPhysicalMemory() ?? device?.memorySize, - freeMemory: platformMemory.getFreePhysicalMemory() ?? device?.freeMemory, + memorySize: device?.memorySize ?? platformMemory.getTotalPhysicalMemory(), + freeMemory: device?.freeMemory ?? platformMemory.getFreePhysicalMemory(), ); } diff --git a/dart/lib/src/event_processor/enricher/io_platform_memory.dart b/dart/lib/src/event_processor/enricher/io_platform_memory.dart index f2fe1a0519..4acde3f15b 100644 --- a/dart/lib/src/event_processor/enricher/io_platform_memory.dart +++ b/dart/lib/src/event_processor/enricher/io_platform_memory.dart @@ -6,30 +6,27 @@ import '../../sentry_options.dart'; // Get total & free platform memory (in bytes) for linux and windows operating systems. // Source: https://github.com/onepub-dev/system_info/blob/8a9bf6b8eb7c86a09b3c3df4bf6d7fa5a6b50732/lib/src/platform/memory.dart class PlatformMemory { - PlatformMemory(this.operatingSystem, this.options); + PlatformMemory(this.options); - final String operatingSystem; final SentryOptions options; int? getTotalPhysicalMemory() { - switch (operatingSystem) { - case 'linux': - return _getLinuxMemInfoValue('MemTotal'); - case 'windows': - return _getWindowsWmicValue('ComputerSystem', 'TotalPhysicalMemory'); - default: - return null; + if (options.platformChecker.platform.isLinux) { + return _getLinuxMemInfoValue('MemTotal'); + } else if (options.platformChecker.platform.isWindows) { + return _getWindowsWmicValue('ComputerSystem', 'TotalPhysicalMemory'); + } else { + return null; } } int? getFreePhysicalMemory() { - switch (operatingSystem) { - case 'linux': - return _getLinuxMemInfoValue('MemFree'); - case 'windows': - return _getWindowsWmicValue('OS', 'FreePhysicalMemory'); - default: - return null; + if (options.platformChecker.platform.isLinux) { + return _getLinuxMemInfoValue('MemFree'); + } else if (options.platformChecker.platform.isWindows) { + return _getWindowsWmicValue('OS', 'FreePhysicalMemory'); + } else { + return null; } } diff --git a/dart/test/event_processor/enricher/io_platform_memory_test.dart b/dart/test/event_processor/enricher/io_platform_memory_test.dart index 4c2d26530e..0f987b935c 100644 --- a/dart/test/event_processor/enricher/io_platform_memory_test.dart +++ b/dart/test/event_processor/enricher/io_platform_memory_test.dart @@ -55,6 +55,6 @@ class Fixture { var options = SentryOptions(); PlatformMemory getSut() { - return PlatformMemory(Platform.operatingSystem, options); + return PlatformMemory(options); } } From f880f63453b97eef91ecaf49c5dd65989af4ffe1 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 2 Jul 2024 11:24:08 +0200 Subject: [PATCH 16/18] always load appMemory --- .../enricher/io_enricher_event_processor.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index e87a8ab8b1..0d0a3b11f4 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -18,6 +18,10 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { @override SentryEvent? apply(SentryEvent event, Hint hint) { + + // Amend app with current memory usage, as this is not available on native. + final app = _getApp(event.contexts.app); + // If there's a native integration available, it probably has better // information available than Flutter. @@ -29,10 +33,6 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { ? null : _getOperatingSystem(event.contexts.operatingSystem); - final app = _options.platformChecker.hasNativeIntegration - ? null - : _getApp(event.contexts.app); - final culture = _options.platformChecker.hasNativeIntegration ? null : _getSentryCulture(event.contexts.culture); @@ -114,7 +114,7 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { SentryApp _getApp(SentryApp? app) { return (app ?? SentryApp()).copyWith( - appMemory: ProcessInfo.currentRss, + appMemory: app?.appMemory ?? ProcessInfo.currentRss, ); } From ce54ed1d69c02f8ff8181619b9edb3dd17ea3237 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 2 Jul 2024 11:30:32 +0200 Subject: [PATCH 17/18] format --- .../event_processor/enricher/io_enricher_event_processor.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart index 0d0a3b11f4..63304cdbf6 100644 --- a/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart +++ b/dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart @@ -18,7 +18,6 @@ class IoEnricherEventProcessor implements EnricherEventProcessor { @override SentryEvent? apply(SentryEvent event, Hint hint) { - // Amend app with current memory usage, as this is not available on native. final app = _getApp(event.contexts.app); From c1d87f6fef82c0175d9045c4acf4190c79f57587 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 8 Jul 2024 16:19:21 +0200 Subject: [PATCH 18/18] fix changelog --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5656ffeb38..9d024ca890 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +### Features + +- Add memory usage to contexts ([#2133](https://github.com/getsentry/sentry-dart/pull/2133)) + - Only for Linux/Windows applications, as iOS/Android/macOS use native SDKs + ### Fixes - App starts hanging for 30s ([#2140](https://github.com/getsentry/sentry-dart/pull/2140)) @@ -17,8 +22,6 @@ - Use `SentryFlutter.pauseAppHangTracking()` and `SentryFlutter.resumeAppHangTracking()` - 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)) - - Only for Linux/Windows applications, as iOS/Android/macOS use native SDKs ### Deprecated