Skip to content

Commit

Permalink
feat: Added MinimumBreadcrumbLevel (#1030)
Browse files Browse the repository at this point in the history
Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
  • Loading branch information
bitsandfoxes and getsentry-bot authored Oct 24, 2022
1 parent 3b66076 commit 5a7cd66
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Features

- Added flags per LogType for automatically adding breadcrumbs ([#1030](https://github.com/getsentry/sentry-unity/pull/1030))
- The Cocoa SDK is now bundled as `.xcframework` ([#1002](https://github.com/getsentry/sentry-unity/pull/1002))
- Automated Performance Instrumentation for Runtime Initialization ([#991](https://github.com/getsentry/sentry-unity/pull/991))
- Automated Performance Instrumentation for Scene Loading ([#768](https://github.com/getsentry/sentry-unity/pull/768))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ MonoBehaviour:
<AttachScreenshot>k__BackingField: 1
<ScreenshotQuality>k__BackingField: 2
<ScreenshotCompression>k__BackingField: 75
<BreadcrumbsForLogs>k__BackingField: 1
<BreadcrumbsForWarnings>k__BackingField: 1
<BreadcrumbsForAsserts>k__BackingField: 1
<BreadcrumbsForErrors>k__BackingField: 1
<BreadcrumbsForExceptions>k__BackingField: 1
<MaxBreadcrumbs>k__BackingField: 100
<ReportAssembliesMode>k__BackingField: 1
<SendDefaultPii>k__BackingField: 0
Expand Down
23 changes: 23 additions & 0 deletions src/Sentry.Unity.Editor/ConfigurationWindow/EnrichmentTab.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;

namespace Sentry.Unity.Editor.ConfigurationWindow
{
Expand Down Expand Up @@ -63,6 +64,28 @@ internal static void Display(ScriptableSentryUnityOptions options)
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

GUILayout.Label("Breadcrumbs automatically added for LogType:", EditorStyles.boldLabel);

options.BreadcrumbsForLogs = EditorGUILayout.Toggle(
new GUIContent("Log", "Whether the SDK automatically adds breadcrumbs 'Debug.Log'."),
options.BreadcrumbsForLogs);
options.BreadcrumbsForWarnings = EditorGUILayout.Toggle(
new GUIContent("Warning", "Whether the SDK automatically adds breadcrumbs for 'Debug.LogWarning'."),
options.BreadcrumbsForWarnings);
options.BreadcrumbsForAsserts = EditorGUILayout.Toggle(
new GUIContent("Assert", "Whether the SDK automatically adds breadcrumbs for 'Debug.Assert'."),
options.BreadcrumbsForAsserts);
options.BreadcrumbsForErrors = EditorGUILayout.Toggle(
new GUIContent("Error", "Whether the SDK automatically adds breadcrumbs for 'Debug.LogError'."),
options.BreadcrumbsForLogs);
options.BreadcrumbsForExceptions = EditorGUILayout.Toggle(
new GUIContent("Exception", "Whether the SDK automatically adds breadcrumbs for exceptions and 'Debug.LogException'."),
options.BreadcrumbsForExceptions);

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

options.MaxBreadcrumbs = EditorGUILayout.IntField(
new GUIContent("Max Breadcrumbs", "Maximum number of breadcrumbs that get captured." +
"\nDefault: 100"),
Expand Down
14 changes: 10 additions & 4 deletions src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ internal void CaptureException(Exception exception, UnityEngine.Object? context)
exception.Data[Mechanism.MechanismKey] = "Unity.LogException";
_ = _hub.CaptureException(exception);

// So the next event includes this error as a breadcrumb
_hub.AddBreadcrumb(message: $"{exception.GetType()}: {exception.Message}", category: "unity.logger", level: BreadcrumbLevel.Error);
if (_sentryOptions?.AddBreadcrumbsForLogType[LogType.Exception] is true)
{
// So the next event includes this error as a breadcrumb
_hub.AddBreadcrumb(message: $"{exception.GetType()}: {exception.Message}", category: "unity.logger", level: BreadcrumbLevel.Error);
}
}

public void LogFormat(LogType logType, UnityEngine.Object? context, string format, params object[] args)
Expand Down Expand Up @@ -129,8 +132,11 @@ internal void CaptureLogFormat(LogType logType, UnityEngine.Object? context, str
_hub.CaptureMessage(logMessage, ToEventTagType(logType));
}

// So the next event includes this as a breadcrumb
_hub.AddBreadcrumb(message: logMessage, category: "unity.logger", level: ToBreadcrumbLevel(logType));
if (_sentryOptions?.AddBreadcrumbsForLogType[logType] is true)
{
// So the next event includes this as a breadcrumb
_hub.AddBreadcrumb(message: logMessage, category: "unity.logger", level: ToBreadcrumbLevel(logType));
}
}

private void OnQuitting()
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry.Unity/ScriptableSentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public static string GetConfigPath(string? notDefaultConfigName = null)
[field: SerializeField] public ScreenshotQuality ScreenshotQuality { get; set; } = ScreenshotQuality.High;
[field: SerializeField] public int ScreenshotCompression { get; set; } = 75;

[field: SerializeField] public bool BreadcrumbsForLogs { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForWarnings { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForAsserts { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForErrors { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForExceptions { get; set; } = true;

[field: SerializeField] public int MaxBreadcrumbs { get; set; } = Constants.DefaultMaxBreadcrumbs;

[field: SerializeField] public ReportAssembliesMode ReportAssembliesMode { get; set; } = ReportAssembliesMode.Version;
Expand Down Expand Up @@ -138,6 +144,12 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn
options.Environment = EnvironmentOverride;
}

options.AddBreadcrumbsForLogType[LogType.Log] = BreadcrumbsForLogs;
options.AddBreadcrumbsForLogType[LogType.Warning] = BreadcrumbsForWarnings;
options.AddBreadcrumbsForLogType[LogType.Assert] = BreadcrumbsForAsserts;
options.AddBreadcrumbsForLogType[LogType.Error] = BreadcrumbsForErrors;
options.AddBreadcrumbsForLogType[LogType.Exception] = BreadcrumbsForExceptions;

options.SetupLogging();

if (IsKnownPlatform(application))
Expand Down
15 changes: 15 additions & 0 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.RegularExpressions;
using Sentry.Unity.Integrations;
using Sentry.Extensibility;
using UnityEngine;
using CompressionLevel = System.IO.Compression.CompressionLevel;

namespace Sentry.Unity
Expand Down Expand Up @@ -81,6 +82,11 @@ public sealed class SentryUnityOptions : SentryOptions
/// </summary>
public int ScreenshotCompression { get; set; } = 75;

/// <summary>
/// Whether the SDK should automatically add breadcrumbs per LogType
/// </summary>
public Dictionary<LogType, bool> AddBreadcrumbsForLogType { get; set; }

/// <summary>
/// Whether the SDK should add native support for iOS
/// </summary>
Expand Down Expand Up @@ -202,6 +208,15 @@ internal SentryUnityOptions(SentryMonoBehaviour behaviour, IApplication applicat
Environment = application.IsEditor && !isBuilding
? "editor"
: "production";

AddBreadcrumbsForLogType = new Dictionary<LogType, bool>
{
{ LogType.Log, true},
{ LogType.Warning, true},
{ LogType.Assert, true},
{ LogType.Error, true},
{ LogType.Exception, true},
};
}

public override string ToString()
Expand Down
64 changes: 64 additions & 0 deletions test/Sentry.Unity.Tests/UnityLogHandlerIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,70 @@ public void CaptureLogFormat_UnityNotErrorLogTypes_NotCaptured(LogType unityLogT
Assert.AreEqual(breadcrumbLevel, breadcrumb.Level);
}

[Test]
[TestCase(LogType.Log)]
[TestCase(LogType.Warning)]
[TestCase(LogType.Error)]
[TestCase(LogType.Assert)]
public void CaptureLogFormat_AddAsBreadcrumbEnabled_AddedAsBreadcrumb(LogType logType)
{
_fixture.SentryOptions.AddBreadcrumbsForLogType[logType] = true;
var sut = _fixture.GetSut();
var message = NUnit.Framework.TestContext.CurrentContext.Test.Name;

sut.CaptureLogFormat(logType, null, "{0}", message);

var scope = new Scope(_fixture.SentryOptions);
_fixture.Hub.ConfigureScopeCalls.Single().Invoke(scope);
var breadcrumb = scope.Breadcrumbs.Single();

Assert.AreEqual(message, breadcrumb.Message);
}

[Test]
public void CaptureException_AddAsBreadcrumbEnabled_AddedAsBreadcrumb()
{
_fixture.SentryOptions.AddBreadcrumbsForLogType[LogType.Exception] = true;
var sut = _fixture.GetSut();
var message = NUnit.Framework.TestContext.CurrentContext.Test.Name;

sut.CaptureException(new Exception(message), null);

var scope = new Scope(_fixture.SentryOptions);
_fixture.Hub.ConfigureScopeCalls.Single().Invoke(scope);
var breadcrumb = scope.Breadcrumbs.Single();

StringAssert.Contains(message, breadcrumb.Message);
}

[Test]
[TestCase(LogType.Log)]
[TestCase(LogType.Warning)]
[TestCase(LogType.Error)]
[TestCase(LogType.Assert)]
public void CaptureLogFormat_AddAsBreadcrumbDisabled_NotAddedAsBreadcrumb(LogType logType)
{
_fixture.SentryOptions.AddBreadcrumbsForLogType[logType] = false;
var sut = _fixture.GetSut();
var message = NUnit.Framework.TestContext.CurrentContext.Test.Name;

sut.CaptureLogFormat(logType, null, "{0}", message);

Assert.IsFalse(_fixture.Hub.ConfigureScopeCalls.Count > 0);
}

[Test]
public void CaptureException_AddAsBreadcrumbEnabled_NotAddedAsBreadcrumb()
{
_fixture.SentryOptions.AddBreadcrumbsForLogType[LogType.Exception] = false;
var sut = _fixture.GetSut();
var message = NUnit.Framework.TestContext.CurrentContext.Test.Name;

sut.CaptureException(new Exception("Test Exception"), null);

Assert.IsFalse(_fixture.Hub.ConfigureScopeCalls.Count > 0);
}

[Test]
public void CaptureException_ExceptionCapturedAndMechanismSet()
{
Expand Down

0 comments on commit 5a7cd66

Please sign in to comment.