Skip to content

Commit e35d211

Browse files
authored
chore: Added comments to SentryInitialization.cs (#2024)
1 parent a3f3a2e commit e35d211

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

package-dev/Runtime/SentryInitialization.cs

+57-44
Original file line numberDiff line numberDiff line change
@@ -55,63 +55,76 @@ public static class SentryInitialization
5555
#endif
5656
public static void Init()
5757
{
58-
var sentryUnityInfo = new SentryUnityInfo();
59-
var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions(sentryUnityInfo);
58+
var unityInfo = new SentryUnityInfo();
59+
// Loading the options invokes the ScriptableOption`Configure` callback. Users can disable the SDK via code.
60+
var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions(unityInfo);
6061
if (options != null && options.ShouldInitializeSdk())
6162
{
63+
// Certain integrations require access to preprocessor directives so we provide them as `.cs` and
64+
// compile them with the game instead of precompiling them with the rest of the SDK.
65+
// i.e. SceneManagerAPI requires UNITY_2020_3_OR_NEWER
6266
SentryIntegrations.Configure(options);
67+
// Configures scope sync and (by default) initializes the native SDK.
68+
SetupNativeSdk(options, unityInfo);
69+
SentryUnity.Init(options);
70+
SetupStartupTracing(options);
71+
}
72+
else
73+
{
74+
// If the SDK is not `enabled` we're closing down the native layer as well. This is especially relevant
75+
// in a `built-time-initialization` scenario where the native SDKs self-initialize.
76+
#if SENTRY_NATIVE_COCOA
77+
SentryNativeCocoa.Close(options, unityInfo);
78+
#elif SENTRY_NATIVE_ANDROID
79+
SentryNativeAndroid.Close(options, unityInfo);
80+
#endif
81+
}
82+
}
6383

64-
try
65-
{
84+
private static void SetupNativeSdk(SentryUnityOptions options, SentryUnityInfo unityInfo)
85+
{
86+
try
87+
{
6688
#if SENTRY_NATIVE_COCOA
67-
SentryNativeCocoa.Configure(options, sentryUnityInfo);
89+
SentryNativeCocoa.Configure(options, unityInfo);
6890
#elif SENTRY_NATIVE_ANDROID
69-
SentryNativeAndroid.Configure(options, sentryUnityInfo);
91+
SentryNativeAndroid.Configure(options, unityInfo);
7092
#elif SENTRY_NATIVE
71-
SentryNative.Configure(options, sentryUnityInfo);
93+
SentryNative.Configure(options, unityInfo);
7294
#elif SENTRY_WEBGL
73-
SentryWebGL.Configure(options);
95+
SentryWebGL.Configure(options);
7496
#endif
75-
}
76-
catch (DllNotFoundException e)
77-
{
78-
options.DiagnosticLogger?.LogError(e,
79-
"Sentry native-error capture configuration failed to load a native library. This usually " +
80-
"means the library is missing from the application bundle or the installation directory.");
81-
}
82-
catch (Exception e)
83-
{
84-
options.DiagnosticLogger?.LogError(e, "Sentry native error capture configuration failed.");
85-
}
86-
87-
SentryUnity.Init(options);
97+
}
98+
catch (DllNotFoundException e)
99+
{
100+
options.DiagnosticLogger?.LogError(e,
101+
"Sentry native-error capture configuration failed to load a native library. This usually " +
102+
"means the library is missing from the application bundle or the installation directory.");
103+
}
104+
catch (Exception e)
105+
{
106+
options.DiagnosticLogger?.LogError(e, "Sentry native error capture configuration failed.");
107+
}
108+
}
88109

110+
private static void SetupStartupTracing(SentryUnityOptions options)
111+
{
89112
#if !SENTRY_WEBGL
90-
if (options.TracesSampleRate > 0.0f && options.AutoStartupTraces)
91-
{
92-
options.DiagnosticLogger?.LogInfo("Creating '{0}' transaction for runtime initialization.",
93-
StartupTransactionOperation);
94-
95-
var runtimeStartTransaction =
96-
SentrySdk.StartTransaction("runtime.initialization", StartupTransactionOperation);
97-
SentrySdk.ConfigureScope(scope => scope.Transaction = runtimeStartTransaction);
98-
99-
options.DiagnosticLogger?.LogDebug("Creating '{0}' span.", InitSpanOperation);
100-
InitSpan = runtimeStartTransaction.StartChild(InitSpanOperation, "runtime initialization");
101-
options.DiagnosticLogger?.LogDebug("Creating '{0}' span.", SubSystemSpanOperation);
102-
SubSystemRegistrationSpan = InitSpan.StartChild(SubSystemSpanOperation, "subsystem registration");
103-
}
104-
#endif
105-
}
106-
else
113+
if (options.TracesSampleRate > 0.0f && options.AutoStartupTraces)
107114
{
108-
// Closing down the native layer that are set up during build and self-initialize
109-
#if SENTRY_NATIVE_COCOA
110-
SentryNativeCocoa.Close(options, sentryUnityInfo);
111-
#elif SENTRY_NATIVE_ANDROID
112-
SentryNativeAndroid.Close(options, sentryUnityInfo);
113-
#endif
115+
options.DiagnosticLogger?.LogInfo("Creating '{0}' transaction for runtime initialization.",
116+
StartupTransactionOperation);
117+
118+
var runtimeStartTransaction =
119+
SentrySdk.StartTransaction("runtime.initialization", StartupTransactionOperation);
120+
SentrySdk.ConfigureScope(scope => scope.Transaction = runtimeStartTransaction);
121+
122+
options.DiagnosticLogger?.LogDebug("Creating '{0}' span.", InitSpanOperation);
123+
InitSpan = runtimeStartTransaction.StartChild(InitSpanOperation, "runtime initialization");
124+
options.DiagnosticLogger?.LogDebug("Creating '{0}' span.", SubSystemSpanOperation);
125+
SubSystemRegistrationSpan = InitSpan.StartChild(SubSystemSpanOperation, "subsystem registration");
114126
}
127+
#endif
115128
}
116129
}
117130

0 commit comments

Comments
 (0)