Skip to content

Commit

Permalink
feat: improve device info parsing on dart & non-mobile Flutter (#2441)
Browse files Browse the repository at this point in the history
* feat: improve device info parsing on dart & non-mobile Flutter

* chore: update changelog

* fix tests on Linux
  • Loading branch information
vaind authored Nov 22, 2024
1 parent 97c29ca commit 25fc225
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 143 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Linux native error & obfuscation support ([#2431](https://github.com/getsentry/sentry-dart/pull/2431))
- Improve Device context on plain Dart and Flutter desktop apps ([#2441](https://github.com/getsentry/sentry-dart/pull/2441))

## 8.11.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:io';

import 'package:meta/meta.dart';

import '../../../sentry.dart';
import 'enricher_event_processor.dart';
import 'io_platform_memory.dart';
Expand All @@ -16,6 +18,8 @@ class IoEnricherEventProcessor implements EnricherEventProcessor {

final SentryOptions _options;
late final String _dartVersion = _extractDartVersion(Platform.version);
late final SentryOperatingSystem _os = extractOperatingSystem(
Platform.operatingSystem, Platform.operatingSystemVersion);

/// Extracts the semantic version and channel from the full version string.
///
Expand Down Expand Up @@ -137,9 +141,46 @@ class IoEnricherEventProcessor implements EnricherEventProcessor {
}

SentryOperatingSystem _getOperatingSystem(SentryOperatingSystem? os) {
return (os ?? SentryOperatingSystem()).copyWith(
name: os?.name ?? Platform.operatingSystem,
version: os?.version ?? Platform.operatingSystemVersion,
if (os == null) {
return _os.clone();
} else {
return _os.mergeWith(os);
}
}

@internal
SentryOperatingSystem extractOperatingSystem(
String name, String rawDescription) {
RegExpMatch? match;
switch (name) {
case 'android':
match = _androidOsRegexp.firstMatch(rawDescription);
name = 'Android';
break;
case 'ios':
name = 'iOS';
match = _appleOsRegexp.firstMatch(rawDescription);
break;
case 'macos':
name = 'macOS';
match = _appleOsRegexp.firstMatch(rawDescription);
break;
case 'linux':
name = 'Linux';
match = _linuxOsRegexp.firstMatch(rawDescription);
break;
case 'windows':
name = 'Windows';
match = _windowsOsRegexp.firstMatch(rawDescription);
break;
}

return SentryOperatingSystem(
name: name,
rawDescription: rawDescription,
version: match?.namedGroupOrNull('version'),
build: match?.namedGroupOrNull('build'),
kernelVersion: match?.namedGroupOrNull('kernelVersion'),
);
}

Expand All @@ -150,3 +191,32 @@ class IoEnricherEventProcessor implements EnricherEventProcessor {
);
}
}

// LYA-L29 10.1.0.289(C432E7R1P5)
// TE1A.220922.010
final _androidOsRegexp = RegExp('^(?<build>.*)\$', caseSensitive: false);

// Linux 5.11.0-1018-gcp #20~20.04.2-Ubuntu SMP Fri Sep 3 01:01:37 UTC 2021
final _linuxOsRegexp = RegExp(
'(?<kernelVersion>[a-z0-9+.\\-]+) (?<build>#.*)\$',
caseSensitive: false);

// Version 14.5 (Build 18E182)
final _appleOsRegexp = RegExp(
'(?<version>[a-z0-9+.\\-]+)( \\(Build (?<build>[a-z0-9+.\\-]+))\\)?\$',
caseSensitive: false);

// "Windows 10 Pro" 10.0 (Build 19043)
final _windowsOsRegexp = RegExp(
' (?<version>[a-z0-9+.\\-]+)( \\(Build (?<build>[a-z0-9+.\\-]+))\\)?\$',
caseSensitive: false);

extension on RegExpMatch {
String? namedGroupOrNull(String name) {
if (groupNames.contains(name)) {
return namedGroup(name);
} else {
return null;
}
}
}
16 changes: 16 additions & 0 deletions dart/lib/src/protocol/sentry_operating_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,20 @@ class SentryOperatingSystem {
theme: theme ?? this.theme,
unknown: unknown,
);

SentryOperatingSystem mergeWith(SentryOperatingSystem other) =>
SentryOperatingSystem(
name: other.name ?? name,
version: other.version ?? version,
build: other.build ?? build,
kernelVersion: other.kernelVersion ?? kernelVersion,
rooted: other.rooted ?? rooted,
rawDescription: other.rawDescription ?? rawDescription,
theme: other.theme ?? theme,
unknown: other.unknown == null
? unknown
: unknown == null
? null
: {...unknown!, ...other.unknown!},
);
}
Loading

0 comments on commit 25fc225

Please sign in to comment.