sentry-unity icon indicating copy to clipboard operation
sentry-unity copied to clipboard

Allow for a delayed or manual SDK initialization

Open bitsandfoxes opened this issue 6 months ago • 0 comments
trafficstars

Problem Statement

Currently, there is no straight forward way to delay the SDK's initialization. In addition, while manual initialization is possible, it might come with some hurdles.

Context

The Unity SDK relies on configuration provided within the Editor that is saved to /Resources as a .asset file.

The SDK compiles SentryInitialization.cs with your game. This way it has access to preprocessor directives.

https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/package-dev/Runtime/SentryInitialization.cs#L54-L56

Part of loading the options is invoking the user's configuration callback.

https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/package-dev/Runtime/SentryInitialization.cs#L60

https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/src/Sentry.Unity/ScriptableSentryUnityOptions.cs#L214

So while you can call SentryUnity.Init, users are missing out on the native SDK setup (which includes init and scope sync) https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/package-dev/Runtime/SentryInitialization.cs#L68

So in all practicality, manually calling call SentryUnity.Init has a very limited usecase.

Proposal

Users should have the option to control automatic initialization. This could be a separate option in addition to Enabled. Users should have the option to manually call Init the same way the SDK does.

Workaround

  1. Setup programmatic configuration
  2. Have the options.Enabled resolve to false based on your condition for the delay
  3. The SDK will attempt to auto-initialize and because of 2. skip
  4. When you're ready can call SentryInitialization.Init yourself
  5. The SDK will load the options again
  6. The SDK will invoke the options configuration callback again
  7. This time, you should have options.Enabled resolve to true
  8. The SDK will do the rest of the initialization

** Note, that on mobile platforms, for this workaround it is recommended to have the options.InitializationType set to Runtime as otherwise, the SDK will close down the native SDK during the first initialization attempt.

I know, this is not a pretty solution but at least you're not blocked.

bitsandfoxes avatar May 06 '25 13:05 bitsandfoxes