Skip to content

feat: Added ANR options #1181

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

Merged
merged 8 commits into from
Feb 22, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Preventing `LoggingIntegration` from registering multiple times ([#1178](https://github.com/getsentry/sentry-unity/pull/1178))
- Fixed the logging integration only capturing tags and missing the message ([#1150](https://github.com/getsentry/sentry-unity/pull/1150))

### Features

- Added ANR options to the editor window and made ANR timeout accessible on the options object ([#1181](https://github.com/getsentry/sentry-unity/pull/1181))

### Dependencies

- Bump Java SDK from v6.12.1 to v6.14.0 ([#1156](https://github.com/getsentry/sentry-unity/pull/1156), [#1171](https://github.com/getsentry/sentry-unity/pull/1171), [#1184](https://github.com/getsentry/sentry-unity/pull/1184))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ MonoBehaviour:
<MacosNativeSupportEnabled>k__BackingField: 1
<LinuxNativeSupportEnabled>k__BackingField: 1
<Il2CppLineNumberSupportEnabled>k__BackingField: 1
<AnrDetectionEnabled>k__BackingField: 1
<AnrTimeout>k__BackingField: 5000
<OptionsConfiguration>k__BackingField: {fileID: 11400000, guid: 407b4d5f2fef2c845bf2a3dcdf6b00fb,
type: 2}
<BuildtimeOptionsConfiguration>k__BackingField: {fileID: 11400000, guid: 6bcc81a646c08ce439ab03805d477458,
Expand Down
148 changes: 88 additions & 60 deletions src/Sentry.Unity.Editor/ConfigurationWindow/AdvancedTab.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEditor;
using UnityEngine;

Expand All @@ -7,87 +8,114 @@ internal static class AdvancedTab
{
internal static void Display(ScriptableSentryUnityOptions options, SentryCliOptions? cliOptions)
{
options.Debug = EditorGUILayout.BeginToggleGroup(
new GUIContent("Enable Debug Output", "Whether the Sentry SDK should print its " +
"diagnostic logs to the console."),
options.Debug);

options.DebugOnlyInEditor = EditorGUILayout.Toggle(
new GUIContent("Only In Editor", "Only print logs when in the editor. Development " +
"builds of the player will not include Sentry's SDK diagnostics."),
options.DebugOnlyInEditor);
{
options.Debug = EditorGUILayout.BeginToggleGroup(
new GUIContent("Enable Debug Output", "Whether the Sentry SDK should print its " +
"diagnostic logs to the console."),
options.Debug);

options.DebugOnlyInEditor = EditorGUILayout.Toggle(
new GUIContent("Only In Editor", "Only print logs when in the editor. Development " +
"builds of the player will not include Sentry's SDK diagnostics."),
options.DebugOnlyInEditor);

options.DiagnosticLevel = (SentryLevel)EditorGUILayout.EnumPopup(
new GUIContent("Verbosity Level", "The minimum level allowed to be printed to the console. " +
"Log messages with a level below this level are dropped."),
options.DiagnosticLevel);

EditorGUILayout.EndToggleGroup();
}

options.DiagnosticLevel = (SentryLevel)EditorGUILayout.EnumPopup(
new GUIContent("Verbosity Level", "The minimum level allowed to be printed to the console. " +
"Log messages with a level below this level are dropped."),
options.DiagnosticLevel);
EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

EditorGUILayout.EndToggleGroup();
{
options.AutoSessionTracking = EditorGUILayout.BeginToggleGroup(
new GUIContent("Auto Session Tracking", "Whether the SDK should start and end sessions " +
"automatically. If the timeout is reached the old session will" +
"be ended and a new one started."),
options.AutoSessionTracking);

options.AutoSessionTrackingInterval = EditorGUILayout.IntField(
new GUIContent("Session Timeout [ms]", "The duration of time a session can stay paused " +
"(i.e. the application has been put in the background) before " +
"it is considered ended."),
options.AutoSessionTrackingInterval);
options.AutoSessionTrackingInterval = Mathf.Max(0, options.AutoSessionTrackingInterval);
EditorGUILayout.EndToggleGroup();
}
Comment on lines +11 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please disregard. This is just an added indentation to help navigate this file.


EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

options.AutoSessionTracking = EditorGUILayout.BeginToggleGroup(
new GUIContent("Auto Session Tracking", "Whether the SDK should start and end sessions " +
"automatically. If the timeout is reached the old session will" +
"be ended and a new one started."),
options.AutoSessionTracking);

options.AutoSessionTrackingInterval = EditorGUILayout.IntField(
new GUIContent("Session Timeout [ms]", "The duration of time a session can stay paused " +
"(i.e. the application has been put in the background) before " +
"it is considered ended."),
options.AutoSessionTrackingInterval);
options.AutoSessionTrackingInterval = Mathf.Max(0, options.AutoSessionTrackingInterval);
EditorGUILayout.EndToggleGroup();
{
options.AnrDetectionEnabled = EditorGUILayout.BeginToggleGroup(
new GUIContent("ANR Detection", "Whether the SDK should report 'Application Not " +
"Responding' events."),
options.AnrDetectionEnabled);

options.AnrTimeout = EditorGUILayout.IntField(
new GUIContent("ANR Timeout [ms]", "The duration in [ms] for how long the game has to be unresponsive " +
"before an ANR event is reported.\nDefault: 5000ms"),
options.AnrTimeout);
options.AnrTimeout = Math.Max(0, options.AnrTimeout);
;
EditorGUILayout.EndToggleGroup();
}

EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

GUILayout.Label("Native Support", EditorStyles.boldLabel);

options.IosNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("iOS Native Support", "Whether to enable Native iOS support to capture" +
"errors written in languages such as Objective-C, Swift, C and C++."),
options.IosNativeSupportEnabled);

options.AndroidNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("Android Native Support", "Whether to enable Native Android support to " +
"capture errors written in languages such as Java, Kotlin, C and C++."),
options.AndroidNativeSupportEnabled);

options.WindowsNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("Windows Native Support", "Whether to enable native crashes support on Windows."),
options.WindowsNativeSupportEnabled);

options.MacosNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("macOS Native Support", "Whether to enable native crashes support on macOS."),
options.MacosNativeSupportEnabled);

options.LinuxNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("Linux Native Support", "Whether to enable native crashes support on Linux."),
options.LinuxNativeSupportEnabled);
{
options.IosNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("iOS Native Support", "Whether to enable Native iOS support to capture" +
"errors written in languages such as Objective-C, Swift, C and C++."),
options.IosNativeSupportEnabled);

options.AndroidNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("Android Native Support", "Whether to enable Native Android support to " +
"capture errors written in languages such as Java, Kotlin, C and C++."),
options.AndroidNativeSupportEnabled);

options.WindowsNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("Windows Native Support", "Whether to enable native crashes support on Windows."),
options.WindowsNativeSupportEnabled);

options.MacosNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("macOS Native Support", "Whether to enable native crashes support on macOS."),
options.MacosNativeSupportEnabled);

options.LinuxNativeSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("Linux Native Support", "Whether to enable native crashes support on Linux."),
options.LinuxNativeSupportEnabled);
}
Comment on lines +75 to +97
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please disregard. This is just an added indentation to help navigate this file.


EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

options.Il2CppLineNumberSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("IL2CPP line numbers", "Whether the SDK should try to to provide line " +
"numbers for exceptions in IL2CPP builds."),
options.Il2CppLineNumberSupportEnabled);

