sentry-unity
sentry-unity copied to clipboard
Improve SDK Initialization
The Problem
Currently there are multiple ways to initialize the SDK, none of them ideal. There are multiple Init methods while the SDK is auto-initializing. Users have no straight forward way to control or delay initialization.
Context
There are multiple ways to initialize the SDK.
SentrySdk.Init()
This is the entry point into the .NET SDK. It is discoverable and callable from anywhere inside the Unity project. The SDK calls this as part of SentryUnity.Init.
SentryUnity.Init()
This is the entry point into the Unity SDK.
https://github.com/getsentry/sentry-unity/blob/14882b7b53636e05ce995c8d7ba20c68d0a90f2e/src/Sentry.Unity/SentryUnity.cs#L31
The SDK is also using it as part of the auto-initialization through SentryInitialization
SentryInitialization.cs
The SDK provides a SentryInitialization.cs as .cs script so it has access to precompiler directives and other, targeted platform specifics like IL2CPP methods
https://github.com/getsentry/sentry-unity/blob/14882b7b53636e05ce995c8d7ba20c68d0a90f2e/package-dev/Runtime/SentryInitialization.cs#L54-L56
Due to the RuntimeInitializeOnLoadMethod there's currently no way around the SDK attempting to auto-initialize.
The flow is
- The options get loaded from
/Resources - The
ConfigureCallbackgets invoked, allowing user code to get executed https://github.com/getsentry/sentry-unity/blob/14882b7b53636e05ce995c8d7ba20c68d0a90f2e/package-dev/Runtime/SentryInitialization.cs#L60 - Native SDKs get setup/initialized (scope observer, background worker setup, e.t.c)
- https://github.com/getsentry/sentry-unity/blob/14882b7b53636e05ce995c8d7ba20c68d0a90f2e/package-dev/Runtime/SentryInitialization.cs#L88-L96
- The SDK calls
Inithttps://github.com/getsentry/sentry-unity/blob/14882b7b53636e05ce995c8d7ba20c68d0a90f2e/package-dev/Runtime/SentryInitialization.cs#L69
Proposal
Changes
- Unify all
Initinto one classSentryUnity. - Hide .NET SDK init - we do have access to the source due to submodule. We can hide it, or have it partial?
- Have one
Initmethod with theRuntimeInitializeOnLoadMethodthat checks foroption.Enabledand bails early. It calls 4. otherwise - Have a public
Initmethod that gets called by 4. that also does the native-sdk configuration. Add/keep appropriate overloads. - Put
UnityInfosomewhere accessible (i.e. the options, or have it onSentryUnityand have the options fetch it from there).
User flow
Automatic Initialization
Nothing changes. The runtime attribute still initializes the SDK. The configure callback still gets invoked, allowing users to make programmatic changes to the options without having to worry/care about initialization timing.
Programmatic Initialization
For users that want to delay or manually initialize they'd disable the SDK in the config window. The auto-initialization will bail early. At any point, they can then call SentryUnity.Init and the SDK will correctly handle native-sdk initialization and platform restriction.