Skip to content

Commit

Permalink
fix: collect main thread data synchronously during init (#744)
Browse files Browse the repository at this point in the history
* fix: collect main thread data synchronously during init

* chore: update changelog
  • Loading branch information
vaind authored May 16, 2022
1 parent 84bc162 commit 279acb4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Linux native crash support ([#734](https://github.com/getsentry/sentry-unity/pull/734))
- Collect context information synchronously during init to capture it for very early events ([#744](https://github.com/getsentry/sentry-unity/pull/744))

## 0.16.0

Expand Down
11 changes: 6 additions & 5 deletions src/Sentry.Unity/SentryMonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ internal ISentrySystemInfo SentrySystemInfo
set => _sentrySystemInfo = value;
}

private void Start()
=> StartCoroutine(CollectData());
// Note: Awake is called only once and synchronously while the object is built.
// We want to do it this way instead of a StartCoroutine() so that we have the context info ASAP.
private void Awake() => CollectData();

internal IEnumerator CollectData()
internal void CollectData()
{
// Note: Awake() runs on the main thread. The following code just reads a couple of variables so there's no
// delay on the UI and we're safe to do it on the main thread.
MainThreadData.MainThreadId = SentrySystemInfo.MainThreadId;
yield return null;
MainThreadData.ProcessorCount = SentrySystemInfo.ProcessorCount;
MainThreadData.DeviceType = SentrySystemInfo.DeviceType;
MainThreadData.OperatingSystem = SentrySystemInfo.OperatingSystem;
Expand All @@ -154,7 +156,6 @@ internal IEnumerator CollectData()
MainThreadData.DeviceUniqueIdentifier = SentrySystemInfo.DeviceUniqueIdentifier;
MainThreadData.DeviceModel = SentrySystemInfo.DeviceModel;
MainThreadData.SystemMemorySize = SentrySystemInfo.SystemMemorySize;
yield return null;
MainThreadData.GraphicsDeviceId = SentrySystemInfo.GraphicsDeviceId;
MainThreadData.GraphicsDeviceName = SentrySystemInfo.GraphicsDeviceName;
MainThreadData.GraphicsDeviceVendorId = SentrySystemInfo.GraphicsDeviceVendorId;
Expand Down
60 changes: 30 additions & 30 deletions test/Sentry.Unity.Tests/UnityEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public void SentrySdkCaptureEvent_OnNotUIThread_Succeeds()
Assert.NotZero(_testLogger.Logs.Count(log => log.logLevel <= SentryLevel.Info));
}

[UnityTest]
public IEnumerator SentrySdkCaptureEvent_OnNotUIThreadThenUIThreadThenNotUIThread_Cached()
[Test]
public void SentrySdkCaptureEvent_OnNotUIThreadThenUIThreadThenNotUIThread_Cached()
{
// arrange
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
Expand All @@ -113,7 +113,7 @@ public IEnumerator SentrySdkCaptureEvent_OnNotUIThreadThenUIThreadThenNotUIThrea
options.AddEventProcessor(new UnityEventProcessor(options, _sentryMonoBehaviour, _testApplication));
SentryUnity.Init(options);

yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();

// act & assert
var nonUiThreadEventDataNotCached = NonUiThread();
Expand Down Expand Up @@ -275,24 +275,24 @@ public void Process_ServerName_IsNull()
Assert.IsNull(sentryEvent.ServerName);
}

[UnityTest]
public IEnumerator Process_DeviceUniqueIdentifierWithSendDefaultPii_IsNotNull()
[Test]
public void Process_DeviceUniqueIdentifierWithSendDefaultPii_IsNotNull()
{
// arrange
var sentryOptions = new SentryOptions { SendDefaultPii = true };
var sut = new UnityEventProcessor(sentryOptions, _sentryMonoBehaviour, _testApplication);
var sentryEvent = new SentryEvent();

// act
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

// assert
Assert.IsNotNull(sentryEvent.Contexts.Device.DeviceUniqueIdentifier);
}

[UnityTest]
public IEnumerator Process_AppProtocol_Assigned()
[Test]
public void Process_AppProtocol_Assigned()
{
// arrange
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
Expand All @@ -303,15 +303,15 @@ public IEnumerator Process_AppProtocol_Assigned()
var sentryEvent = new SentryEvent();

// act
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
unityEventProcessor.Process(sentryEvent);

// assert
Assert.IsNotNull(sentryEvent.Contexts.App.StartTime);
}

[UnityTest]
public IEnumerator Process_Tags_Set()
[Test]
public void Process_Tags_Set()
{
// arrange
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
Expand All @@ -327,7 +327,7 @@ public IEnumerator Process_Tags_Set()
var sentryEvent = new SentryEvent();

// act
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
unityEventProcessor.Process(sentryEvent);

// assert
Expand Down Expand Up @@ -357,8 +357,8 @@ public IEnumerator Process_Tags_Set()
Assert.AreEqual("true", isMainThread.Value);
}

[UnityTest]
public IEnumerator Process_OperatingSystemProtocol_Assigned()
[Test]
public void Process_OperatingSystemProtocol_Assigned()
{
// arrange
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo { OperatingSystem = "Windows" };
Expand All @@ -367,15 +367,15 @@ public IEnumerator Process_OperatingSystemProtocol_Assigned()

// act
// SentryInitialization always called
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

// assert
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.OperatingSystem, sentryEvent.Contexts.OperatingSystem.RawDescription);
}

[UnityTest]
public IEnumerator Process_DeviceProtocol_Assigned()
[Test]
public void Process_DeviceProtocol_Assigned()
{
const long toByte = 1048576L; // in `UnityEventProcessor.PopulateDevice`
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
Expand All @@ -391,7 +391,7 @@ public IEnumerator Process_DeviceProtocol_Assigned()
var sut = new UnityEventProcessor(_sentryOptions, _sentryMonoBehaviour, _testApplication);
var sentryEvent = new SentryEvent();

yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.ProcessorCount, sentryEvent.Contexts.Device.ProcessorCount);
Expand All @@ -403,8 +403,8 @@ public IEnumerator Process_DeviceProtocol_Assigned()
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.SystemMemorySize * toByte, sentryEvent.Contexts.Device.MemorySize);
}

[UnityTest]
public IEnumerator Process_UnityProtocol_Assigned()
[Test]
public void Process_UnityProtocol_Assigned()
{
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
{
Expand All @@ -418,7 +418,7 @@ public IEnumerator Process_UnityProtocol_Assigned()
var sentryEvent = new SentryEvent();

// act
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

var unityProtocol = (Unity.Protocol.Unity)sentryEvent.Contexts.GetOrAdd(Unity.Protocol.Unity.Type, _ => new Unity.Protocol.Unity());
Expand All @@ -428,8 +428,8 @@ public IEnumerator Process_UnityProtocol_Assigned()
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.RenderingThreadingMode!.Value, unityProtocol.RenderingThreadingMode);
}

[UnityTest]
public IEnumerator Process_GpuProtocol_Assigned()
[Test]
public void Process_GpuProtocol_Assigned()
{
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
{
Expand All @@ -453,7 +453,7 @@ public IEnumerator Process_GpuProtocol_Assigned()

// act
// SentryInitialization always called
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.GraphicsDeviceId, sentryEvent.Contexts.Gpu.Id);
Expand All @@ -472,8 +472,8 @@ public IEnumerator Process_GpuProtocol_Assigned()
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.SupportsGeometryShaders, sentryEvent.Contexts.Gpu.SupportsGeometryShaders);
}

