Skip to content
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

Unity Sentry SDK programmatic setup #130

Merged
merged 23 commits into from
May 11, 2021
Merged

Conversation

semuserable
Copy link
Contributor

@semuserable semuserable commented Apr 23, 2021

Initial attempt of programmatic setup, there are a lot to cover.

Current:

  • Only one SentryUnity can exist, all subsequent inits will dispose old one and replace with a new one.

Example from SentryInitialization.Init:

// options from Sentry Window, DSN = https://94677106febe46b88b9b9ae5efd18a00@o447951.ingest.sentry.io/5439417
SentryUnity.Init(options);

Debug.LogError("Error!"); // sent to https://94677106febe46b88b9b9ae5efd18a00@o447951.ingest.sentry.io/5439417

// previous disposed, new one created
SentryUnity.Init(new UnitySentryOptions
{
    Enabled = true,
    Dsn = "YOUR_DSN"
});

Debug.LogError("Error next!"); // sent to 'YOUR_DSN'

All of the above are checked on il2cpp builds.

  • Tests rewritten. Now uses SentryUnity

Things to consider:

  • link.xml is not implemented for this approach yet
  • Unity options should be renamed to be consistent with Sentry dotnet SDKs
  • Should notify a user (more gracefully then now) that a previous SentryUnity is disposed (removed/destroyed) and a new one is created. My thoughts: retrieve call site (stack? frame? CallMember?) of the Init methods and store it somewhere, then check and compare when new Init happens -> notify user.
  • More use cases?

Let's discuss further steps.

src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/UnitySentryOptions.cs Outdated Show resolved Hide resolved
src/test/Sentry.Unity.Tests/PlayModeTests.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryInitialization.cs Show resolved Hide resolved
src/Sentry.Unity/Extensions/SentryOptionsExtensions.cs Outdated Show resolved Hide resolved
@semuserable
Copy link
Contributor Author

What do you think about this updated window?
image

Main points

  • Disable Programmatic Initialization
  • Compression level UI based on SentryOptions - System.IO.Compression.CompressionLevel

cc @bruno-garcia @bitsandfoxes

@semuserable
Copy link
Contributor Author

semuserable commented Apr 29, 2021

Checked InternalsVisibleTo on Sentry.Unity
image
( AssemblyInfo.cs(18, 12): [CS1726] Friend assembly reference 'Sentry.Unity' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.)

As Sentry is signed, we must also sign Sentry.Unity if we want to reuse internals.

cc @bruno-garcia

@semuserable semuserable marked this pull request as ready for review April 30, 2021 09:16
@semuserable
Copy link
Contributor Author

Green, yay!

@semuserable
Copy link
Contributor Author

Changes:

  • Sentry window updated
    image
    • Disable Programmatic Initialization opt-in programmatic init
    • Disable Auto Compress Payload now a flag, CompressionLevel reused from Sentry dotnet SDK, if not ticked -> NoCompression is selected
  • Sentry integrations use Hub instance, not static calls
  • Infra for more testability (IAppDomain, TestHub), new tests
  • Tests reorganized

CHANGELOG.md Show resolved Hide resolved
@@ -105,10 +107,16 @@ public void OnLostFocus()
// ReSharper disable once UnusedMember.Local
private void OnGUI()
{
GUILayout.Label(new GUIContent(GUIContent.none), EditorStyles.boldLabel);
Options.DisableProgrammaticInitialization = EditorGUILayout.BeginToggleGroup(
new GUIContent("Disable Programmatic Initialization", "Disable manual Sentry setup. Rely on SentryOptions config."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels backwards though.

Ideally the experience is defined via SentryWindow. In order to opt-out from the "auto init", we can have a checkbox:

"Initialize SDK programatically'. If the user selects that, the whole SentryWindow should be disabled. Or best: we hide everything.

Another option it to just rely on the Enable flag that' already there. If the user Disables it, it means SentryWindow won't be defining the options to initialize the SDK. The user is always free to call SentryUnity.Init.

I think the best approach is ideal. What are your thoughts? @semuserable @bitsandfoxes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried to make it like you described, that's why it feels backwards for now. I encountered some problems with the way Unity provides API for this enable/disable functionality. Probably need to take another shot at this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option it to just rely on the Enable flag that' already there. If the user Disables it, it means SentryWindow won't be defining the options to initialize the SDK. The user is always free to call SentryUnity.Init.

I think this is the way to go. Any reasons not to take this approach?

@@ -138,6 +142,15 @@ private void OnGUI()
"If not set, auto detects such as 'development', 'production' or 'editor'."),
Options.Environment);

GUILayout.Label(new GUIContent(GUIContent.none), EditorStyles.boldLabel);
Options.DisableAutoCompression = EditorGUILayout.BeginToggleGroup(
new GUIContent("Disable Auto Compress Payload", "Disable auto Sentry setup. Rely on SentryOptions config."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a bit confusing IMHO.

Can't we have a "fake" value called Auto and have that selected? And if a user changes to one of the other values we'll just store that instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was to use Sentry CompressionLevel as is for this window.

I can create another enum with the same structure as CompressionLevel, call it UnityCompressionLevel and do the proper mapping to CompressionLevel.

Do you prefer the second approach (with UnityCompressionLevel enum)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's simpler to have auto in the UI and not bother creating a new enum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like that

image
?

src/Sentry.Unity/Extensions/JsonExtensions.cs Show resolved Hide resolved
src/Sentry.Unity/Extensions/SentryOptionsExtensions.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryInitialization.cs Outdated Show resolved Hide resolved
src/test/Sentry.Unity.Tests/Stubs/TestHub.cs Outdated Show resolved Hide resolved
Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvements. Thanks

@@ -105,10 +107,16 @@ public void OnLostFocus()
// ReSharper disable once UnusedMember.Local
private void OnGUI()
{
GUILayout.Label(new GUIContent(GUIContent.none), EditorStyles.boldLabel);
Options.DisableProgrammaticInitialization = EditorGUILayout.BeginToggleGroup(
new GUIContent("Disable Programmatic Initialization", "Disable manual Sentry setup. Rely on SentryOptions config."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option it to just rely on the Enable flag that' already there. If the user Disables it, it means SentryWindow won't be defining the options to initialize the SDK. The user is always free to call SentryUnity.Init.

I think this is the way to go. Any reasons not to take this approach?

public UnityBeforeSceneLoadIntegration(IApplication? appDomain = null)
=> _application = appDomain ?? ApplicationAdapter.Instance;

public void Register(IHub hub, SentryOptions options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called by Sentry when the SDK is initialized.

THe integrations are supposed to call into Sentry whenever something happens. Like when the game changes scenes, we could add a new breadcrumb. And even change a tag. That would be an integration

src/Sentry.Unity/SentryInitialization.cs Outdated Show resolved Hide resolved
@bruno-garcia
Copy link
Member

We should discuss this on a call and wrap it up together

@semuserable
Copy link
Contributor Author

semuserable commented May 11, 2021

Updated with the latest (+ main merged).

cc @bruno-garcia let's review it together on a call and close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants