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

feat: Added Func<bool> IsMainThread to options #1803

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Features

- Added a hook `IsMainThread` to the options to allow overwriting of the logic to determine whether the currently active thread is the main thread ([#1803](https://github.com/getsentry/sentry-unity/pull/1803))
- Contexts, such as `device` and `gpu` that the SDK retrieves during the game's startup is now available even earlier and irrespective whether an error was captured on the main or on a background thread ([#1802](https://github.com/getsentry/sentry-unity/pull/1802))

### Dependencies

- Bump CLI from v2.34.1 to v2.36.1 ([#1788](https://github.com/getsentry/sentry-unity/pull/1788), [#1792](https://github.com/getsentry/sentry-unity/pull/1792), [#1796](https://github.com/getsentry/sentry-unity/pull/1796))
Expand Down
86 changes: 41 additions & 45 deletions src/Sentry.Unity/Integrations/UnityScopeIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,28 @@ internal static class UnitySdkInfo

internal class UnityScopeIntegration : ISdkIntegration
{
private readonly MainThreadData _mainThreadData;
private readonly IApplication _application;

public UnityScopeIntegration(SentryMonoBehaviour monoBehaviour, IApplication application)
public UnityScopeIntegration(IApplication application)
{
_mainThreadData = monoBehaviour.MainThreadData;
_application = application;
}

public void Register(IHub hub, SentryOptions options)
{
var scopeUpdater = new UnityScopeUpdater((SentryUnityOptions)options, _mainThreadData, _application);
var scopeUpdater = new UnityScopeUpdater((SentryUnityOptions)options, _application);
hub.ConfigureScope(scopeUpdater.ConfigureScope);
}
}

internal class UnityScopeUpdater
{
private readonly SentryUnityOptions _options;
private readonly MainThreadData _mainThreadData;
private readonly IApplication _application;

public UnityScopeUpdater(SentryUnityOptions options, MainThreadData mainThreadData, IApplication application)
public UnityScopeUpdater(SentryUnityOptions options, IApplication application)
{
_options = options;
_mainThreadData = mainThreadData;
_application = application;
}

Expand Down Expand Up @@ -70,57 +66,57 @@ private static void PopulateSdk(SdkVersion sdk)

private void PopulateApp(App app)
{
app.StartTime = _mainThreadData.StartTime;
var isDebugBuild = _mainThreadData.IsDebugBuild;
app.StartTime = MainThreadData.StartTime;
var isDebugBuild = MainThreadData.IsDebugBuild;
app.BuildType = isDebugBuild is null ? null : (isDebugBuild.Value ? "debug" : "release");
}

private void PopulateOperatingSystem(OperatingSystem operatingSystem)
{
operatingSystem.RawDescription = _mainThreadData.OperatingSystem;
operatingSystem.RawDescription = MainThreadData.OperatingSystem;
}

private void PopulateDevice(Device device)
{
device.ProcessorCount = _mainThreadData.ProcessorCount;
device.CpuDescription = _mainThreadData.CpuDescription;
device.ProcessorCount = MainThreadData.ProcessorCount;
device.CpuDescription = MainThreadData.CpuDescription;
device.Timezone = TimeZoneInfo.Local;
device.SupportsVibration = _mainThreadData.SupportsVibration;
device.Name = _mainThreadData.DeviceName;
device.SupportsVibration = MainThreadData.SupportsVibration;
device.Name = MainThreadData.DeviceName;

// The app can be run in an iOS or Android emulator. We can't safely set a value for simulator.
device.Simulator = _application.IsEditor ? true : null;
device.DeviceUniqueIdentifier = _options.SendDefaultPii
? _mainThreadData.DeviceUniqueIdentifier
? MainThreadData.DeviceUniqueIdentifier
: null;
device.DeviceType = _mainThreadData.DeviceType;
device.Model = _mainThreadData.DeviceModel;
device.DeviceType = MainThreadData.DeviceType;
device.Model = MainThreadData.DeviceModel;

// This is the approximate amount of system memory in megabytes.
// This function is not supported on Windows Store Apps and will always return 0.
if (_mainThreadData.SystemMemorySize > 0)
if (MainThreadData.SystemMemorySize > 0)
{
device.MemorySize = _mainThreadData.SystemMemorySize * 1048576L; // Sentry device mem is in Bytes
device.MemorySize = MainThreadData.SystemMemorySize * 1048576L; // Sentry device mem is in Bytes
}
}

private void PopulateGpu(Gpu gpu)
{
gpu.Id = _mainThreadData.GraphicsDeviceId;
gpu.Name = _mainThreadData.GraphicsDeviceName;
gpu.VendorName = _mainThreadData.GraphicsDeviceVendor;
gpu.MemorySize = _mainThreadData.GraphicsMemorySize;
gpu.NpotSupport = _mainThreadData.NpotSupport;
gpu.Version = _mainThreadData.GraphicsDeviceVersion;
gpu.ApiType = _mainThreadData.GraphicsDeviceType;
gpu.MaxTextureSize = _mainThreadData.MaxTextureSize;
gpu.SupportsDrawCallInstancing = _mainThreadData.SupportsDrawCallInstancing;
gpu.SupportsRayTracing = _mainThreadData.SupportsRayTracing;
gpu.SupportsComputeShaders = _mainThreadData.SupportsComputeShaders;
gpu.SupportsGeometryShaders = _mainThreadData.SupportsGeometryShaders;
gpu.VendorId = _mainThreadData.GraphicsDeviceVendorId;
gpu.MultiThreadedRendering = _mainThreadData.GraphicsMultiThreaded;
gpu.GraphicsShaderLevel = _mainThreadData.GraphicsShaderLevel switch
gpu.Id = MainThreadData.GraphicsDeviceId;
gpu.Name = MainThreadData.GraphicsDeviceName;
gpu.VendorName = MainThreadData.GraphicsDeviceVendor;
gpu.MemorySize = MainThreadData.GraphicsMemorySize;
gpu.NpotSupport = MainThreadData.NpotSupport;
gpu.Version = MainThreadData.GraphicsDeviceVersion;
gpu.ApiType = MainThreadData.GraphicsDeviceType;
gpu.MaxTextureSize = MainThreadData.MaxTextureSize;
gpu.SupportsDrawCallInstancing = MainThreadData.SupportsDrawCallInstancing;
gpu.SupportsRayTracing = MainThreadData.SupportsRayTracing;
gpu.SupportsComputeShaders = MainThreadData.SupportsComputeShaders;
gpu.SupportsGeometryShaders = MainThreadData.SupportsGeometryShaders;
gpu.VendorId = MainThreadData.GraphicsDeviceVendorId;
gpu.MultiThreadedRendering = MainThreadData.GraphicsMultiThreaded;
gpu.GraphicsShaderLevel = MainThreadData.GraphicsShaderLevel switch
{
null => null,
-1 => null,
Expand All @@ -132,38 +128,38 @@ private void PopulateGpu(Gpu gpu)
45 => "Metal / OpenGL ES 3.1",
46 => "OpenGL 4.1",
50 => "Shader Model 5.0",
_ => _mainThreadData.GraphicsShaderLevel.ToString()
_ => MainThreadData.GraphicsShaderLevel.ToString()
};
}

private void PopulateUnity(Protocol.Unity unity)
{
unity.EditorVersion = _mainThreadData.EditorVersion;
unity.InstallMode = _mainThreadData.InstallMode;
unity.TargetFrameRate = _mainThreadData.TargetFrameRate;
unity.CopyTextureSupport = _mainThreadData.CopyTextureSupport;
unity.RenderingThreadingMode = _mainThreadData.RenderingThreadingMode;
unity.EditorVersion = MainThreadData.EditorVersion;
unity.InstallMode = MainThreadData.InstallMode;
unity.TargetFrameRate = MainThreadData.TargetFrameRate;
unity.CopyTextureSupport = MainThreadData.CopyTextureSupport;
unity.RenderingThreadingMode = MainThreadData.RenderingThreadingMode;
}

private void PopulateTags(Action<string, string> setTag)
{
// TODO revisit which tags we should be adding by default
if (_mainThreadData.InstallMode is { } installMode)
if (MainThreadData.InstallMode is { } installMode)
{
setTag("unity.install_mode", installMode);
}

if (_mainThreadData.SupportsDrawCallInstancing.HasValue)
if (MainThreadData.SupportsDrawCallInstancing.HasValue)
{
setTag("unity.gpu.supports_instancing", _mainThreadData.SupportsDrawCallInstancing.Value.ToTagValue());
setTag("unity.gpu.supports_instancing", MainThreadData.SupportsDrawCallInstancing.Value.ToTagValue());
}

if (_mainThreadData.DeviceType is { } deviceType)
if (MainThreadData.DeviceType is { } deviceType)
{
setTag("unity.device.device_type", deviceType);
}

if (_options.SendDefaultPii && _mainThreadData.DeviceUniqueIdentifier is { } deviceUniqueIdentifier)
if (_options.SendDefaultPii && MainThreadData.DeviceUniqueIdentifier is { } deviceUniqueIdentifier)
{
setTag("unity.device.unique_identifier", deviceUniqueIdentifier);
}
Expand Down
108 changes: 74 additions & 34 deletions src/Sentry.Unity/MainThreadData.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,113 @@
using System;
using System.Threading;
using UnityEngine;

namespace Sentry.Unity;

internal sealed class MainThreadData
internal static class MainThreadData
{
internal int? MainThreadId { get; set; }
internal static int? MainThreadId { get; set; }

public string? OperatingSystem { get; set; }
public static string? OperatingSystem { get; set; }

public int? ProcessorCount { get; set; }
public static int? ProcessorCount { get; set; }

public bool? SupportsVibration { get; set; }
public static bool? SupportsVibration { get; set; }

public string? DeviceType { get; set; }
public static string? DeviceType { get; set; }

public string? CpuDescription { get; set; }
public static string? CpuDescription { get; set; }

public string? DeviceName { get; set; }
public static string? DeviceName { get; set; }

public string? DeviceUniqueIdentifier { get; set; }
public static string? DeviceUniqueIdentifier { get; set; }

public string? DeviceModel { get; set; }
public static string? DeviceModel { get; set; }

public int? SystemMemorySize { get; set; }
public static int? SystemMemorySize { get; set; }

public int? GraphicsDeviceId { get; set; }
public static int? GraphicsDeviceId { get; set; }

public string? GraphicsDeviceName { get; set; }
public static string? GraphicsDeviceName { get; set; }

public string? GraphicsDeviceVendorId { get; set; }
public static string? GraphicsDeviceVendorId { get; set; }

public string? GraphicsDeviceVendor { get; set; }
public static string? GraphicsDeviceVendor { get; set; }

public int? GraphicsMemorySize { get; set; }
public static int? GraphicsMemorySize { get; set; }

public bool? GraphicsMultiThreaded { get; set; }
public static bool? GraphicsMultiThreaded { get; set; }

public string? NpotSupport { get; set; }
public static string? NpotSupport { get; set; }

public string? GraphicsDeviceVersion { get; set; }
public static string? GraphicsDeviceVersion { get; set; }

public string? GraphicsDeviceType { get; set; }
public static string? GraphicsDeviceType { get; set; }

public int? MaxTextureSize { get; set; }
public static int? MaxTextureSize { get; set; }

public bool? SupportsDrawCallInstancing { get; set; }
public static bool? SupportsDrawCallInstancing { get; set; }

public bool? SupportsRayTracing { get; set; }
public static bool? SupportsRayTracing { get; set; }

public bool? SupportsComputeShaders { get; set; }
public static bool? SupportsComputeShaders { get; set; }

public bool? SupportsGeometryShaders { get; set; }
public static bool? SupportsGeometryShaders { get; set; }

public int? GraphicsShaderLevel { get; set; }
public static int? GraphicsShaderLevel { get; set; }

public bool? IsDebugBuild { get; set; }
public static bool? IsDebugBuild { get; set; }

public string? EditorVersion { get; set; }
public string? InstallMode { get; set; }
public static string? EditorVersion { get; set; }
public static string? InstallMode { get; set; }

public string? TargetFrameRate { get; set; }
public static string? TargetFrameRate { get; set; }

public string? CopyTextureSupport { get; set; }
public static string? CopyTextureSupport { get; set; }

public string? RenderingThreadingMode { get; set; }
public static string? RenderingThreadingMode { get; set; }

public DateTimeOffset? StartTime { get; set; }
public static DateTimeOffset? StartTime { get; set; }

public bool IsMainThread()
internal static bool IsMainThread()
=> MainThreadId.HasValue && Thread.CurrentThread.ManagedThreadId == MainThreadId;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void CollectData() => CollectData(SentrySystemInfoAdapter.Instance);

internal static void CollectData(ISentrySystemInfo sentrySystemInfo)
{
MainThreadId = sentrySystemInfo.MainThreadId;
ProcessorCount = sentrySystemInfo.ProcessorCount;
OperatingSystem = sentrySystemInfo.OperatingSystem;
CpuDescription = sentrySystemInfo.CpuDescription;
SupportsVibration = sentrySystemInfo.SupportsVibration;
DeviceName = sentrySystemInfo.DeviceName;
SystemMemorySize = sentrySystemInfo.SystemMemorySize;
GraphicsDeviceId = sentrySystemInfo.GraphicsDeviceId;
GraphicsDeviceName = sentrySystemInfo.GraphicsDeviceName;
GraphicsDeviceVendor = sentrySystemInfo.GraphicsDeviceVendor;
GraphicsMemorySize = sentrySystemInfo.GraphicsMemorySize;
NpotSupport = sentrySystemInfo.NpotSupport;
GraphicsDeviceVersion = sentrySystemInfo.GraphicsDeviceVersion;
GraphicsDeviceType = sentrySystemInfo.GraphicsDeviceType;
MaxTextureSize = sentrySystemInfo.MaxTextureSize;
SupportsDrawCallInstancing = sentrySystemInfo.SupportsDrawCallInstancing;
SupportsRayTracing = sentrySystemInfo.SupportsRayTracing;
SupportsComputeShaders = sentrySystemInfo.SupportsComputeShaders;
SupportsGeometryShaders = sentrySystemInfo.SupportsGeometryShaders;
GraphicsShaderLevel = sentrySystemInfo.GraphicsShaderLevel;
EditorVersion = sentrySystemInfo.EditorVersion;
InstallMode = sentrySystemInfo.InstallMode;
DeviceType = sentrySystemInfo.DeviceType?.Value;
DeviceUniqueIdentifier = sentrySystemInfo.DeviceUniqueIdentifier?.Value;
DeviceModel = sentrySystemInfo.DeviceModel?.Value;
GraphicsDeviceVendorId = sentrySystemInfo.GraphicsDeviceVendorId?.Value;
GraphicsMultiThreaded = sentrySystemInfo.GraphicsMultiThreaded?.Value;
IsDebugBuild = sentrySystemInfo.IsDebugBuild?.Value;
TargetFrameRate = sentrySystemInfo.TargetFrameRate?.Value;
CopyTextureSupport = sentrySystemInfo.CopyTextureSupport?.Value;
RenderingThreadingMode = sentrySystemInfo.RenderingThreadingMode?.Value;
StartTime = sentrySystemInfo.StartTime?.Value;
}
}
6 changes: 2 additions & 4 deletions src/Sentry.Unity/ScreenshotAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ public ScreenshotAttachment(IAttachmentContent content)

internal class ScreenshotAttachmentContent : IAttachmentContent
{
private readonly SentryMonoBehaviour _behaviour;
private readonly SentryUnityOptions _options;

public ScreenshotAttachmentContent(SentryUnityOptions options, SentryMonoBehaviour behaviour)
public ScreenshotAttachmentContent(SentryUnityOptions options)
{
_behaviour = behaviour;
_options = options;
}

public Stream GetStream()
{
// Note: we need to check explicitly that we're on the same thread. While Unity would throw otherwise
// when capturing the screenshot, it would only do so on development builds. On release, it just crashes...
if (!_behaviour.MainThreadData.IsMainThread())
if (!_options.IsMainThread())
{
_options.DiagnosticLogger?.LogDebug("Can't capture screenshots on other than main (UI) thread.");
return Stream.Null;
Expand Down
Loading
Loading