diff --git a/CHANGELOG.md b/CHANGELOG.md index a52995b6a7..8892e33927 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Update Sentry-Android to 6.0.0 ([#1697](https://github.com/getsentry/sentry-dotnet/pull/1697)) - Set Java/Android SDK options ([#1694](https://github.com/getsentry/sentry-dotnet/pull/1694)) - Refactor and update Android options ([#1705](https://github.com/getsentry/sentry-dotnet/pull/1705)) + - Add Android OS information to the event context ([#1716](https://github.com/getsentry/sentry-dotnet/pull/1716)) ### Fixes diff --git a/src/Sentry/Android/AndroidEventProcessor.cs b/src/Sentry/Android/AndroidEventProcessor.cs index c5c17b9deb..45de123b0a 100644 --- a/src/Sentry/Android/AndroidEventProcessor.cs +++ b/src/Sentry/Android/AndroidEventProcessor.cs @@ -27,11 +27,12 @@ public SentryEvent Process(SentryEvent @event) { // Get what information we can ourselves first @event.Contexts.Device.ApplyFromAndroidRuntime(); + @event.Contexts.OperatingSystem.ApplyFromAndroidRuntime(); // Copy more information from the Android SDK if (_androidProcessor is { } androidProcessor) { - // TODO: Can we gather more device data directly and remove this? + // TODO: Can we gather more data directly and remove this? // Run a fake event through the Android processor, so we can get context info from the Android SDK. // We'll want to do this every time, so that all information is current. (ex: device orientation) @@ -43,6 +44,10 @@ public SentryEvent Process(SentryEvent @event) { @event.Contexts.Device.ApplyFromSentryAndroidSdk(device); } + if (e.Contexts.OperatingSystem is { } os) + { + @event.Contexts.OperatingSystem.ApplyFromSentryAndroidSdk(os); + } } return @event; diff --git a/src/Sentry/Android/Extensions/OperatingSystemExtensions.cs b/src/Sentry/Android/Extensions/OperatingSystemExtensions.cs new file mode 100644 index 0000000000..0745eacbc8 --- /dev/null +++ b/src/Sentry/Android/Extensions/OperatingSystemExtensions.cs @@ -0,0 +1,36 @@ +using System.Runtime.InteropServices; +using OperatingSystem = Sentry.Protocol.OperatingSystem; + +namespace Sentry.Android.Extensions; + +internal static class OperatingSystemExtensions +{ + public static void ApplyFromAndroidRuntime(this OperatingSystem operatingSystem) + { + operatingSystem.Name ??= "Android"; + operatingSystem.Version ??= AndroidBuild.VERSION.Release; + operatingSystem.Build ??= AndroidBuild.Display; + + if (operatingSystem.KernelVersion == null) + { + // ex: "Linux 5.10.98-android13-0-00003-g6ea688a79989-ab8162051 #1 SMP PREEMPT Tue Feb 8 00:20:26 UTC 2022" + var osParts = RuntimeInformation.OSDescription.Split(' '); + if (osParts.Length >= 2 && osParts[1].Contains("android", StringComparison.OrdinalIgnoreCase)) + { + // ex: "5.10.98-android13-0-00003-g6ea688a79989-ab8162051" + operatingSystem.KernelVersion = osParts[1]; + } + } + + // operatingSystem.RawDescription is already set in Enricher.cs + } + + public static void ApplyFromSentryAndroidSdk(this OperatingSystem operatingSystem, Java.Protocol.OperatingSystem os) + { + // We already have everything above, except the Rooted flag. + // The Android SDK figures this out in RootChecker.java + // TODO: Consider porting the Java root checker to .NET. We probably have access to all the same information. + // https://github.com/getsentry/sentry-java/blob/main/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/RootChecker.java + operatingSystem.Rooted ??= os.IsRooted()?.BooleanValue(); + } +}