[UnityTest]
public IEnumerator Process_GpuProtocolGraphicsShaderLevel_Assigned(
[Test]
public void Process_GpuProtocolGraphicsShaderLevel_Assigned(
[ValueSource(nameof(ShaderLevels))] (int, string) shaderValue)
{
var (shaderLevel, shaderDescription) = shaderValue;
Expand All @@ -488,7 +488,7 @@ public IEnumerator Process_GpuProtocolGraphicsShaderLevel_Assigned(

// act
// SentryInitialization always called
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

Assert.AreEqual(shaderDescription, sentryEvent.Contexts.Gpu.GraphicsShaderLevel);
Expand All @@ -507,8 +507,8 @@ private static readonly (int shaderLevel, string shaderDescription)[] ShaderLeve
(21, "21")
};

[UnityTest]
public IEnumerator Process_GpuProtocolGraphicsShaderLevelMinusOne_Ignored()
[Test]
public void Process_GpuProtocolGraphicsShaderLevelMinusOne_Ignored()
{
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
{
Expand All @@ -520,7 +520,7 @@ public IEnumerator Process_GpuProtocolGraphicsShaderLevelMinusOne_Ignored()

// act
// SentryInitialization always called
yield return _sentryMonoBehaviour.CollectData();
_sentryMonoBehaviour.CollectData();
sut.Process(sentryEvent);

Assert.IsNull(sentryEvent.Contexts.Gpu.GraphicsShaderLevel);
Expand Down

0 comments on commit 279acb4

Please sign in to comment.