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

fix: Missing contexts #1850

Merged
merged 3 commits into from
Oct 18, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- The SDK no longer fails to report contexts like `device` and `gpu` ([#1850](https://github.com/getsentry/sentry-unity/pull/1850))

## 2.2.1

### Dependencies
Expand Down
8 changes: 5 additions & 3 deletions src/Sentry.Unity/MainThreadData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ internal static class MainThreadData
public static bool IsMainThread()
=> MainThreadId.HasValue && Thread.CurrentThread.ManagedThreadId == MainThreadId;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void CollectData() => CollectData(SentrySystemInfoAdapter.Instance);
// For testing
internal static ISentrySystemInfo? SentrySystemInfo { get; set; }

internal static void CollectData(ISentrySystemInfo sentrySystemInfo)
public static void CollectData()
{
var sentrySystemInfo = SentrySystemInfo ?? SentrySystemInfoAdapter.Instance;

MainThreadId = sentrySystemInfo.MainThreadId;
ProcessorCount = sentrySystemInfo.ProcessorCount;
OperatingSystem = sentrySystemInfo.OperatingSystem;
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry.Unity/SentryUnitySDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ private SentryUnitySdk(SentryUnityOptions options)
return null;
}

MainThreadData.CollectData();
vaind marked this conversation as resolved.
Show resolved Hide resolved

// On Standalone, we disable cache dir in case multiple app instances run over the same path.
// Note: we cannot use a named Mutex, because Unit doesn't support it. Instead, we create a file with `FileShare.None`.
// https://forum.unity.com/threads/unsupported-internal-call-for-il2cpp-mutex-createmutex_internal-named-mutexes-are-not-supported.387334/
Expand Down
29 changes: 23 additions & 6 deletions test/Scripts.Integration.Test/Scripts/SmokeTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,13 @@ public static void SmokeTest()
t.Start("SMOKE");
try
{
#if !UNITY_EDITOR
var crashed = CrashedLastRun();
t.Expect($"options.CrashedLastRun ({crashed}) == false (0)", crashed == 0);

#endif
var currentMessage = 0;
t.ExpectMessage(currentMessage, "'type':'session'");

var guid = Guid.NewGuid().ToString();
Debug.LogError($"LogError(GUID)={guid}");

// Skip the session init requests (there may be multiple of them). We can't skip them by a "positive"
// because they're also repeated with standard events (in an envelope).
Debug.Log("SMOKE TEST: Skipping all session requests");
Expand All @@ -114,17 +112,34 @@ public static void SmokeTest()

t.ExpectMessage(currentMessage, "'type':'transaction");
t.ExpectMessage(currentMessage, "'op':'app.start'"); // startup transaction
#if !UNITY_EDITOR
t.ExpectMessage(currentMessage, "'op':'awake','description':'Main Camera.SmokeTester'"); // auto instrumentation
#endif
t.ExpectMessageNot(currentMessage, "'length':0");

var guid = Guid.NewGuid().ToString();
Debug.LogError($"LogError(GUID)={guid}");
currentMessage++;

t.ExpectMessage(++currentMessage, "'type':'event'");
t.ExpectMessage(currentMessage, "'type':'event'");
t.ExpectMessage(currentMessage, $"LogError(GUID)={guid}");
// Contexts
t.ExpectMessage(currentMessage, "'type':'app',");
t.ExpectMessage(currentMessage, "'type':'device',");
t.ExpectMessage(currentMessage, "'type':'gpu',");
t.ExpectMessage(currentMessage, "'type':'os',");
t.ExpectMessage(currentMessage, "'type':'runtime',");
t.ExpectMessage(currentMessage, "'type':'unity',");
// User
t.ExpectMessage(currentMessage, "'user':{'id':'"); // non-null automatic ID
// Attachment
t.ExpectMessage(currentMessage, "'filename':'screenshot.jpg','attachment_type':'event.attachment'");
t.ExpectMessageNot(currentMessage, "'length':0");

SentrySdk.CaptureMessage($"CaptureMessage(GUID)={guid}");
t.ExpectMessage(++currentMessage, "'type':'event'");
currentMessage++;

t.ExpectMessage(currentMessage, "'type':'event'");
t.ExpectMessage(currentMessage, $"CaptureMessage(GUID)={guid}");
t.ExpectMessage(currentMessage, "'filename':'screenshot.jpg','attachment_type':'event.attachment'");
t.ExpectMessageNot(currentMessage, "'length':0");
Expand Down Expand Up @@ -252,10 +267,12 @@ public void Exit(int code)
}
else
{
#if !UNITY_EDITOR
ExitCode = code;
Application.Quit(code);
// Application.Quit doesn't actually terminate immediately so exit the context at least...
throw new Exception($"Quitting with exit code {code}");
#endif
}
}

Expand Down
7 changes: 6 additions & 1 deletion test/Scripts.Integration.Test/run-smoke-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
[Parameter()]
[switch] $Crash
)
. $PSScriptRoot/globals.ps1

if (-not $Global:NewProjectPathCache)
{
. ./test/Scripts.Integration.Test/globals.ps1
}

. $PSScriptRoot/common.ps1

Write-Host "Given parameters:"
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Unity.Tests/ContextWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Arguments()
};

