Skip to content

Commit

Permalink
Set OS context details on Android (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Jun 15, 2022
1 parent a2db32b commit c108ddd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/Sentry/Android/AndroidEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/Sentry/Android/Extensions/OperatingSystemExtensions.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit c108ddd

Please sign in to comment.