Skip to content

Commit 1daeb87

Browse files
authored
fix: Set ActiveSceneName in EventProcessor (#2400)
1 parent 2ee89b5 commit 1daeb87

File tree

6 files changed

+67
-24
lines changed

6 files changed

+67
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- The SDK now correctly sets the currently active scene's name on the event ([#2400](https://github.com/getsentry/sentry-unity/pull/2400))
78
- Fixed an issue where screenshot capture triggered on a burst job would crash the game. The SDK can now also capture screenshots on events that occur outside of the main thread ([#2392](https://github.com/getsentry/sentry-unity/pull/2392))
89
- Structured logs now have the `origin` and `sdk` attributes correctly set ([#2390](https://github.com/getsentry/sentry-unity/pull/2390))
910
- Resolved possible startup crashes on Android VR platforms like the Oculus Quest. The SDK no longer natively subscribes to interaction hooks for automatic tracing and breadcrumb creation. ([#2393](https://github.com/getsentry/sentry-unity/pull/2393))

src/Sentry.Unity/Integrations/UnityScopeIntegration.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ internal static class UnitySdkInfo
2020
internal class UnityScopeIntegration : ISdkIntegration
2121
{
2222
private readonly IApplication _application;
23-
private readonly ISentryUnityInfo? _unityInfo;
2423

25-
public UnityScopeIntegration(IApplication application, ISentryUnityInfo? unityInfo)
24+
public UnityScopeIntegration(IApplication application)
2625
{
2726
_application = application;
28-
_unityInfo = unityInfo;
2927
}
3028

3129
public void Register(IHub hub, SentryOptions options)
3230
{
33-
var scopeUpdater = new UnityScopeUpdater((SentryUnityOptions)options, _application, _unityInfo);
31+
var scopeUpdater = new UnityScopeUpdater((SentryUnityOptions)options, _application);
3432
hub.ConfigureScope(scopeUpdater.ConfigureScope);
3533
}
3634
}
@@ -39,15 +37,11 @@ internal class UnityScopeUpdater
3937
{
4038
private readonly SentryUnityOptions _options;
4139
private readonly IApplication _application;
42-
private readonly ISentryUnityInfo? _unityInfo;
43-
private readonly ISceneManager _sceneManager;
4440

45-
public UnityScopeUpdater(SentryUnityOptions options, IApplication application, ISentryUnityInfo? unityInfo = null, ISceneManager? sceneManager = null)
41+
public UnityScopeUpdater(SentryUnityOptions options, IApplication application)
4642
{
4743
_options = options;
4844
_application = application;
49-
_unityInfo = unityInfo;
50-
_sceneManager = sceneManager ?? SceneManagerAdapter.Instance;
5145
}
5246

5347
public void ConfigureScope(Scope scope)
@@ -158,12 +152,6 @@ private void PopulateUnity(Protocol.Unity unity)
158152
unity.TargetFrameRate = MainThreadData.TargetFrameRate;
159153
unity.CopyTextureSupport = MainThreadData.CopyTextureSupport;
160154
unity.RenderingThreadingMode = MainThreadData.RenderingThreadingMode;
161-
162-
if (_unityInfo?.IL2CPP is true)
163-
{
164-
// Currently an IL2CPP only feature: see https://github.com/getsentry/sentry-unity/issues/2181
165-
unity.ActiveSceneName = _sceneManager.GetActiveScene().Name;
166-
}
167155
}
168156

169157
private void PopulateTags(Action<string, string> setTag)

src/Sentry.Unity/SentryUnityOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ internal SentryUnityOptions(IApplication? application = null,
341341
AddInAppExclude("Cysharp");
342342
AddInAppExclude("DG.Tweening");
343343

344-
var processor = new UnityEventProcessor(this);
344+
var processor = new UnityEventProcessor(this, UnityInfo);
345345
AddEventProcessor(processor);
346346
AddTransactionProcessor(processor);
347347

@@ -354,7 +354,7 @@ internal SentryUnityOptions(IApplication? application = null,
354354

355355
AddIntegration(new StartupTracingIntegration());
356356
AddIntegration(new AnrIntegration(behaviour));
357-
AddIntegration(new UnityScopeIntegration(application, unityInfo));
357+
AddIntegration(new UnityScopeIntegration(application));
358358
AddIntegration(new UnityBeforeSceneLoadIntegration());
359359
AddIntegration(new SceneManagerIntegration());
360360
AddIntegration(new SceneManagerTracingIntegration());

src/Sentry.Unity/UnityEventProcessor.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Sentry.Extensibility;
33
using Sentry.Protocol;
4+
using Sentry.Unity.Integrations;
45
using UnityEngine;
56
using DeviceOrientation = Sentry.Protocol.DeviceOrientation;
67

@@ -11,13 +12,19 @@ internal class UnityEventProcessor :
1112
ISentryTransactionProcessor
1213
{
1314
private readonly SentryUnityOptions _sentryOptions;
15+
private readonly ISentryUnityInfo _unityInfo;
16+
private readonly ISceneManager _sceneManager;
17+
private readonly IApplication _application;
1418

15-
public UnityEventProcessor(SentryUnityOptions sentryOptions)
19+
public UnityEventProcessor(SentryUnityOptions sentryOptions, ISentryUnityInfo unityInfo, IApplication? application = null, ISceneManager? sceneManager = null)
1620
{
1721
_sentryOptions = sentryOptions;
22+
_unityInfo = unityInfo;
23+
_application = application ?? ApplicationAdapter.Instance;
24+
_sceneManager = sceneManager ?? SceneManagerAdapter.Instance;
1825
}
1926

20-
public SentryTransaction? Process(SentryTransaction transaction)
27+
public SentryTransaction Process(SentryTransaction transaction)
2128
{
2229
SetEventContext(transaction);
2330
return transaction;
@@ -37,6 +44,17 @@ private void SetEventContext(IEventLike sentryEvent)
3744
try
3845
{
3946
PopulateDevice(sentryEvent.Contexts.Device);
47+
48+
// The Unity context should get set in the UnityScopeIntegration automatically sets it when it gets registered
49+
sentryEvent.Contexts.TryGetValue(Protocol.Unity.Type, out var contextObject);
50+
if (contextObject is not Protocol.Unity unityContext)
51+
{
52+
unityContext = new Protocol.Unity();
53+
sentryEvent.Contexts.Add(Protocol.Unity.Type, unityContext);
54+
}
55+
56+
PopulateUnity(unityContext);
57+
4058
// Populating the SDK Integrations here (for now) instead of UnityScopeIntegration because it cannot be guaranteed
4159
// that it got added last or that there was not an integration added at a later point
4260
PopulateSdkIntegrations(sentryEvent.Sdk);
@@ -81,6 +99,20 @@ private void PopulateDevice(Device device)
8199
}
82100
}
83101

102+
private void PopulateUnity(Protocol.Unity unity)
103+
{
104+
if (!MainThreadData.IsMainThread())
105+
{
106+
return;
107+
}
108+
109+
if (_application.IsEditor || _unityInfo.IL2CPP)
110+
{
111+
// Currently an IL2CPP only feature: see https://github.com/getsentry/sentry-unity/issues/2181
112+
unity.ActiveSceneName = _sceneManager.GetActiveScene().Name;
113+
}
114+
}
115+
84116
private void PopulateSdkIntegrations(SdkVersion sdkVersion)
85117
{
86118
foreach (var integrationName in _sentryOptions.SdkIntegrationNames)

test/Scripts.Integration.Test/Scripts/SmokeTester.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private IEnumerator SmokeTestCoroutine()
148148
t.ExpectMessage(currentMessage, "'type':'os',");
149149
t.ExpectMessage(currentMessage, "'type':'runtime',");
150150
t.ExpectMessage(currentMessage, "'type':'unity',");
151+
t.ExpectMessage(currentMessage, "'active_scene_name':'"); // active scene name
151152
// User
152153
t.ExpectMessage(currentMessage, "'user':{'id':'"); // non-null automatic ID
153154
t.ExpectMessageNot(currentMessage, "'length':0");

test/Sentry.Unity.Tests/UnityEventScopeTests.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void SentrySdkCaptureEvent(bool captureOnUiThread)
163163
Assert.AreEqual(systemInfo.TargetFrameRate!.Value, unityContext!.TargetFrameRate);
164164
Assert.AreEqual(systemInfo.CopyTextureSupport!.Value, unityContext.CopyTextureSupport);
165165
Assert.AreEqual(systemInfo.RenderingThreadingMode!.Value, unityContext.RenderingThreadingMode);
166-
Assert.AreEqual(SceneManager.GetActiveScene().name, unityContext.ActiveSceneName);
166+
Assert.AreEqual(captureOnUiThread ? SceneManager.GetActiveScene().name : null, unityContext.ActiveSceneName);
167167

168168
Assert.IsNull(@event.ServerName);
169169
}
@@ -338,7 +338,8 @@ public void Tags_Set()
338338

339339
var sentryOptions = new SentryUnityOptions { SendDefaultPii = true };
340340
var scopeUpdater = new UnityScopeUpdater(sentryOptions, _testApplication);
341-
var unityEventProcessor = new UnityEventProcessor(sentryOptions);
341+
var unityInfo = new TestUnityInfo { IL2CPP = true };
342+
var unityEventProcessor = new UnityEventProcessor(sentryOptions, unityInfo);
342343
var scope = new Scope(sentryOptions);
343344
var sentryEvent = new SentryEvent();
344345
var transaction = new SentryTransaction("name", "operation");
@@ -438,7 +439,6 @@ public void DeviceProtocol_Assigned()
438439
[TestCase(false)]
439440
public void UnityProtocol_Assigned(bool isIL2CPP)
440441
{
441-
var sceneManager = new SceneManagerIntegrationTests.FakeSceneManager { ActiveSceneName = "TestScene" };
442442
var systemInfo = new TestSentrySystemInfo
443443
{
444444
EditorVersion = "TestEditorVersion2022.3.2f1",
@@ -450,7 +450,7 @@ public void UnityProtocol_Assigned(bool isIL2CPP)
450450
MainThreadData.SentrySystemInfo = systemInfo;
451451
MainThreadData.CollectData();
452452

453-
var sut = new UnityScopeUpdater(_sentryOptions, _testApplication, new TestUnityInfo { IL2CPP = isIL2CPP }, sceneManager);
453+
var sut = new UnityScopeUpdater(_sentryOptions, _testApplication);
454454
var scope = new Scope(_sentryOptions);
455455

456456
// act
@@ -465,7 +465,6 @@ public void UnityProtocol_Assigned(bool isIL2CPP)
465465
Assert.AreEqual(systemInfo.TargetFrameRate!.Value, unityProtocol.TargetFrameRate);
466466
Assert.AreEqual(systemInfo.CopyTextureSupport!.Value, unityProtocol.CopyTextureSupport);
467467
Assert.AreEqual(systemInfo.RenderingThreadingMode!.Value, unityProtocol.RenderingThreadingMode);
468-
Assert.AreEqual(isIL2CPP ? sceneManager.GetActiveScene().Name : null, unityProtocol.ActiveSceneName);
469468
}
470469

471470
[Test]
@@ -565,6 +564,28 @@ public void GpuProtocolGraphicsShaderLevelMinusOne_Ignored()
565564
// assert
566565
Assert.IsNull(scope.Contexts.Gpu.GraphicsShaderLevel);
567566
}
567+
568+
[Test]
569+
[TestCase(true, true)]
570+
[TestCase(false, true)]
571+
[TestCase(true, false)]
572+
[TestCase(false, false)]
573+
public void Process_SetsActiveSceneName(bool isEditor, bool isIL2CPP)
574+
{
575+
var sentryOptions = new SentryUnityOptions();
576+
var application = new TestApplication { IsEditor = isEditor };
577+
var unityInfo = new TestUnityInfo { IL2CPP = isIL2CPP };
578+
var sceneManager = new SceneManagerIntegrationTests.FakeSceneManager { ActiveSceneName = "TestScene" };
579+
var sut = new UnityEventProcessor(sentryOptions, unityInfo, application, sceneManager);
580+
var sentryEvent = new SentryEvent();
581+
582+
sut.Process(sentryEvent);
583+
584+
sentryEvent.Contexts.TryGetValue(Unity.Protocol.Unity.Type, out var unityProtocolObject);
585+
var unityProtocol = unityProtocolObject as Unity.Protocol.Unity;
586+
Assert.NotNull(unityProtocol);
587+
Assert.AreEqual(isEditor || isIL2CPP ? sceneManager.GetActiveScene().Name : null, unityProtocol!.ActiveSceneName);
588+
}
568589
}
569590

570591
internal sealed class TestSentrySystemInfo : ISentrySystemInfo

0 commit comments

Comments
 (0)