Skip to content

Commit c91b85d

Browse files
authored
Merge branch 'main' into feat/default-user-id
2 parents 78fdd89 + 4b15fb8 commit c91b85d

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Fixes
1212

1313
- The SDK no longer accesses `AnalyticsSessionInfo.userId` when targeting console platforms. This was leading to crashes during launch. ([#2309](https://github.com/getsentry/sentry-unity/pull/2309))
14+
- The SDK now deduplicates trace generation during startup and scene loading ([#2301](https://github.com/getsentry/sentry-unity/pull/2301))
1415
- The SDK no longer appends multiple upload tasks when building for Android ([#2300](https://github.com/getsentry/sentry-unity/pull/2300))
1516
- Fixed false positive ANR events on `WebGL`, i.e. when switching tabs, or unfocusing the player ([#2306](https://github.com/getsentry/sentry-unity/pull/2306))
1617
- The SDK now automatically picks up previously missing debug symbols - i.e. `BurstDebugInformation`, by passing the

src/Sentry.Unity/Integrations/TraceGenerationIntegration.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Sentry.Unity.Integrations;
55

6+
/// <summary>
7+
/// The TraceGenerationIntegration is used to generate new trace propagation contexts
8+
/// /// </summary>
69
internal sealed class TraceGenerationIntegration : ISdkIntegration
710
{
811
private readonly ISceneManager _sceneManager;
@@ -19,21 +22,34 @@ internal TraceGenerationIntegration(ISentryMonoBehaviour sentryMonoBehaviour, IS
1922

2023
public void Register(IHub hub, SentryOptions options)
2124
{
22-
hub.ConfigureScope(UpdatePropagationContext);
25+
if (options is not SentryUnityOptions unityOptions)
26+
{
27+
return;
28+
}
2329

2430
_sentryMonoBehaviour.ApplicationResuming += () =>
2531
{
26-
options.DiagnosticLogger?.LogDebug("Application resumed. Creating new Trace.");
27-
hub.ConfigureScope(UpdatePropagationContext);
32+
options.DiagnosticLogger?.LogDebug("Game resuming. Creating new Trace.");
33+
hub.ConfigureScope(scope => scope.SetPropagationContext(new SentryPropagationContext()));
2834
};
2935

30-
_sceneManager.ActiveSceneChanged += (_, _) =>
36+
var isTracingEnabled = unityOptions.TracesSampleRate > 0.0f;
37+
38+
// Create initial trace context if tracing is disabled or startup tracing is disabled
39+
if (!isTracingEnabled || !unityOptions.AutoStartupTraces)
3140
{
32-
options.DiagnosticLogger?.LogDebug("Active Scene changed. Creating new Trace.");
33-
hub.ConfigureScope(UpdatePropagationContext);
34-
};
35-
}
41+
options.DiagnosticLogger?.LogDebug("Startup. Creating new Trace.");
42+
hub.ConfigureScope(scope => scope.SetPropagationContext(new SentryPropagationContext()));
43+
}
3644

37-
private static void UpdatePropagationContext(Scope scope) =>
38-
scope.SetPropagationContext(new SentryPropagationContext());
45+
// Set up scene change handling if tracing is disabled or auto scene load traces are disabled
46+
if (!isTracingEnabled || !unityOptions.AutoSceneLoadTraces)
47+
{
48+
_sceneManager.ActiveSceneChanged += (_, _) =>
49+
{
50+
options.DiagnosticLogger?.LogDebug("Active Scene changed. Creating new Trace.");
51+
hub.ConfigureScope(scope => scope.SetPropagationContext(new SentryPropagationContext()));
52+
};
53+
}
54+
}
3955
}

test/Sentry.Unity.Tests/TraceGenerationIntegrationTests.cs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ private class Fixture
1616
public TestSentryMonoBehaviour SentryMonoBehaviour { get; set; } = new();
1717
public TestHub TestHub { get; set; } = new();
1818
public TestLogger Logger { get; set; } = new();
19-
public SentryOptions SentryOptions { get; set; }
19+
public SentryUnityOptions SentryOptions { get; set; }
2020

21-
public Fixture() => SentryOptions = new SentryOptions { DiagnosticLogger = Logger };
21+
public Fixture() => SentryOptions = new SentryUnityOptions { DiagnosticLogger = Logger };
2222

2323
public TraceGenerationIntegration GetSut() => new(SentryMonoBehaviour, SceneManager);
2424
}
@@ -32,10 +32,14 @@ public void SetUp()
3232
Sentry.SentrySdk.UseHub(_fixture.TestHub);
3333
}
3434

35-
[Test]
36-
public void TraceGeneration_OnRegister_GeneratesInitialTrace()
35+
[TestCase(0.0f, false)]
36+
[TestCase(0.0f, true)]
37+
[TestCase(1.0f, false)]
38+
public void Register_TracingDisabledOrAutoStartupTracesDisabled_GeneratesInitialTrace(float tracesSampleRate, bool autoStartupTraces)
3739
{
3840
// Arrange
41+
_fixture.SentryOptions.TracesSampleRate = tracesSampleRate;
42+
_fixture.SentryOptions.AutoStartupTraces = autoStartupTraces;
3943
var sut = _fixture.GetSut();
4044

4145
// Act
@@ -46,12 +50,26 @@ public void TraceGeneration_OnRegister_GeneratesInitialTrace()
4650
var scope = new Scope(_fixture.SentryOptions);
4751
var initialPropagationContext = scope.PropagationContext;
4852
configureScope(scope);
49-
5053
Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext);
5154
}
5255

5356
[Test]
54-
public void TraceGeneration_OnApplicationResume_GeneratesNewTrace()
57+
public void Register_TracingEnabledAndAutoStartupTracesEnabled_DoesNotGenerateInitialTrace()
58+
{
59+
// Arrange
60+
_fixture.SentryOptions.TracesSampleRate = 1.0f;
61+
_fixture.SentryOptions.AutoStartupTraces = true;
62+
var sut = _fixture.GetSut();
63+
64+
// Act
65+
sut.Register(_fixture.TestHub, _fixture.SentryOptions);
66+
67+
// Assert
68+
Assert.IsEmpty(_fixture.TestHub.ConfigureScopeCalls);
69+
}
70+
71+
[Test]
72+
public void ApplicationResuming_WhenCalled_GeneratesNewTrace()
5573
{
5674
// Arrange
5775
var sut = _fixture.GetSut();
@@ -62,7 +80,6 @@ public void TraceGeneration_OnApplicationResume_GeneratesNewTrace()
6280
_fixture.SentryMonoBehaviour.ResumeApplication();
6381

6482
// Assert
65-
// Calling 'Register' already generated a trace, so we expect 1+1 calls to ConfigureScope
6683
Assert.AreEqual(initialCallsCount + 1, _fixture.TestHub.ConfigureScopeCalls.Count);
6784
var configureScope = _fixture.TestHub.ConfigureScopeCalls.Last();
6885
var scope = new Scope(_fixture.SentryOptions);
@@ -72,10 +89,15 @@ public void TraceGeneration_OnApplicationResume_GeneratesNewTrace()
7289
Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext);
7390
}
7491

75-
[Test]
76-
public void TraceGeneration_OnActiveSceneChange_GeneratesNewTrace()
92+
[TestCase(0.0f, false)]
93+
[TestCase(0.0f, true)]
94+
[TestCase(1.0f, false)]
95+
public void ActiveSceneChanged_TracingDisabledOrAutoSceneLoadTracesDisabled_GeneratesTrace(float tracesSampleRate, bool autoSceneLoadTraces)
7796
{
7897
// Arrange
98+
_fixture.SentryOptions.TracesSampleRate = tracesSampleRate;
99+
_fixture.SentryOptions.AutoSceneLoadTraces = autoSceneLoadTraces;
100+
79101
var sut = _fixture.GetSut();
80102
sut.Register(_fixture.TestHub, _fixture.SentryOptions);
81103
var initialCallsCount = _fixture.TestHub.ConfigureScopeCalls.Count;
@@ -84,13 +106,29 @@ public void TraceGeneration_OnActiveSceneChange_GeneratesNewTrace()
84106
_fixture.SceneManager.OnActiveSceneChanged(new SceneAdapter("from scene name"), new SceneAdapter("to scene name"));
85107

86108
// Assert
87-
// Calling 'Register' already generated a trace, so we expect 1+1 calls to ConfigureScope
88109
Assert.AreEqual(initialCallsCount + 1, _fixture.TestHub.ConfigureScopeCalls.Count);
89110
var configureScope = _fixture.TestHub.ConfigureScopeCalls.Last();
90111
var scope = new Scope(_fixture.SentryOptions);
91112
var initialPropagationContext = scope.PropagationContext;
92113
configureScope(scope);
93-
94114
Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext);
95115
}
116+
117+
[Test]
118+
public void ActiveSceneChanged_TracingEnabledAndAutoSceneLoadTracesEnabled_DoesNotGenerateTrace()
119+
{
120+
// Arrange
121+
_fixture.SentryOptions.TracesSampleRate = 1.0f;
122+
_fixture.SentryOptions.AutoSceneLoadTraces = true;
123+
124+
var sut = _fixture.GetSut();
125+
sut.Register(_fixture.TestHub, _fixture.SentryOptions);
126+
var initialCallsCount = _fixture.TestHub.ConfigureScopeCalls.Count;
127+
128+
// Act
129+
_fixture.SceneManager.OnActiveSceneChanged(new SceneAdapter("from scene name"), new SceneAdapter("to scene name"));
130+
131+
// Assert
132+
Assert.AreEqual(initialCallsCount, _fixture.TestHub.ConfigureScopeCalls.Count);
133+
}
96134
}

0 commit comments

Comments
 (0)