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

Align device types with Cocoa and Java SDKs #3567

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -7,6 +7,7 @@
- You should no longer pass `AndroidContext` as an argument to `SentrySdk.Init` ([#3562](https://github.com/getsentry/sentry-dotnet/pull/3562))
- The `SentryUser.Segment` property has been deprecated. Consider sending this as a tag or additional data instead ([#3563](https://github.com/getsentry/sentry-dotnet/pull/3563))
- The ITraceContext now includes an [Origin](https://develop.sentry.dev/sdk/telemetry/traces/trace-origin/), which is set automatically and is primarily used internally by the Sentry server ([#3564](https://github.com/getsentry/sentry-dotnet/pull/3564))
- `Device.BatteryLevel` and `Device.ProcessorFrequency` are now stored as floats rather than ints, to align with the Cocoa and Java SDKs ([#3567](https://github.com/getsentry/sentry-dotnet/pull/3567))

## 4.10.2

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.Maui/Internal/MauiDeviceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void ApplyMauiDeviceData(this Device device, IDiagnosticLogger? lo
try
{
var battery = Battery.Default;
device.BatteryLevel ??= battery.ChargeLevel < 0 ? null : (short)(battery.ChargeLevel * 100.0);
device.BatteryLevel ??= battery.ChargeLevel < 0 ? null : (float)(battery.ChargeLevel * 100.0);
device.BatteryStatus ??= battery.State.ToString();
device.IsCharging ??= battery.State switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void ApplyFromSentryAndroidSdk(this Device device, JavaSdk.Protoco
device.Name ??= d.Name;
device.Family ??= d.Family;
device.ModelId ??= d.ModelId;
device.BatteryLevel ??= d.BatteryLevel?.ShortValue();
device.BatteryLevel ??= d.BatteryLevel?.FloatValue();
device.IsCharging ??= d.IsCharging()?.BooleanValue();
device.IsOnline ??= d.IsOnline()?.BooleanValue();
device.Orientation ??= d.Orientation?.ToDeviceOrientation();
Expand Down
25 changes: 7 additions & 18 deletions src/Sentry/Protocol/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public sealed class Device : ISentryJsonSerializable, ICloneable<Device>, IUpdat
public string? Architecture { get; set; }

/// <summary>
/// If the device has a battery an integer defining the battery level (in the range 0-100).
/// If the device has a battery a number defining the battery level (in the range 0-100).
/// </summary>
public short? BatteryLevel { get; set; }
public float? BatteryLevel { get; set; }

/// <summary>
/// True if the device is charging.
Expand Down Expand Up @@ -184,7 +184,7 @@ public sealed class Device : ISentryJsonSerializable, ICloneable<Device>, IUpdat
/// <example>
/// 2500
/// </example>
public int? ProcessorFrequency { get; set; }
public float? ProcessorFrequency { get; set; }

/// <summary>
/// Kind of device the application is running on.
Expand Down Expand Up @@ -426,15 +426,9 @@ public static Device FromJson(JsonElement json)
var model = json.GetPropertyOrNull("model")?.GetString();
var modelId = json.GetPropertyOrNull("model_id")?.GetString();
var architecture = json.GetPropertyOrNull("arch")?.GetString();

// TODO: For next major: Remove this and change BatteryLevel from short to float
// The Java and Cocoa SDK report the battery as `float`
// Cocoa https://github.com/getsentry/sentry-cocoa/blob/e773cad622b86735f1673368414009475e4119fd/Sources/Sentry/include/SentryUIDeviceWrapper.h#L18
// Java https://github.com/getsentry/sentry-java/blob/25f1ca4e1636a801c17c1662f0145f888550bce8/sentry/src/main/java/io/sentry/protocol/Device.java#L231-L233
var batteryLevel = json.GetPropertyOrNull("battery_level")?.TryGetDouble(out var level) is true
? (short)level
: (short?)null;

? (float)level
: (float?)null;
var isCharging = json.GetPropertyOrNull("charging")?.GetBoolean();
var isOnline = json.GetPropertyOrNull("online")?.GetBoolean();
var orientation = json.GetPropertyOrNull("orientation")?.GetString()?.ParseEnum<DeviceOrientation>();
Expand All @@ -453,14 +447,9 @@ public static Device FromJson(JsonElement json)
var bootTime = json.GetPropertyOrNull("boot_time")?.GetDateTimeOffset();
var processorCount = json.GetPropertyOrNull("processor_count")?.GetInt32();
var cpuDescription = json.GetPropertyOrNull("cpu_description")?.GetString();

// TODO: For next major: Remove this and change ProcessorFrequency from int to float
// The Java SDK reports the processorFrequency as `double`
// Java https://github.com/getsentry/sentry-java/blob/9762f09afa51944b40a9b77e116a55e54636e6c5/sentry/src/main/java/io/sentry/protocol/Device.java#L130
var processorFrequency = json.GetPropertyOrNull("processor_frequency")?.TryGetDouble(out var frequency) is true
? (int)frequency
: (int?)null;

? (float)frequency
: (float?)null;
var deviceType = json.GetPropertyOrNull("device_type")?.GetString();
var batteryStatus = json.GetPropertyOrNull("battery_status")?.GetString();
var deviceUniqueIdentifier = json.GetPropertyOrNull("device_unique_identifier")?.GetString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ namespace Sentry.Protocol
public const string Type = "device";
public Device() { }
public string? Architecture { get; set; }
public short? BatteryLevel { get; set; }
public float? BatteryLevel { get; set; }
public string? BatteryStatus { get; set; }
public System.DateTimeOffset? BootTime { get; set; }
public string? Brand { get; set; }
Expand All @@ -1610,7 +1610,7 @@ namespace Sentry.Protocol
public string? Name { get; set; }
public Sentry.Protocol.DeviceOrientation? Orientation { get; set; }
public int? ProcessorCount { get; set; }
public int? ProcessorFrequency { get; set; }
public float? ProcessorFrequency { get; set; }
public float? ScreenDensity { get; set; }
public int? ScreenDpi { get; set; }
public string? ScreenResolution { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ namespace Sentry.Protocol
public const string Type = "device";
public Device() { }
public string? Architecture { get; set; }
public short? BatteryLevel { get; set; }
public float? BatteryLevel { get; set; }
public string? BatteryStatus { get; set; }
public System.DateTimeOffset? BootTime { get; set; }
public string? Brand { get; set; }
Expand All @@ -1610,7 +1610,7 @@ namespace Sentry.Protocol
public string? Name { get; set; }
public Sentry.Protocol.DeviceOrientation? Orientation { get; set; }
public int? ProcessorCount { get; set; }
public int? ProcessorFrequency { get; set; }
public float? ProcessorFrequency { get; set; }
public float? ScreenDensity { get; set; }
public int? ScreenDpi { get; set; }
public string? ScreenResolution { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ namespace Sentry.Protocol
public const string Type = "device";
public Device() { }
public string? Architecture { get; set; }
public short? BatteryLevel { get; set; }
public float? BatteryLevel { get; set; }
public string? BatteryStatus { get; set; }
public System.DateTimeOffset? BootTime { get; set; }
public string? Brand { get; set; }
Expand All @@ -1612,7 +1612,7 @@ namespace Sentry.Protocol
public string? Name { get; set; }
public Sentry.Protocol.DeviceOrientation? Orientation { get; set; }
public int? ProcessorCount { get; set; }
public int? ProcessorFrequency { get; set; }
public float? ProcessorFrequency { get; set; }
public float? ScreenDensity { get; set; }
public int? ScreenDpi { get; set; }
public string? ScreenResolution { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ namespace Sentry.Protocol
public const string Type = "device";
public Device() { }
public string? Architecture { get; set; }
public short? BatteryLevel { get; set; }
public float? BatteryLevel { get; set; }
public string? BatteryStatus { get; set; }
public System.DateTimeOffset? BootTime { get; set; }
public string? Brand { get; set; }
Expand All @@ -1608,7 +1608,7 @@ namespace Sentry.Protocol
public string? Name { get; set; }
public Sentry.Protocol.DeviceOrientation? Orientation { get; set; }
public int? ProcessorCount { get; set; }
public int? ProcessorFrequency { get; set; }
public float? ProcessorFrequency { get; set; }
public float? ScreenDensity { get; set; }
public int? ScreenDpi { get; set; }
public string? ScreenResolution { get; set; }
Expand Down
Loading
Loading