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

fix/compression level auto #160

Merged
merged 14 commits into from
May 12, 2021
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

pull_request:
paths:
- '.github/**/*.yaml'
- '.github/**/*.yml'
- 'src/**'
- 'test/**'
- 'sample/**'
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- SDK version format correction (#120)
- Auto compression option is part of drop down (no extra checkbox) (#160)

## 0.0.13

Expand Down
7 changes: 3 additions & 4 deletions src/Sentry.Unity.Editor/SentryWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ private void OnGUI()
Options.SampleRate = EditorGUILayout.Slider(
new GUIContent("Event Sample Rate", "What random sample rate to apply. 1.0 captures everything, 0.7 captures 70%."),
Options.SampleRate ?? 1.0f, 0.01f, 1);
Options.EnableAutoPayloadCompression = EditorGUILayout.Toggle(
new GUIContent("Compress Payload (Auto)", "The level of which to compress the Sentry event before sending to Sentry (Auto)."),
Options.EnableAutoPayloadCompression);
Options.RequestBodyCompressionLevel = (CompressionLevel)EditorGUILayout.EnumPopup(

Options.RequestBodyCompressionLevel = (CompressionLevelWithAuto)EditorGUILayout.EnumPopup(
new GUIContent("Compress Payload", "The level of which to compress the Sentry event before sending to Sentry."),
Options.RequestBodyCompressionLevel);

Options.AttachStacktrace = EditorGUILayout.Toggle(
new GUIContent("Stacktrace For Logs", "Whether to include a stack trace for non error events like logs. " +
"Even when Unity didn't include and no Exception was thrown.."),
Expand Down
19 changes: 15 additions & 4 deletions src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@

namespace Sentry.Unity
{
/// <summary>
/// Sentry Unity initialization class.
/// </summary>
public static class SentryUnity
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Init(SentryUnityOptions sentryUnityOptions)
=> SentrySdk.Init(sentryUnityOptions);

/// <summary>
/// Initializes Sentry Unity SDK while configuring the options.
/// </summary>
/// <param name="unitySentryOptionsConfigure">Callback to configure the options.</param>
public static void Init(Action<SentryUnityOptions> unitySentryOptionsConfigure)
{
var unitySentryOptions = new SentryUnityOptions();
unitySentryOptionsConfigure.Invoke(unitySentryOptions);
Init(unitySentryOptions);
}

/// <summary>
/// Initializes Sentry Unity SDK while providing an options object.
/// </summary>
/// <param name="unitySentryOptions">The options object.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Init(SentryUnityOptions unitySentryOptions)
=> SentrySdk.Init(unitySentryOptions);
}
}
60 changes: 49 additions & 11 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,30 @@ internal static string GetConfigPath(string? notDefaultConfigName = null)
public bool CaptureInEditor { get; set; } = true; // Lower entry barrier, likely set to false after initial setup.
public bool DebugOnlyInEditor { get; set; } = true;
public SentryLevel DiagnosticsLevel { get; set; } = SentryLevel.Error; // By default logs out Error or higher.
public bool EnableAutoPayloadCompression { get; set; }

private CompressionLevelWithAuto _requestBodyCompressionLevel = CompressionLevelWithAuto.Auto;

public new CompressionLevelWithAuto RequestBodyCompressionLevel
{
get => _requestBodyCompressionLevel;
set
{
_requestBodyCompressionLevel = value;
if (value == CompressionLevelWithAuto.Auto)
{
// TODO: If WebGL, then NoCompression, else .. optimize (e.g: adapt to platform)
// The target platform is known when building the player, so 'auto' should resolve there(here).
// Since some platforms don't support GZipping fallback: no compression.
base.RequestBodyCompressionLevel = CompressionLevel.NoCompression;
}
else
{
// Auto would result in -1 set if not treated before providing the options to the Sentry .NET SDK
// DeflateStream would throw System.ArgumentOutOfRangeException
base.RequestBodyCompressionLevel = (CompressionLevel)value;
}
}
}

public SentryUnityOptions()
{
Expand All @@ -48,12 +71,6 @@ public SentryUnityOptions()
// Uses the game `version` as Release unless the user defined one via the Options
Release ??= Application.version; // TODO: Should we move it out and use via IApplication something?

// The target platform is known when building the player, so 'auto' should resolve there.
// Since some platforms don't support GZipping fallback no no compression.
RequestBodyCompressionLevel = EnableAutoPayloadCompression
? CompressionLevel.NoCompression
: RequestBodyCompressionLevel;

Environment = Environment is { } environment
? environment
: Application.isEditor // TODO: Should we move it out and use via IApplication something?
Expand Down Expand Up @@ -96,8 +113,7 @@ public void WriteTo(Utf8JsonWriter writer)
writer.WriteNumber("diagnosticsLevel", (int)DiagnosticsLevel);
writer.WriteBoolean("attachStacktrace", AttachStacktrace);

writer.WriteBoolean("enableAutoPayloadCompression", EnableAutoPayloadCompression);
writer.WriteNumber("requestBodyCompressionLevel", EnableAutoPayloadCompression ? (int)CompressionLevel.NoCompression : (int)RequestBodyCompressionLevel);
writer.WriteNumber("requestBodyCompressionLevel", (int)RequestBodyCompressionLevel);

if (SampleRate != null)
{
Expand Down Expand Up @@ -127,8 +143,7 @@ public static SentryUnityOptions FromJson(JsonElement json)
Debug = json.GetPropertyOrNull("debug")?.GetBoolean() ?? true,
DebugOnlyInEditor = json.GetPropertyOrNull("debugOnlyInEditor")?.GetBoolean() ?? true,
DiagnosticsLevel = json.GetEnumOrNull<SentryLevel>("diagnosticsLevel") ?? SentryLevel.Error,
RequestBodyCompressionLevel = json.GetEnumOrNull<CompressionLevel>("requestBodyCompressionLevel") ?? CompressionLevel.NoCompression,
EnableAutoPayloadCompression = json.GetPropertyOrNull("enableAutoPayloadCompression")?.GetBoolean() ?? false,
RequestBodyCompressionLevel = json.GetEnumOrNull<CompressionLevelWithAuto>("requestBodyCompressionLevel") ?? CompressionLevelWithAuto.Auto,
AttachStacktrace = json.GetPropertyOrNull("attachStacktrace")?.GetBoolean() ?? false,
SampleRate = json.GetPropertyOrNull("sampleRate")?.GetSingle() ?? 1.0f,
Release = json.GetPropertyOrNull("release")?.GetString(),
Expand All @@ -150,4 +165,27 @@ public void SaveToUnity(string path)
WriteTo(writer);
}
}

/// <summary>
/// <see cref="CompressionLevel"/> with an additional value for Automatic
/// </summary>
public enum CompressionLevelWithAuto
{
/// <summary>
/// The Unity SDK will attempt to choose the best option for the target player.
/// </summary>
Auto = -1,
/// <summary>
/// The compression operation should be optimally compressed, even if the operation takes a longer time (and CPU) to complete.
/// </summary>
Optimal = CompressionLevel.Optimal,
/// <summary>
/// The compression operation should complete as quickly as possible, even if the resulting data is not optimally compressed.
/// </summary>
Fastest = CompressionLevel.Fastest,
/// <summary>
/// No compression should be performed.
/// </summary>
NoCompression = CompressionLevel.NoCompression,
}
}
5 changes: 5 additions & 0 deletions test/Sentry.Unity.Tests/Stubs/TestApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ internal sealed class TestApplication : IApplication
public event Application.LogCallback? LogMessageReceived;
public event Action? Quitting;
public string ActiveSceneName => "TestSceneName";

private void OnQuitting() => Quitting?.Invoke();

private void OnLogMessageReceived(string condition, string stacktrace, LogType type)
=> LogMessageReceived?.Invoke(condition, stacktrace, type);
}
}
6 changes: 2 additions & 4 deletions test/Sentry.Unity.Tests/UnitySentryOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public void Options_WriteRead_Equals()
var optionsExpected = new SentryUnityOptions
{
Enabled = true,
Dsn = "http://test.com",
Dsn = "https://test.com",
CaptureInEditor = true,
Debug = true,
DebugOnlyInEditor = false,
DiagnosticsLevel = SentryLevel.Info,
EnableAutoPayloadCompression = false,
RequestBodyCompressionLevel = CompressionLevel.NoCompression,
RequestBodyCompressionLevel = CompressionLevelWithAuto.NoCompression,
AttachStacktrace = true,
SampleRate = 1f,
Release = "release",
Expand Down Expand Up @@ -68,7 +67,6 @@ private static void AssertOptions(SentryUnityOptions actual, SentryUnityOptions
Assert.AreEqual(expected.SampleRate, actual.SampleRate);
Assert.AreEqual(expected.Release, actual.Release);
Assert.AreEqual(expected.Environment, actual.Environment);
Assert.AreEqual(expected.EnableAutoPayloadCompression, actual.EnableAutoPayloadCompression);
Assert.AreEqual(expected.RequestBodyCompressionLevel, actual.RequestBodyCompressionLevel);
}

Expand Down