-
-
Notifications
You must be signed in to change notification settings - Fork 55
feat: Trace Generation Integration #2123
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
772d806
first iteration
bitsandfoxes 578aed5
Format code
getsentry-bot 1d17bef
finalized
bitsandfoxes 3768708
Merge branch 'feat/trace-id-integration' of https://github.com/getsen…
bitsandfoxes a33f5ee
Merge branch 'main' into feat/trace-id-integration
bitsandfoxes d9e1831
Updated CHANGELOG.md
bitsandfoxes e54332e
Merge branch 'main' into feat/trace-id-integration
bitsandfoxes 7b48645
Merge branch 'main' into feat/trace-id-integration
bitsandfoxes 06f1f45
additional creation
bitsandfoxes 1d885d8
Merge branch 'feat/trace-id-integration' of https://github.com/getsen…
bitsandfoxes 89d82c8
Format code
getsentry-bot 7e529db
fixed tests
bitsandfoxes 5f0a05a
Merge branch 'feat/trace-id-integration' of https://github.com/getsen…
bitsandfoxes fd0d202
Updated CHANGELOG.md
bitsandfoxes f61f931
seal
bitsandfoxes 535fe05
merged main
bitsandfoxes acbcda8
cleanup
bitsandfoxes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Sentry.Unity/Integrations/TraceGenerationIntegration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Sentry.Extensibility; | ||
using Sentry.Integrations; | ||
|
||
namespace Sentry.Unity.Integrations; | ||
|
||
internal sealed class TraceGenerationIntegration : ISdkIntegration | ||
{ | ||
private readonly ISceneManager _sceneManager; | ||
private readonly ISentryMonoBehaviour _sentryMonoBehaviour; | ||
|
||
public TraceGenerationIntegration(SentryMonoBehaviour sentryMonoBehaviour) : this(sentryMonoBehaviour, SceneManagerAdapter.Instance) | ||
{ } | ||
|
||
internal TraceGenerationIntegration(ISentryMonoBehaviour sentryMonoBehaviour, ISceneManager sceneManager) | ||
{ | ||
_sceneManager = sceneManager; | ||
_sentryMonoBehaviour = sentryMonoBehaviour; | ||
} | ||
|
||
public void Register(IHub hub, SentryOptions options) | ||
{ | ||
hub.ConfigureScope(UpdatePropagationContext); | ||
|
||
_sentryMonoBehaviour.ApplicationResuming += () => | ||
{ | ||
options.DiagnosticLogger?.LogDebug("Application resumed. Creating new Trace."); | ||
hub.ConfigureScope(UpdatePropagationContext); | ||
}; | ||
|
||
_sceneManager.ActiveSceneChanged += (_, _) => | ||
{ | ||
options.DiagnosticLogger?.LogDebug("Active Scene changed. Creating new Trace."); | ||
hub.ConfigureScope(UpdatePropagationContext); | ||
}; | ||
} | ||
|
||
private static void UpdatePropagationContext(Scope scope) => | ||
scope.SetPropagationContext(new SentryPropagationContext()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,16 @@ | |
|
||
namespace Sentry.Unity; | ||
|
||
internal interface ISentryMonoBehaviour | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm creating an internal interface to be able to mock this in the tests. |
||
{ | ||
event Action? ApplicationResuming; | ||
} | ||
|
||
/// <summary> | ||
/// Singleton and DontDestroyOnLoad setup. | ||
/// </summary> | ||
[AddComponentMenu("")] // Hides it from being added as a component in the inspector | ||
public partial class SentryMonoBehaviour : MonoBehaviour | ||
public partial class SentryMonoBehaviour : MonoBehaviour, ISentryMonoBehaviour | ||
{ | ||
private static SentryMonoBehaviour? _instance; | ||
public static SentryMonoBehaviour Instance | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
test/Sentry.Unity.Tests/TraceGenerationIntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using Sentry.Unity.Integrations; | ||
using Sentry.Unity.Tests.SharedClasses; | ||
using Sentry.Unity.Tests.Stubs; | ||
using static Sentry.Unity.Tests.SceneManagerIntegrationTests; | ||
|
||
namespace Sentry.Unity.Tests; | ||
|
||
public class TraceGenerationIntegrationTests | ||
{ | ||
private class Fixture | ||
{ | ||
public FakeSceneManager SceneManager { get; set; } = new(); | ||
public TestSentryMonoBehaviour SentryMonoBehaviour { get; set; } = new(); | ||
public TestHub TestHub { get; set; } = new(); | ||
public TestLogger Logger { get; set; } = new(); | ||
public SentryOptions SentryOptions { get; set; } | ||
|
||
public Fixture() => SentryOptions = new SentryOptions { DiagnosticLogger = Logger }; | ||
|
||
public TraceGenerationIntegration GetSut() => new(SentryMonoBehaviour, SceneManager); | ||
} | ||
|
||
private readonly Fixture _fixture = new(); | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_fixture.TestHub = new TestHub(); | ||
SentrySdk.UseHub(_fixture.TestHub); | ||
} | ||
|
||
[Test] | ||
public void TraceGeneration_OnRegister_GeneratesInitialTrace() | ||
{ | ||
// Arrange | ||
var sut = _fixture.GetSut(); | ||
|
||
// Act | ||
sut.Register(_fixture.TestHub, _fixture.SentryOptions); | ||
|
||
// Assert | ||
var configureScope = _fixture.TestHub.ConfigureScopeCalls.Single(); | ||
var scope = new Scope(_fixture.SentryOptions); | ||
var initialPropagationContext = scope.PropagationContext; | ||
configureScope(scope); | ||
|
||
Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext); | ||
} | ||
|
||
[Test] | ||
public void TraceGeneration_OnApplicationResume_GeneratesNewTrace() | ||
{ | ||
// Arrange | ||
var sut = _fixture.GetSut(); | ||
sut.Register(_fixture.TestHub, _fixture.SentryOptions); | ||
var initialCallsCount = _fixture.TestHub.ConfigureScopeCalls.Count; | ||
|
||
// Act | ||
_fixture.SentryMonoBehaviour.ResumeApplication(); | ||
|
||
// Assert | ||
// Calling 'Register' already generated a trace, so we expect 1+1 calls to ConfigureScope | ||
Assert.AreEqual(initialCallsCount + 1, _fixture.TestHub.ConfigureScopeCalls.Count); | ||
var configureScope = _fixture.TestHub.ConfigureScopeCalls.Last(); | ||
var scope = new Scope(_fixture.SentryOptions); | ||
var initialPropagationContext = scope.PropagationContext; | ||
configureScope(scope); | ||
Flash0ver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext); | ||
} | ||
|
||
[Test] | ||
public void TraceGeneration_OnActiveSceneChange_GeneratesNewTrace() | ||
{ | ||
// Arrange | ||
var sut = _fixture.GetSut(); | ||
sut.Register(_fixture.TestHub, _fixture.SentryOptions); | ||
var initialCallsCount = _fixture.TestHub.ConfigureScopeCalls.Count; | ||
|
||
// Act | ||
_fixture.SceneManager.OnActiveSceneChanged(new SceneAdapter("from scene name"), new SceneAdapter("to scene name")); | ||
|
||
// Assert | ||
// Calling 'Register' already generated a trace, so we expect 1+1 calls to ConfigureScope | ||
Assert.AreEqual(initialCallsCount + 1, _fixture.TestHub.ConfigureScopeCalls.Count); | ||
var configureScope = _fixture.TestHub.ConfigureScopeCalls.Last(); | ||
var scope = new Scope(_fixture.SentryOptions); | ||
var initialPropagationContext = scope.PropagationContext; | ||
configureScope(scope); | ||
|
||
Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext); | ||
} | ||
|
||
internal class TestSentryMonoBehaviour : ISentryMonoBehaviour | ||
{ | ||
public event Action? ApplicationResuming; | ||
|
||
public void ResumeApplication() => ApplicationResuming?.Invoke(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hub already logs which integration get registered. This is redundant.