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

feat: Add OptionConfiguration to replace Runtime/BuildTime #1888

Open
wants to merge 24 commits into
base: feat/create-cli-config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- The SDK now has a singular `OptionsConfiguration` script that will be used both at runtime and build time. This allows you to programmatically configure the SDK in one place using precompile directives. Note, that the current limitation of options for Android and iOS being baked into the project at build time still remains. ([#1888](https://github.com/getsentry/sentry-unity/pull/1888))
- The SDK now provides a dedicated sentry-cli scriptable config, available on the `Debug Symbols` tab. This allows for programmatic configuration of the used cli-options during build. ([#1887](https://github.com/getsentry/sentry-unity/pull/1887))

### Fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 66c22ade0547549a9bfd67d33dd2f26a, type: 3}
m_Name: SentryOptionConfiguration
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions samples/unity-of-bugs/Assets/Resources/Sentry/SentryOptions.asset
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ MonoBehaviour:
m_Name: SentryOptions
m_EditorClassIdentifier:
<Enabled>k__BackingField: 1
<Dsn>k__BackingField: https://[email protected]/4504604988538880
<Dsn>k__BackingField: https://[email protected].us.sentry.io/4504604988538880
<CaptureInEditor>k__BackingField: 1
<EnableLogDebouncing>k__BackingField: 0
<DebounceTimeLog>k__BackingField: 1000
<DebounceTimeWarning>k__BackingField: 1000
<DebounceTimeError>k__BackingField: 1000
<TracesSampleRate>k__BackingField: 1
<TracesSampleRate>k__BackingField: 0
<AutoStartupTraces>k__BackingField: 1
<AutoSceneLoadTraces>k__BackingField: 1
<AutoAwakeTraces>k__BackingField: 1
<AutoAwakeTraces>k__BackingField: 0
<AutoSessionTracking>k__BackingField: 1
<AutoSessionTrackingInterval>k__BackingField: 30000
<ReleaseOverride>k__BackingField:
<EnvironmentOverride>k__BackingField:
<AttachStacktrace>k__BackingField: 0
<AttachScreenshot>k__BackingField: 1
<ScreenshotQuality>k__BackingField: 2
<AttachScreenshot>k__BackingField: 0
<ScreenshotQuality>k__BackingField: 1
<ScreenshotCompression>k__BackingField: 75
<AttachViewHierarchy>k__BackingField: 1
<AttachViewHierarchy>k__BackingField: 0
<MaxViewHierarchyRootObjects>k__BackingField: 100
<MaxViewHierarchyObjectChildCount>k__BackingField: 20
<MaxViewHierarchyDepth>k__BackingField: 10
Expand All @@ -46,25 +46,30 @@ MonoBehaviour:
<IsEnvironmentUser>k__BackingField: 0
<EnableOfflineCaching>k__BackingField: 1
<MaxCacheItems>k__BackingField: 30
<InitCacheFlushTimeout>k__BackingField: 2000
<InitCacheFlushTimeout>k__BackingField: 0
<SampleRate>k__BackingField: 1
<ShutdownTimeout>k__BackingField: 2000
<MaxQueueItems>k__BackingField: 30
<AnrDetectionEnabled>k__BackingField: 1
<AnrTimeout>k__BackingField: 5000
<CaptureFailedRequests>k__BackingField: 1
<FailedRequestStatusCodes>k__BackingField: f401000057020000
<FilterBadGatewayExceptions>k__BackingField: 1
<FilterWebExceptions>k__BackingField: 1
<FilterSocketExceptions>k__BackingField: 1
<IosNativeSupportEnabled>k__BackingField: 1
<AndroidNativeSupportEnabled>k__BackingField: 1
<NdkIntegrationEnabled>k__BackingField: 1
<NdkScopeSyncEnabled>k__BackingField: 1
<PostGenerateGradleProjectCallbackOrder>k__BackingField: 1
<WindowsNativeSupportEnabled>k__BackingField: 1
<MacosNativeSupportEnabled>k__BackingField: 1
<LinuxNativeSupportEnabled>k__BackingField: 1
<Il2CppLineNumberSupportEnabled>k__BackingField: 1
<RuntimeOptionsConfiguration>k__BackingField: {fileID: 11400000, guid: f9a47e8442a604414a84d5ad6da6aa43,
type: 2}
<BuildTimeOptionsConfiguration>k__BackingField: {fileID: 11400000, guid: 1c61999bd2a61408393cfe218d42287e,
<RuntimeOptionsConfiguration>k__BackingField: {fileID: 0}
<BuildTimeOptionsConfiguration>k__BackingField: {fileID: 0}
<OptionsConfiguration>k__BackingField: {fileID: 11400000, guid: a25a5a0961ec2473dafd8d49df35a927,
type: 2}
<Debug>k__BackingField: 1
<DebugOnlyInEditor>k__BackingField: 0
<DiagnosticLevel>k__BackingField: 0
<DebugOnlyInEditor>k__BackingField: 1
<DiagnosticLevel>k__BackingField: 2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Sentry;
using Sentry.Unity;
using UnityEngine;

public class SentryOptionConfiguration : SentryOptionsConfiguration
{
public override void Configure(SentryUnityOptions options)
{
// Here you can programmatically modify the Sentry option properties used for the SDK's initialization

#if UNITY_ANDROID || UNITY_IOS
// Important!
// Android and iOS options are getting validated and set at build time. Changes to them while the game
// On Android and iOS, ALL options configured here will be "baked" into the exported project during
// build time. Any runtime changes to these options will not take effect.

// Works as expected and will disable all debug logging of the native SDKs
// options.Debug = false;

// Will NOT work as expected as this will need to get validated at runtime
// options.Debug = SystemInfo.deviceName.Contains("Pixel");
#endif
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ internal static class OptionsConfigurationItem

public static T? Display<T>(T? value, string fieldName, string scriptName, string toolTip) where T : ScriptableObject
{
GUILayout.BeginHorizontal();
EditorGUILayout.BeginHorizontal();

var result = EditorGUILayout.ObjectField(
new GUIContent(fieldName, toolTip),
value,
typeof(T),
false
) as T;

if (GUILayout.Button("New", GUILayout.ExpandWidth(false)))
{
var t = typeof(T);
if (t == typeof(SentryRuntimeOptionsConfiguration) || t == typeof(SentryBuildTimeOptionsConfiguration))
{
CreateDeprecatedConfigurationScript<T>(fieldName, scriptName);
}
else if (t == typeof(SentryOptionsConfiguration))
{
CreateConfigurationScript(fieldName, SentryOptionsConfiguration.Template, scriptName);
}
else if (t == typeof(SentryCliOptionsConfiguration))
{
CreateConfigurationScript(fieldName, SentryCliOptionsConfiguration.Template, scriptName);
Expand All @@ -36,7 +42,9 @@ internal static class OptionsConfigurationItem
throw new Exception("Attempting to create a new instance of unsupported type " + typeof(T).FullName);
}
}
GUILayout.EndHorizontal();

EditorGUILayout.EndHorizontal();

return result;
}

Expand Down Expand Up @@ -177,6 +185,9 @@ internal static void SetScript(string scriptNameWithoutExtension)
case SentryBuildTimeOptionsConfiguration buildTimeConfiguration:
options.BuildTimeOptionsConfiguration ??= buildTimeConfiguration; // Don't overwrite if already set
break;
case SentryOptionsConfiguration configuration:
options.OptionsConfiguration ??= configuration; // Don't overwrite if already set
break;
case SentryCliOptionsConfiguration cliConfiguration:
cliOptions.CliOptionsConfiguration ??= cliConfiguration; // Don't overwrite if already set
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

Expand All @@ -11,41 +9,49 @@ public static void Display(ScriptableSentryUnityOptions options)
{
GUILayout.Label("Scriptable Options Configuration", EditorStyles.boldLabel);

EditorGUILayout.Space();

EditorGUILayout.HelpBox(
"The scriptable options configuration allows you to programmatically modify Sentry options." +
"\n" +
"\n" +
"You can use the 'Runtime Configuration Script' to modify options just before Sentry SDK gets " +
"initialized. This allows you to access options and functionality otherwise unavailable from the " +
"Editor UI, e.g. set a custom BeforeSend callback." +
"\n" +
"\n" +
"Use the 'Build Time Configuration Script' in case you need to change build-time behavior, " +
"e.g. specify custom Sentry-CLI options or change settings for native SDKs that start before the " +
"managed layer does (such as Android, iOS, macOS).",
MessageType.Info);

EditorGUILayout.HelpBox("Clicking the 'New' button will prompt you with selecting a location for " +
"your custom 'SentryConfiguration' script and automatically " +
"create a new asset instance.", MessageType.Info);

EditorGUILayout.Space();

EditorGUILayout.BeginVertical("box");

EditorGUILayout.HelpBox("The Runtime/BuildTime scriptable objects have been deprecated and will be removed in a future version." +
"\nPlease use the 'Option Config Script' below." +
"\nInstead of implementing your configuration in two places you can control the options via precompile directives.",
MessageType.Warning);

options.RuntimeOptionsConfiguration = OptionsConfigurationItem.Display(
options.RuntimeOptionsConfiguration,
"Runtime Configuration Script",
"Runtime Config Script",
"SentryRuntimeConfiguration",
"A scriptable object that inherits from 'ScriptableOptionsConfiguration' " +
"DEPRECATED: A scriptable object that inherits from 'ScriptableOptionsConfiguration' " +
"and allows you to programmatically modify Sentry options."
);

options.BuildTimeOptionsConfiguration = OptionsConfigurationItem.Display(
options.BuildTimeOptionsConfiguration,
"Build Time Configuration Script",
"Build Time Config Script",
"SentryBuildTimeConfiguration",
"A scriptable object that inherits from 'ScriptableOptionsConfiguration' " +
"DEPRECATED: A scriptable object that inherits from 'ScriptableOptionsConfiguration' " +
"and allows you to programmatically modify Sentry options."
);

EditorGUILayout.EndVertical();

EditorGUILayout.Space();

EditorGUILayout.BeginVertical("box");

EditorGUILayout.HelpBox("The 'Option Config Script' allows you to programmatically configure and " +
"modify the options used by the Sentry SDK.", MessageType.Info);

options.OptionsConfiguration = OptionsConfigurationItem.Display(
options.OptionsConfiguration,
"Option Config Script",
"SentryOptionConfiguration",
"A scriptable object that inherits from 'SentryOptionsConfiguration' " +
"and allows you to programmatically modify Sentry options."
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ private static void ConfigureOptions(Dictionary<string, string> args, [CallerMem
Debug.LogFormat("{0}: Found SentryOptions", functionName);

var value = "";
if (args.TryGetValue("runtimeOptionsScript", out value))
if (args.TryGetValue("deprecatedRuntimeOptionsScript", out value))
{
Debug.LogFormat("{0}: Configuring Runtime Options Script to {1}", functionName, value);
OptionsConfigurationItem.SetScript(value);
}

if (args.TryGetValue("buildTimeOptionsScript", out value))
if (args.TryGetValue("deprecatedBuildTimeOptionsScript", out value))
{
Debug.LogFormat("{0}: Configuring Build Time Options Script to {1}", functionName, value);
OptionsConfigurationItem.SetScript(value);
}

if (args.TryGetValue("optionsScript", out value))
{
Debug.LogFormat("{0}: Configuring Build Time Options Script to {1}", functionName, value);
OptionsConfigurationItem.SetScript(value);
Expand All @@ -51,6 +57,12 @@ private static void ConfigureOptions(Dictionary<string, string> args, [CallerMem
cliOptions.UrlOverride = value;
}

if (args.TryGetValue("cliOptionsScript", out value))
{
Debug.LogFormat("{0}: Configuring CliOptions Script to {1}", functionName, value);
OptionsConfigurationItem.SetScript(value);
}

optionsWindow.Close();
Debug.LogFormat("{0}: SUCCESS", functionName);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Sentry.Unity.Editor/SentryScriptableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ internal static (SentryUnityOptions?, SentryCliOptions?) ConfiguredBuildTimeOpti
if (scriptableOptions is not null)
{
options = scriptableOptions.ToSentryUnityOptions(isBuilding: true, unityInfo: null);

// TODO: Move this into `Load` once we remove Runtime- and BuildTimeConfig
// We're calling `Configure` here and not in `Load` so the new Config does not overwrite the BuildTimeConfig
cliOptions?.CliOptionsConfiguration?.Configure(cliOptions);
// Must be non-nullable in the interface otherwise Unity script compilation fails...
cliOptions ??= ScriptableObject.CreateInstance<SentryCliOptions>();
var setupScript = scriptableOptions.BuildTimeOptionsConfiguration;
if (setupScript != null)

var deprecatedConfiguration = scriptableOptions.BuildTimeOptionsConfiguration;
if (deprecatedConfiguration != null)
{
setupScript.Configure(options, cliOptions);
deprecatedConfiguration.Configure(options, cliOptions);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Sentry.Unity/ScriptableSentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static string GetConfigPath(string? notDefaultConfigName = null)

[field: SerializeField] public SentryRuntimeOptionsConfiguration? RuntimeOptionsConfiguration { get; set; }
[field: SerializeField] public SentryBuildTimeOptionsConfiguration? BuildTimeOptionsConfiguration { get; set; }
[field: SerializeField] public SentryOptionsConfiguration? OptionsConfiguration { get; set; }

[field: SerializeField] public bool Debug { get; set; } = true;
[field: SerializeField] public bool DebugOnlyInEditor { get; set; } = true;
Expand Down Expand Up @@ -174,6 +175,7 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn
IosNativeSupportEnabled = IosNativeSupportEnabled,
AndroidNativeSupportEnabled = AndroidNativeSupportEnabled,
NdkIntegrationEnabled = NdkIntegrationEnabled,
NdkScopeSyncEnabled = NdkScopeSyncEnabled,
WindowsNativeSupportEnabled = WindowsNativeSupportEnabled,
MacosNativeSupportEnabled = MacosNativeSupportEnabled,
LinuxNativeSupportEnabled = LinuxNativeSupportEnabled,
Expand Down Expand Up @@ -206,6 +208,9 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn

options.SetupLogging();

OptionsConfiguration?.Configure(options);

// TODO: Deprecated and to be removed in the next major
if (RuntimeOptionsConfiguration != null && !isBuilding)
{
// This has to happen in between options object creation and updating the options based on programmatic changes
Expand Down
39 changes: 39 additions & 0 deletions src/Sentry.Unity/SentryOptionsConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using UnityEngine;

namespace Sentry.Unity;

public abstract class SentryOptionsConfiguration : ScriptableObject
{
public static readonly string Template =
"""
using Sentry;
using Sentry.Unity;

public class {{ScriptName}} : SentryOptionsConfiguration
{
public override void Configure(SentryUnityOptions options)
{
// Here you can programmatically modify the Sentry option properties used for the SDK's initialization

#if UNITY_ANDROID || UNITY_IOS
// Important!
// On Android and iOS, ALL options configured here will be "baked" into the exported project during
// build time. Any runtime changes to these options will not take effect.

// Examples:

// Works as expected and will disable all debug logging of the native SDKs
// options.Debug = false;

// Will NOT work as expected as this will need to get validated at runtime
// options.Debug = SystemInfo.deviceName.Contains("Pixel");
#endif
}
}
""";

/// <summary>
/// Called during build and during the game's startup to configure the options used to initialize the SDK
/// </summary>
public abstract void Configure(SentryUnityOptions options);
}
Loading
Loading