// act
MainThreadData.CollectData(sysInfo);
MainThreadData.SentrySystemInfo = sysInfo;
SentryUnity.Init(options);
Assert.IsTrue(context.SyncFinished.WaitOne(TimeSpan.FromSeconds(10)));

Expand Down
34 changes: 23 additions & 11 deletions test/Sentry.Unity.Tests/UnityEventScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public void SentrySdkCaptureEvent(bool captureOnUiThread)
DiagnosticLogger = _testLogger
};

// In an actual build, the collection is automatically triggered by `RuntimeInitializeLoadType.BeforeSceneLoad`
MainThreadData.CollectData(systemInfo);
// In an actual build, the collection is automatically triggered before the SDK initializes
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();

SentryUnity.Init(options);

Expand Down Expand Up @@ -232,7 +233,11 @@ public void EventDeviceSimulator_SetCorrectly(bool isEditor, bool? isSimulator)
public void DeviceUniqueIdentifierWithSendDefaultPii_IsNotNull()
{
// arrange
MainThreadData.CollectData(new TestSentrySystemInfo { DeviceUniqueIdentifier = new Lazy<string>(() => "83fdd6d4-50b1-4735-a4d1-d4f7de64aff0") });
MainThreadData.SentrySystemInfo = new TestSentrySystemInfo
{
DeviceUniqueIdentifier = new Lazy<string>(() => "83fdd6d4-50b1-4735-a4d1-d4f7de64aff0")
};
MainThreadData.CollectData();

var sentryOptions = new SentryUnityOptions { SendDefaultPii = true };
var sut = new UnityScopeUpdater(sentryOptions, _testApplication);
Expand Down Expand Up @@ -304,7 +309,8 @@ public void Tags_Set()
DeviceUniqueIdentifier = new(() => "f810306c-68db-4ebe-89ba-13c457449339"),
InstallMode = ApplicationInstallMode.Store.ToString()
};
MainThreadData.CollectData(systemInfo);
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();

var sentryOptions = new SentryUnityOptions { SendDefaultPii = true };
var scopeUpdater = new UnityScopeUpdater(sentryOptions, _testApplication);
Expand Down Expand Up @@ -356,7 +362,8 @@ public void OperatingSystemProtocol_Assigned()
{
// arrange
var systemInfo = new TestSentrySystemInfo { OperatingSystem = "Windows" };
MainThreadData.CollectData(systemInfo);
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();

var sut = new UnityScopeUpdater(_sentryOptions, _testApplication);
var scope = new Scope(_sentryOptions);
Expand All @@ -382,7 +389,8 @@ public void DeviceProtocol_Assigned()
DeviceModel = new Lazy<string>(() => "Samsung Galaxy S3"),
SystemMemorySize = 16000
};
MainThreadData.CollectData(systemInfo);
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();

var sut = new UnityScopeUpdater(_sentryOptions, _testApplication);
var scope = new Scope(_sentryOptions);
Expand Down Expand Up @@ -412,7 +420,8 @@ public void UnityProtocol_Assigned()
CopyTextureSupport = new Lazy<string>(() => "Basic, Copy3D, DifferentTypes, TextureToRT, RTToTexture"),
RenderingThreadingMode = new Lazy<string>(() => "MultiThreaded")
};
MainThreadData.CollectData(systemInfo);
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();

var sut = new UnityScopeUpdater(_sentryOptions, _testApplication);
var scope = new Scope(_sentryOptions);
Expand Down Expand Up @@ -451,7 +460,8 @@ public void GpuProtocol_Assigned()
SupportsComputeShaders = true,
SupportsGeometryShaders = true
};
MainThreadData.CollectData(systemInfo);
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();

var sut = new UnityScopeUpdater(_sentryOptions, _testApplication);
var scope = new Scope(_sentryOptions);
Expand Down Expand Up @@ -482,7 +492,8 @@ public void GpuProtocolGraphicsShaderLevel_Assigned(
[ValueSource(nameof(ShaderLevels))] (int, string) shaderValue)
{
var (shaderLevel, shaderDescription) = shaderValue;
MainThreadData.CollectData(new TestSentrySystemInfo { GraphicsShaderLevel = shaderLevel });
MainThreadData.SentrySystemInfo = new TestSentrySystemInfo { GraphicsShaderLevel = shaderLevel };
MainThreadData.CollectData();

var sut = new UnityScopeUpdater(_sentryOptions, _testApplication);
var scope = new Scope(_sentryOptions);
Expand Down Expand Up @@ -510,7 +521,7 @@ private static readonly (int shaderLevel, string shaderDescription)[] ShaderLeve
[Test]
public void GpuProtocolGraphicsShaderLevelMinusOne_Ignored()
{
var sentrySystemInfo = new TestSentrySystemInfo
var systemInfo = new TestSentrySystemInfo
{
GraphicsShaderLevel = -1
};
Expand All @@ -519,7 +530,8 @@ public void GpuProtocolGraphicsShaderLevelMinusOne_Ignored()
var scope = new Scope(_sentryOptions);

// act
MainThreadData.CollectData(sentrySystemInfo);
MainThreadData.SentrySystemInfo = systemInfo;
MainThreadData.CollectData();
sut.ConfigureScope(scope);

// assert
Expand Down
Loading