if (options.Il2CppLineNumberSupportEnabled)
{
if (!SentryUnityVersion.IsNewerOrEqualThan("2020.3"))
{
EditorGUILayout.HelpBox("The IL2CPP line number feature is supported from Unity version 2020.3 or newer and 2021.3 or newer onwards", MessageType.Warning);
}
else if (cliOptions is not null && !cliOptions.IsValid(null, EditorUserBuildSettings.development))
options.Il2CppLineNumberSupportEnabled = EditorGUILayout.Toggle(
new GUIContent("IL2CPP line numbers", "Whether the SDK should try to to provide line " +
"numbers for exceptions in IL2CPP builds."),
options.Il2CppLineNumberSupportEnabled);

if (options.Il2CppLineNumberSupportEnabled)
{
EditorGUILayout.HelpBox("The IL2CPP line number support relies on the Debug Symbol Upload to be properly set up.", MessageType.Error);
if (!SentryUnityVersion.IsNewerOrEqualThan("2020.3"))
{
EditorGUILayout.HelpBox("The IL2CPP line number feature is supported from Unity version 2020.3 or newer and 2021.3 or newer onwards", MessageType.Warning);
}
else if (cliOptions is not null && !cliOptions.IsValid(null, EditorUserBuildSettings.development))
{
EditorGUILayout.HelpBox("The IL2CPP line number support relies on the Debug Symbol Upload to be properly set up.", MessageType.Error);
}
Comment on lines +104 to +118
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please disregard. This is just an added indentation to help navigate this file.

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public override void OnInspectorGUI()
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

EditorGUILayout.LabelField("Application Not Responding", EditorStyles.boldLabel);
EditorGUILayout.Toggle("Enable ANR Detection", options.AnrDetectionEnabled);
EditorGUILayout.IntField("ANR Timeout [ms]", options.AnrTimeout);

EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

EditorGUILayout.Toggle("iOS Native Support", options.IosNativeSupportEnabled);
EditorGUILayout.Toggle("Android Native Support", options.AndroidNativeSupportEnabled);
EditorGUILayout.Toggle("Windows Native Support", options.WindowsNativeSupportEnabled);
Expand Down
20 changes: 12 additions & 8 deletions src/Sentry.Unity/Integrations/AnrIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public void Register(IHub hub, SentryOptions sentryOptions)
{
if (options.MultiThreading)
{
Watchdog = new AnrWatchDogMultiThreaded(options.DiagnosticLogger, _monoBehaviour);
Watchdog = new AnrWatchDogMultiThreaded(options.DiagnosticLogger,
_monoBehaviour,
options.AnrTimeout);
}
else
{
Watchdog = new AnrWatchDogSingleThreaded(options.DiagnosticLogger, _monoBehaviour);
Watchdog = new AnrWatchDogSingleThreaded(options.DiagnosticLogger,
_monoBehaviour,
options.AnrTimeout);
}
}
}
Expand All @@ -50,11 +54,11 @@ internal abstract class AnrWatchDog
internal event EventHandler<ApplicationNotResponding> OnApplicationNotResponding = delegate { };
protected bool Paused { get; private set; } = false;

internal AnrWatchDog(IDiagnosticLogger? logger, SentryMonoBehaviour monoBehaviour, int detectionTimeoutMilliseconds = 5000)
internal AnrWatchDog(IDiagnosticLogger? logger, SentryMonoBehaviour monoBehaviour, TimeSpan detectionTimeout)
{
MonoBehaviour = monoBehaviour;
Logger = logger;
DetectionTimeoutMs = detectionTimeoutMilliseconds;
DetectionTimeoutMs = detectionTimeout.Milliseconds;
SleepIntervalMs = Math.Max(1, DetectionTimeoutMs / 5);

MonoBehaviour.ApplicationPausing += () => Paused = true;
Expand Down Expand Up @@ -85,8 +89,8 @@ internal class AnrWatchDogMultiThreaded : AnrWatchDog
private bool _stop;
private readonly Thread _thread = null!;

internal AnrWatchDogMultiThreaded(IDiagnosticLogger? logger, SentryMonoBehaviour monoBehaviour, int detectionTimeoutMilliseconds = 5000)
: base(logger, monoBehaviour, detectionTimeoutMilliseconds)
internal AnrWatchDogMultiThreaded(IDiagnosticLogger? logger, SentryMonoBehaviour monoBehaviour, TimeSpan detectionTimeout)
: base(logger, monoBehaviour, detectionTimeout)
{
_thread = new Thread(Run)
{
Expand Down Expand Up @@ -164,8 +168,8 @@ internal class AnrWatchDogSingleThreaded : AnrWatchDog
private readonly Stopwatch _watch = new();
private bool _stop;

internal AnrWatchDogSingleThreaded(IDiagnosticLogger? logger, SentryMonoBehaviour monoBehaviour, int detectionTimeoutMilliseconds = 5000)
: base(logger, monoBehaviour, detectionTimeoutMilliseconds)
internal AnrWatchDogSingleThreaded(IDiagnosticLogger? logger, SentryMonoBehaviour monoBehaviour, TimeSpan detectionTimeout)
: base(logger, monoBehaviour, detectionTimeout)
{
// Check the UI status periodically by running a coroutine on the UI thread and checking the elapsed time
_watch.Start();
Expand Down
11 changes: 11 additions & 0 deletions src/Sentry.Unity/ScriptableSentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ public static string GetConfigPath(string? notDefaultConfigName = null)
[field: SerializeField] public float SampleRate { get; set; } = 1.0f;
[field: SerializeField] public int ShutdownTimeout { get; set; } = 2000;
[field: SerializeField] public int MaxQueueItems { get; set; } = 30;

[field: SerializeField] public bool AnrDetectionEnabled { get; set; } = true;
[field: SerializeField] public int AnrTimeout { get; set; } = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;

[field: SerializeField] public bool IosNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool AndroidNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool WindowsNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool MacosNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool LinuxNativeSupportEnabled { get; set; } = true;

[field: SerializeField] public bool Il2CppLineNumberSupportEnabled { get; set; } = true;

[field: SerializeField] public SentryRuntimeOptionsConfiguration? OptionsConfiguration { get; set; }
Expand Down Expand Up @@ -131,6 +136,7 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn
// need to set it here directly.
Debug = ShouldDebug(application.IsEditor && !isBuilding),
DiagnosticLevel = DiagnosticLevel,
AnrTimeout = TimeSpan.FromMilliseconds(AnrTimeout),
IosNativeSupportEnabled = IosNativeSupportEnabled,
AndroidNativeSupportEnabled = AndroidNativeSupportEnabled,
WindowsNativeSupportEnabled = WindowsNativeSupportEnabled,
Expand Down Expand Up @@ -197,6 +203,11 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn
{
options.AddIl2CppExceptionProcessor(unityInfo);
}

if (!AnrDetectionEnabled)
{
options.DisableAnrIntegration();
}
}

return options;
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public sealed class SentryUnityOptions : SentryOptions
/// </summary>
public Dictionary<LogType, bool> AddBreadcrumbsForLogType { get; set; }

/// <summary>
/// The duration in [ms] for how long the game has to be unresponsive before an ANR event is reported.
/// </summary>
public TimeSpan AnrTimeout { get; set; } = TimeSpan.FromSeconds(5);

/// <summary>
/// Whether the SDK should add native support for iOS
/// </summary>
Expand Down
Loading