-
-
Notifications
You must be signed in to change notification settings - Fork 56
chore: Move startup tracing into the SDK #2234
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
+378
−171
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad0ae02
move startup tracing into the SDK
bitsandfoxes bb6f80a
.
bitsandfoxes c53ccb5
fix naming
bitsandfoxes d100d4f
Format code
getsentry-bot 7faafb9
added tests
bitsandfoxes 490e033
merged
bitsandfoxes 2f42440
Format code
getsentry-bot d2709d1
updated snapshot
bitsandfoxes 326dc32
Merge branch 'chore/cleanup-init' of https://github.com/getsentry/sen…
bitsandfoxes 1183370
.
bitsandfoxes 2e4b3db
Merge branch 'main' into chore/cleanup-init
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 was deleted.
Oops, something went wrong.
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
139 changes: 139 additions & 0 deletions
139
src/Sentry.Unity/Integrations/StartupTracingIntegration.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,139 @@ | ||
using Sentry.Extensibility; | ||
using Sentry.Integrations; | ||
using UnityEngine; | ||
|
||
namespace Sentry.Unity.Integrations; | ||
|
||
internal class StartupTracingIntegration : ISdkIntegration | ||
{ | ||
private const string StartupTransactionOperation = "app.start"; | ||
internal static ISpan? InitSpan; | ||
private const string InitSpanOperation = "runtime.init"; | ||
internal static ISpan? SubSystemRegistrationSpan; | ||
private const string SubSystemSpanOperation = "runtime.init.subsystem"; | ||
internal static ISpan? AfterAssembliesSpan; | ||
private const string AfterAssembliesSpanOperation = "runtime.init.afterassemblies"; | ||
internal static ISpan? SplashScreenSpan; | ||
private const string SplashScreenSpanOperation = "runtime.init.splashscreen"; | ||
internal static ISpan? FirstSceneLoadSpan; | ||
private const string FirstSceneLoadSpanOperation = "runtime.init.firstscene"; | ||
|
||
internal static bool IsGameStartupFinished; // Flag to make sure we only create spans during the game's startup. | ||
internal static bool IsIntegrationRegistered; | ||
|
||
private static IDiagnosticLogger? Logger; | ||
|
||
// For testing. Methods with the RuntimeLoad attribute cannot have arguments | ||
internal static IApplication? Application = null; | ||
|
||
public void Register(IHub hub, SentryOptions options) | ||
{ | ||
Logger = options.DiagnosticLogger; | ||
|
||
if (options is SentryUnityOptions { TracesSampleRate: > 0, AutoStartupTraces: true }) | ||
{ | ||
IsIntegrationRegistered = true; | ||
} | ||
} | ||
|
||
internal static bool IsStartupTracingAllowed() | ||
{ | ||
Application ??= ApplicationAdapter.Instance; | ||
if (!Application.IsEditor | ||
&& Application.Platform != RuntimePlatform.WebGLPlayer // Startup Tracing does not properly work on WebGL | ||
&& IsIntegrationRegistered | ||
&& !IsGameStartupFinished) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static void StartTracing() | ||
{ | ||
if (!IsStartupTracingAllowed()) | ||
{ | ||
return; | ||
} | ||
|
||
Logger?.LogInfo("Creating '{0}' transaction for runtime initialization.", | ||
StartupTransactionOperation); | ||
|
||
var runtimeStartTransaction = | ||
SentrySdk.StartTransaction("runtime.initialization", StartupTransactionOperation); | ||
SentrySdk.ConfigureScope(scope => scope.Transaction = runtimeStartTransaction); | ||
|
||
Logger?.LogDebug("Creating '{0}' span.", InitSpanOperation); | ||
InitSpan = runtimeStartTransaction.StartChild(InitSpanOperation, "runtime initialization"); | ||
Logger?.LogDebug("Creating '{0}' span.", SubSystemSpanOperation); | ||
SubSystemRegistrationSpan = InitSpan.StartChild(SubSystemSpanOperation, "subsystem registration"); | ||
} | ||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] | ||
public static void AfterAssembliesLoaded() | ||
{ | ||
if (!IsStartupTracingAllowed()) | ||
{ | ||
return; | ||
} | ||
|
||
SubSystemRegistrationSpan?.Finish(SpanStatus.Ok); | ||
SubSystemRegistrationSpan = null; | ||
|
||
Logger?.LogDebug("Creating '{0}' span.", AfterAssembliesSpanOperation); | ||
AfterAssembliesSpan = InitSpan?.StartChild(AfterAssembliesSpanOperation, "after assemblies"); | ||
} | ||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)] | ||
public static void BeforeSplashScreen() | ||
{ | ||
if (!IsStartupTracingAllowed()) | ||
{ | ||
return; | ||
} | ||
|
||
AfterAssembliesSpan?.Finish(SpanStatus.Ok); | ||
AfterAssembliesSpan = null; | ||
|
||
Logger?.LogDebug("Creating '{0}' span.", SplashScreenSpanOperation); | ||
SplashScreenSpan = InitSpan?.StartChild(SplashScreenSpanOperation, "splashscreen"); | ||
} | ||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
public static void BeforeSceneLoad() | ||
{ | ||
if (!IsStartupTracingAllowed()) | ||
{ | ||
return; | ||
} | ||
|
||
SplashScreenSpan?.Finish(SpanStatus.Ok); | ||
SplashScreenSpan = null; | ||
|
||
Logger?.LogDebug("Creating '{0}' span.", FirstSceneLoadSpanOperation); | ||
FirstSceneLoadSpan = InitSpan?.StartChild(FirstSceneLoadSpanOperation, "first scene load"); | ||
} | ||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] | ||
public static void AfterSceneLoad() | ||
{ | ||
if (!IsStartupTracingAllowed()) | ||
{ | ||
// To make sure late init calls don't try to trace the startup | ||
IsGameStartupFinished = true; | ||
return; | ||
} | ||
|
||
FirstSceneLoadSpan?.Finish(SpanStatus.Ok); | ||
FirstSceneLoadSpan = null; | ||
|
||
InitSpan?.Finish(SpanStatus.Ok); | ||
InitSpan = null; | ||
|
||
Logger?.LogInfo("Finishing '{0}' transaction.", StartupTransactionOperation); | ||
SentrySdk.ConfigureScope(s => s.Transaction?.Finish(SpanStatus.Ok)); | ||
|
||
IsGameStartupFinished = true; | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
Instead of having Startup Tracing be split between
SentryInitialization
andSentryIntegration
we can delete the latter and move everything intointernal StartupTracing
inside the SDK.