From ee15a30cf2adfe4741ea1f98a5ce8a9dd9fb3061 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 14 Feb 2025 16:56:49 +0100 Subject: [PATCH 1/8] moved viewhierachy capture to eventprocessor --- .../ScriptableSentryUnityOptions.cs | 5 ++++ src/Sentry.Unity/SentryUnitySDK.cs | 7 ----- ...tent.cs => ViewHierarchyEventProcessor.cs} | 30 ++++++++++--------- ... UnityViewHierarchyEventProcessorTests.cs} | 28 +++++++++-------- 4 files changed, 37 insertions(+), 33 deletions(-) rename src/Sentry.Unity/{UnityViewHierarchyAttachmentContent.cs => ViewHierarchyEventProcessor.cs} (80%) rename test/Sentry.Unity.Tests/{UnityViewHierarchyAttachmentTests.cs => UnityViewHierarchyEventProcessorTests.cs} (84%) diff --git a/src/Sentry.Unity/ScriptableSentryUnityOptions.cs b/src/Sentry.Unity/ScriptableSentryUnityOptions.cs index 5490ff58a..b54d70649 100644 --- a/src/Sentry.Unity/ScriptableSentryUnityOptions.cs +++ b/src/Sentry.Unity/ScriptableSentryUnityOptions.cs @@ -226,6 +226,11 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn // Without setting up here we might miss out on logs between option-loading (now) and Init - i.e. native configuration options.SetupLogging(); + if (options.AttachViewHierarchy) + { + options.AddEventProcessor(new ViewHierarchyEventProcessor(options)); + } + if (!application.IsEditor && options.Il2CppLineNumberSupportEnabled && unityInfo is not null) { options.AddIl2CppExceptionProcessor(unityInfo); diff --git a/src/Sentry.Unity/SentryUnitySDK.cs b/src/Sentry.Unity/SentryUnitySDK.cs index 35d2be700..931af939a 100644 --- a/src/Sentry.Unity/SentryUnitySDK.cs +++ b/src/Sentry.Unity/SentryUnitySDK.cs @@ -58,13 +58,6 @@ private SentryUnitySdk(SentryUnityOptions options) new ScreenshotAttachmentContent(options)))); } - if (options.AttachViewHierarchy) - { - SentrySdk.ConfigureScope(s => - s.AddAttachment(new ViewHierarchyAttachment( - new UnityViewHierarchyAttachmentContent(options)))); - } - if (options.NativeContextWriter is { } contextWriter) { SentrySdk.ConfigureScope((scope) => diff --git a/src/Sentry.Unity/UnityViewHierarchyAttachmentContent.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs similarity index 80% rename from src/Sentry.Unity/UnityViewHierarchyAttachmentContent.cs rename to src/Sentry.Unity/ViewHierarchyEventProcessor.cs index 63489e578..f0a8dfa82 100644 --- a/src/Sentry.Unity/UnityViewHierarchyAttachmentContent.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text.Json; using Sentry.Extensibility; using UnityEngine; @@ -9,31 +8,36 @@ namespace Sentry.Unity; -internal class UnityViewHierarchyAttachmentContent : IAttachmentContent +public class ViewHierarchyEventProcessor : ISentryEventProcessorWithHint { private readonly SentryUnityOptions _options; - public UnityViewHierarchyAttachmentContent(SentryUnityOptions options) + public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) { - _options = options; + _options = sentryOptions; } - public Stream GetStream() + public SentryEvent? Process(SentryEvent @event) + { + return @event; + } + + public SentryEvent? Process(SentryEvent @event, SentryHint hint) { - // Note: we need to check explicitly that we're on the same thread. While Unity would throw otherwise - // when capturing the screenshot, it would only do so on development builds. On release, it just crashes... if (!MainThreadData.IsMainThread()) { _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on other than main (UI) thread."); - return Stream.Null; + return @event; } - return CaptureViewHierarchy(); + hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType:"application/json"); + + return @event; } - internal Stream CaptureViewHierarchy() + internal byte[] CaptureViewHierarchy() { - var stream = new MemoryStream(); + using var stream = new MemoryStream(); using var writer = new Utf8JsonWriter(stream); var viewHierarchy = CreateViewHierarchy( @@ -43,9 +47,7 @@ internal Stream CaptureViewHierarchy() viewHierarchy.WriteTo(writer, _options.DiagnosticLogger); writer.Flush(); - stream.Seek(0, SeekOrigin.Begin); - - return stream; + return stream.ToArray(); } internal ViewHierarchy CreateViewHierarchy(int maxRootGameObjectCount, int maxChildCount, int maxDepth) diff --git a/test/Sentry.Unity.Tests/UnityViewHierarchyAttachmentTests.cs b/test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs similarity index 84% rename from test/Sentry.Unity.Tests/UnityViewHierarchyAttachmentTests.cs rename to test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs index 12cebd679..ae0246285 100644 --- a/test/Sentry.Unity.Tests/UnityViewHierarchyAttachmentTests.cs +++ b/test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs @@ -6,13 +6,13 @@ namespace Sentry.Unity.Tests; -public class UnityViewHierarchyAttachmentTests +public class UnityViewHierarchyEventProcessorTests { private class Fixture { public SentryUnityOptions Options = new() { AttachViewHierarchy = true }; - public UnityViewHierarchyAttachmentContent GetSut() => new(Options); + public ViewHierarchyEventProcessor GetSut() => new(Options); } private Fixture _fixture = null!; @@ -30,39 +30,43 @@ public void TearDown() } [Test] - public void GetStream_IsMainThread_ReturnsStream() + public void GetStream_IsMainThread_AddsViewHierarchyToHint() { var sut = _fixture.GetSut(); + var sentryEvent = new SentryEvent(); + var hint = new SentryHint(); - var stream = sut.GetStream(); + sut.Process(sentryEvent, hint); - Assert.IsNotNull(stream); + Assert.AreEqual(1, hint.Attachments.Count); } [Test] - public void GetStream_IsNonMainThread_ReturnsNullStream() + public void GetStream_IsNonMainThread_DoesNotAddViewHierarchyToHint() { var sut = _fixture.GetSut(); + var sentryEvent = new SentryEvent(); + var hint = new SentryHint(); new Thread(() => { Thread.CurrentThread.IsBackground = true; - var stream = sut.GetStream(); + var stream = sut.Process(sentryEvent, hint); - Assert.NotNull(stream); - Assert.AreEqual(Stream.Null, stream); + Assert.AreEqual(0, hint.Attachments.Count); }).Start(); } [Test] - public void CaptureViewHierarchy_ReturnsNonNullStream() + public void CaptureViewHierarchy_ReturnsNonNullOrEmptyByteArray() { var sut = _fixture.GetSut(); - using var stream = sut.CaptureViewHierarchy(); + var byteArray = sut.CaptureViewHierarchy(); - Assert.AreNotEqual(Stream.Null, stream); + Assert.That(byteArray, Is.Not.Null); + Assert.That(byteArray.Length, Is.GreaterThan(0)); } [Test] From c61a2edeab17446b3bf7ace708efa5f8b2612523 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Fri, 14 Feb 2025 16:00:56 +0000 Subject: [PATCH 2/8] Format code --- modules/sentry-java | 2 +- src/Sentry.Unity/ViewHierarchyEventProcessor.cs | 2 +- src/sentry-dotnet | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/sentry-java b/modules/sentry-java index 2de45ebb0..f2910984a 160000 --- a/modules/sentry-java +++ b/modules/sentry-java @@ -1 +1 @@ -Subproject commit 2de45ebb088ed5802dcac00781cbaca7e54ed9d9 +Subproject commit f2910984ad5905276410aac83fc78a311dc8f28c diff --git a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs index f0a8dfa82..510af60cc 100644 --- a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -30,7 +30,7 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) return @event; } - hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType:"application/json"); + hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json"); return @event; } diff --git a/src/sentry-dotnet b/src/sentry-dotnet index 7faf221b6..05ac853ae 160000 --- a/src/sentry-dotnet +++ b/src/sentry-dotnet @@ -1 +1 @@ -Subproject commit 7faf221b6fbf3c293052c30429b38b75564380c4 +Subproject commit 05ac853ae29c10bd76db0e382777a7122bd6b171 From 32c880680c2d244a55d5752f86287b5b5861d5d9 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 14 Feb 2025 17:01:39 +0100 Subject: [PATCH 3/8] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87b170416..163dcc12f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Fixes +- The SDK no longer fails to attach the `ViewHierarchy` when the scope has previously been cleared. ([#2020](https://github.com/getsentry/sentry-unity/pull/2020)) - The SDK's build logs when targeting Android are not a lot less noisy. The SDK will also no longer omit the sentry-cli logs from the gradle build output. ([#1995](https://github.com/getsentry/sentry-unity/pull/1995)) - When targeting iOS and disabling native support, the SDK no longer causes builds to fail with an `Undefined symbol: _SentryNativeBridgeIsEnabled` error. ([#1983](https://github.com/getsentry/sentry-unity/pull/1983)) From 4ed6b402c9301a057137b783dd39f6cab6bbfcaa Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 17 Feb 2025 12:29:45 +0100 Subject: [PATCH 4/8] Updated CHANGELOG.md --- CHANGELOG.md | 50 ++++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18260cff2..43a4004c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,43 +25,25 @@ - Fixed iOS native SDK initialization that could cause memory management issues ([#1964](https://github.com/getsentry/sentry-unity/pull/1964)) - The SDK now properly sets up logging by respecting the debug settings set during the configure callback. Logs created during the configuration of the native SDKs no longer get lost ([#1959](https://github.com/getsentry/sentry-unity/pull/1959)) - ANR events now include the relevant mechanism they have been captured from ([#1955](https://github.com/getsentry/sentry-unity/pull/1955)) -- On Android, the SDK not longer freezes the game when failing to sync with the native SDK ([#1927](https://github.com/getsentry/sentry-unity/pull/1927)) +- On Android, the SDK no longer freezes the game when failing to sync with the native SDK ([#1927](https://github.com/getsentry/sentry-unity/pull/1927)) ### Dependencies -- Bump Native SDK from v0.7.18 to v0.7.20 ([#1981](https://github.com/getsentry/sentry-unity/pull/1981), [#2003](https://github.com/getsentry/sentry-unity/pull/2003)) - - [changelog](https://github.com/getsentry/sentry-native/blob/master/CHANGELOG.md#0720) - - [diff](https://github.com/getsentry/sentry-native/compare/0.7.18...0.7.20) -- Bump CLI from v2.40.0 to v2.41.1 ([#1984](https://github.com/getsentry/sentry-unity/pull/1984)) - - [changelog](https://github.com/getsentry/sentry-cli/blob/master/CHANGELOG.md#2411) - - [diff](https://github.com/getsentry/sentry-cli/compare/2.40.0...2.41.1) -- Bump Cocoa SDK from v8.43.0 to v8.45.0 ([#2001](https://github.com/getsentry/sentry-unity/pull/2001), [#2017](https://github.com/getsentry/sentry-unity/pull/2017)) - - [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8450) - - [diff](https://github.com/getsentry/sentry-cocoa/compare/8.43.0...8.45.0) -- Bump .NET SDK from v5.0.1 to v5.1.1 ([#2005](https://github.com/getsentry/sentry-unity/pull/2005), [#2018](https://github.com/getsentry/sentry-unity/pull/2018)) - - [changelog](https://github.com/getsentry/sentry-dotnet/blob/main/CHANGELOG.md#511) - - [diff](https://github.com/getsentry/sentry-dotnet/compare/5.0.1...5.1.1) -- Bump Java SDK from v7.20.0 to v8.2.0 ([#2014](https://github.com/getsentry/sentry-unity/pull/2014)) - - [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#820) - - [diff](https://github.com/getsentry/sentry-java/compare/7.20.0...8.2.0) - -### Dependencies - -- Bump .NET SDK from v4.13.0 to v5.0.1 ([#1940](https://github.com/getsentry/sentry-unity/pull/1940), [#1953](https://github.com/getsentry/sentry-unity/pull/1953)) - - [changelog](https://github.com/getsentry/sentry-dotnet/blob/main/CHANGELOG.md#501) - - [diff](https://github.com/getsentry/sentry-dotnet/compare/4.13.0...5.0.1) -- Bump CLI from v2.39.0 to v2.40.0 ([#1922](https://github.com/getsentry/sentry-unity/pull/1922), [#1948](https://github.com/getsentry/sentry-unity/pull/1948)) - - [changelog](https://github.com/getsentry/sentry-cli/blob/master/CHANGELOG.md#2400) - - [diff](https://github.com/getsentry/sentry-cli/compare/2.39.0...2.40.0) -- Bump Java SDK from v7.18.0 to v7.20.0 ([#1926](https://github.com/getsentry/sentry-unity/pull/1926), [#1934](https://github.com/getsentry/sentry-unity/pull/1934), [#1946](https://github.com/getsentry/sentry-unity/pull/1946), [#1947](https://github.com/getsentry/sentry-unity/pull/1947)) - - [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#7200) - - [diff](https://github.com/getsentry/sentry-java/compare/7.18.0...7.20.0) -- Bump Cocoa SDK from v8.41.0 to v8.43.0 ([#1937](https://github.com/getsentry/sentry-unity/pull/1937), [#1945](https://github.com/getsentry/sentry-unity/pull/1945), [#1949](https://github.com/getsentry/sentry-unity/pull/1949)) - - [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8430) - - [diff](https://github.com/getsentry/sentry-cocoa/compare/8.41.0...8.43.0) -- Bump Native SDK from v0.7.15 to v0.7.18 ([#1928](https://github.com/getsentry/sentry-unity/pull/1928), [#1939](https://github.com/getsentry/sentry-unity/pull/1939), [#1967](https://github.com/getsentry/sentry-unity/pull/1967)) - - [changelog](https://github.com/getsentry/sentry-native/blob/master/CHANGELOG.md#0718) - - [diff](https://github.com/getsentry/sentry-native/compare/0.7.15...0.7.18) +- Bump Native SDK from v0.7.15 to v0.7.20 ([#1928](https://github.com/getsentry/sentry-unity/pull/1928), [#1939](https://github.com/getsentry/sentry-unity/pull/1939), [#1967](https://github.com/getsentry/sentry-unity/pull/1967), [#1981](https://github.com/getsentry/sentry-unity/pull/1981), [#2003](https://github.com/getsentry/sentry-unity/pull/2003)) + - [changelog](https://github.com/getsentry/sentry-native/blob/master/CHANGELOG.md#0720) + - [diff](https://github.com/getsentry/sentry-native/compare/0.7.15...0.7.20) +- Bump CLI from v2.39.0 to v2.41.1 ([#1922](https://github.com/getsentry/sentry-unity/pull/1922), [#1948](https://github.com/getsentry/sentry-unity/pull/1948), [#1984](https://github.com/getsentry/sentry-unity/pull/1984)) + - [changelog](https://github.com/getsentry/sentry-cli/blob/master/CHANGELOG.md#2411) + - [diff](https://github.com/getsentry/sentry-cli/compare/2.39.0...2.41.1) +- Bump Cocoa SDK from v8.41.0 to v8.45.0 ([#1937](https://github.com/getsentry/sentry-unity/pull/1937), [#1945](https://github.com/getsentry/sentry-unity/pull/1945), [#1949](https://github.com/getsentry/sentry-unity/pull/1949), [#2001](https://github.com/getsentry/sentry-unity/pull/2001), [#2017](https://github.com/getsentry/sentry-unity/pull/2017)) + - [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8450) + - [diff](https://github.com/getsentry/sentry-cocoa/compare/8.41.0...8.45.0) +- Bump .NET SDK from v4.13.0 to v5.1.1 ([#1940](https://github.com/getsentry/sentry-unity/pull/1940), [#1953](https://github.com/getsentry/sentry-unity/pull/1953), [#2005](https://github.com/getsentry/sentry-unity/pull/2005), [#2018](https://github.com/getsentry/sentry-unity/pull/2018)) + - [changelog](https://github.com/getsentry/sentry-dotnet/blob/main/CHANGELOG.md#511) + - [diff](https://github.com/getsentry/sentry-dotnet/compare/4.13.0...5.1.1) +- Bump Java SDK from v7.18.0 to v8.2.0 ([#1926](https://github.com/getsentry/sentry-unity/pull/1926), [#1934](https://github.com/getsentry/sentry-unity/pull/1934), [#1946](https://github.com/getsentry/sentry-unity/pull/1946), [#1947](https://github.com/getsentry/sentry-unity/pull/1947), [#2014](https://github.com/getsentry/sentry-unity/pull/2014)) + - [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#820) + - [diff](https://github.com/getsentry/sentry-java/compare/7.18.0...8.2.0) ## 2.4.0 From f8c37bfa3101796a5a10519ab0479ffab7698af5 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 17 Feb 2025 13:29:56 +0100 Subject: [PATCH 5/8] added callbacks --- src/Sentry.Unity/SentryUnityOptions.cs | 32 +++++++++++++++++++ .../ViewHierarchyEventProcessor.cs | 9 +++++- .../ScreenshotEventProcessorTests.cs | 19 +++++++++-- ...cs => ViewHierarchyEventProcessorTests.cs} | 21 ++++++++++-- 4 files changed, 75 insertions(+), 6 deletions(-) rename test/Sentry.Unity.Tests/{UnityViewHierarchyEventProcessorTests.cs => ViewHierarchyEventProcessorTests.cs} (88%) diff --git a/src/Sentry.Unity/SentryUnityOptions.cs b/src/Sentry.Unity/SentryUnityOptions.cs index edc56b7a3..45ddfc1e1 100644 --- a/src/Sentry.Unity/SentryUnityOptions.cs +++ b/src/Sentry.Unity/SentryUnityOptions.cs @@ -228,6 +228,38 @@ public sealed class SentryUnityOptions : SentryOptions /// public new StackTraceMode StackTraceMode { get; private set; } + private Func? _beforeAttachScreenshot; + + internal Func? BeforeAttachScreenshotInternal => _beforeAttachScreenshot; + + /// + /// Configures a callback function to be invoked before capturing and attaching a screenshot to an event. + /// + /// + /// This callback will get invoked right before a screenshot gets taken. If the screenshot should not + /// be taken return `false`. + /// + public void SetBeforeAttachScreenshot(Func beforeAttachScreenshot) + { + _beforeAttachScreenshot = beforeAttachScreenshot; + } + + private Func? _beforeAttachViewHierarchy; + + internal Func? BeforeAttachViewHierarchyInternal => _beforeAttachViewHierarchy; + + /// + /// Configures a callback function to be invoked before capturing and attaching the view hierarchy to an event. + /// + /// + /// This callback will get invoked right before the view hierarchy gets taken. If the view hierarchy should not + /// be taken return `false`. + /// + public void SetBeforeAttachViewHierarchy(Func beforeAttachViewHierarchy) + { + _beforeAttachViewHierarchy = beforeAttachViewHierarchy; + } + // Initialized by native SDK binding code to set the User.ID in .NET (UnityEventProcessor). internal string? _defaultUserId; internal string? DefaultUserId diff --git a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs index 510af60cc..9024a66ab 100644 --- a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -30,7 +30,14 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) return @event; } - hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json"); + if (_options.BeforeAttachViewHierarchyInternal?.Invoke() is not false) + { + hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json"); + } + else + { + _options.DiagnosticLogger?.LogInfo("View hierarchy attachment skipped by BeforeAttachViewHierarchy callback."); + } return @event; } diff --git a/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs b/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs index 99d4b4e0e..834a20c52 100644 --- a/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs @@ -40,7 +40,7 @@ public void GetTargetResolution_ReturnsTargetMaxSize(ScreenshotQuality quality, } [Test] - public void GetStream_IsMainThread_AddsScreenshotToHint() + public void Process_IsMainThread_AddsScreenshotToHint() { var sut = _fixture.GetSut(); var sentryEvent = new SentryEvent(); @@ -52,7 +52,7 @@ public void GetStream_IsMainThread_AddsScreenshotToHint() } [Test] - public void GetStream_IsNonMainThread_DoesNotAddScreenshotToHint() + public void Process_IsNonMainThread_DoesNotAddScreenshotToHint() { var sut = _fixture.GetSut(); var sentryEvent = new SentryEvent(); @@ -67,6 +67,21 @@ public void GetStream_IsNonMainThread_DoesNotAddScreenshotToHint() }).Start(); } + [Test] + [TestCase(true)] + [TestCase(false)] + public void Process_BeforeAddScreenshotCallbackProvided_RespectsScreenshotCaptureDecision(bool captureScreenshot) + { + _fixture.Options.SetBeforeAttachScreenshot(() => captureScreenshot); + var sut = _fixture.GetSut(); + var sentryEvent = new SentryEvent(); + var hint = new SentryHint(); + + sut.Process(sentryEvent, hint); + + Assert.AreEqual(!captureScreenshot ? 1 : 0, hint.Attachments.Count); + } + [Test] [TestCase(ScreenshotQuality.High, 1920)] [TestCase(ScreenshotQuality.Medium, 1280)] diff --git a/test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs similarity index 88% rename from test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs rename to test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs index ae0246285..2c05b29f8 100644 --- a/test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs @@ -6,7 +6,7 @@ namespace Sentry.Unity.Tests; -public class UnityViewHierarchyEventProcessorTests +public class ViewHierarchyEventProcessorTests { private class Fixture { @@ -30,7 +30,7 @@ public void TearDown() } [Test] - public void GetStream_IsMainThread_AddsViewHierarchyToHint() + public void Process_IsMainThread_AddsViewHierarchyToHint() { var sut = _fixture.GetSut(); var sentryEvent = new SentryEvent(); @@ -42,7 +42,7 @@ public void GetStream_IsMainThread_AddsViewHierarchyToHint() } [Test] - public void GetStream_IsNonMainThread_DoesNotAddViewHierarchyToHint() + public void Process_IsNonMainThread_DoesNotAddViewHierarchyToHint() { var sut = _fixture.GetSut(); var sentryEvent = new SentryEvent(); @@ -58,6 +58,21 @@ public void GetStream_IsNonMainThread_DoesNotAddViewHierarchyToHint() }).Start(); } + [Test] + [TestCase(true)] + [TestCase(false)] + public void Process_BeforeAddViewHierarchyCallbackProvided_RespectViewHierarchyCaptureDecision(bool captureViewHierarchy) + { + _fixture.Options.SetBeforeAttachViewHierarchy(() => captureViewHierarchy); + var sut = _fixture.GetSut(); + var sentryEvent = new SentryEvent(); + var hint = new SentryHint(); + + sut.Process(sentryEvent, hint); + + Assert.AreEqual(!captureViewHierarchy ? 1 : 0, hint.Attachments.Count); + } + [Test] public void CaptureViewHierarchy_ReturnsNonNullOrEmptyByteArray() { From c433e26b6aec1696cff5b5fae7f7740d5b0fb967 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 17 Feb 2025 13:55:49 +0100 Subject: [PATCH 6/8] . --- src/Sentry.Unity/ScreenshotEventProcessor.cs | 15 ++++++++++++--- src/Sentry.Unity/ViewHierarchyEventProcessor.cs | 2 +- .../ScreenshotEventProcessorTests.cs | 2 +- .../ViewHierarchyEventProcessorTests.cs | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Sentry.Unity/ScreenshotEventProcessor.cs b/src/Sentry.Unity/ScreenshotEventProcessor.cs index 8d0e0f8d4..90660505a 100644 --- a/src/Sentry.Unity/ScreenshotEventProcessor.cs +++ b/src/Sentry.Unity/ScreenshotEventProcessor.cs @@ -24,15 +24,24 @@ public ScreenshotEventProcessor(SentryUnityOptions sentryOptions) return @event; } - if (Screen.width == 0 || Screen.height == 0) + if (_options.BeforeAttachScreenshotInternal?.Invoke() is not false) { - _options.DiagnosticLogger?.LogWarning("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.", Screen.width, Screen.height); + if (Screen.width == 0 || Screen.height == 0) + { + _options.DiagnosticLogger?.LogWarning("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.", Screen.width, Screen.height); + } + else + { + hint.AddAttachment(CaptureScreenshot(Screen.width, Screen.height), "screenshot.jpg", contentType: "image/jpeg"); + } } else { - hint.AddAttachment(CaptureScreenshot(Screen.width, Screen.height), "screenshot.jpg", contentType: "image/jpeg"); + _options.DiagnosticLogger?.LogInfo("Screenshot attachment skipped by BeforeAttachScreenshot callback."); } + + return @event; } diff --git a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs index 9024a66ab..7459b249a 100644 --- a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -26,7 +26,7 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) { if (!MainThreadData.IsMainThread()) { - _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on other than main (UI) thread."); + _options.DiagnosticLogger?.LogDebug("Can't capture view hierarchy on other than main (UI) thread."); return @event; } diff --git a/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs b/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs index 834a20c52..c6a936d5d 100644 --- a/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs @@ -79,7 +79,7 @@ public void Process_BeforeAddScreenshotCallbackProvided_RespectsScreenshotCaptur sut.Process(sentryEvent, hint); - Assert.AreEqual(!captureScreenshot ? 1 : 0, hint.Attachments.Count); + Assert.AreEqual(captureScreenshot ? 1 : 0, hint.Attachments.Count); } [Test] diff --git a/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs index 2c05b29f8..798456152 100644 --- a/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs @@ -70,7 +70,7 @@ public void Process_BeforeAddViewHierarchyCallbackProvided_RespectViewHierarchyC sut.Process(sentryEvent, hint); - Assert.AreEqual(!captureViewHierarchy ? 1 : 0, hint.Attachments.Count); + Assert.AreEqual(captureViewHierarchy ? 1 : 0, hint.Attachments.Count); } [Test] From cd76e675d29391b616f7932ce8d5e8e9955292fb Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 17 Feb 2025 14:01:14 +0100 Subject: [PATCH 7/8] renamed --- src/Sentry.Unity/ScreenshotEventProcessor.cs | 2 +- src/Sentry.Unity/SentryUnityOptions.cs | 16 ++++++++-------- src/Sentry.Unity/ViewHierarchyEventProcessor.cs | 2 +- .../ScreenshotEventProcessorTests.cs | 4 ++-- .../ViewHierarchyEventProcessorTests.cs | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Sentry.Unity/ScreenshotEventProcessor.cs b/src/Sentry.Unity/ScreenshotEventProcessor.cs index 90660505a..46ea04c27 100644 --- a/src/Sentry.Unity/ScreenshotEventProcessor.cs +++ b/src/Sentry.Unity/ScreenshotEventProcessor.cs @@ -24,7 +24,7 @@ public ScreenshotEventProcessor(SentryUnityOptions sentryOptions) return @event; } - if (_options.BeforeAttachScreenshotInternal?.Invoke() is not false) + if (_options.BeforeCaptureScreenshotInternal?.Invoke() is not false) { if (Screen.width == 0 || Screen.height == 0) { diff --git a/src/Sentry.Unity/SentryUnityOptions.cs b/src/Sentry.Unity/SentryUnityOptions.cs index 45ddfc1e1..7cc9773f5 100644 --- a/src/Sentry.Unity/SentryUnityOptions.cs +++ b/src/Sentry.Unity/SentryUnityOptions.cs @@ -228,9 +228,9 @@ public sealed class SentryUnityOptions : SentryOptions /// public new StackTraceMode StackTraceMode { get; private set; } - private Func? _beforeAttachScreenshot; + private Func? _beforeCaptureScreenshot; - internal Func? BeforeAttachScreenshotInternal => _beforeAttachScreenshot; + internal Func? BeforeCaptureScreenshotInternal => _beforeCaptureScreenshot; /// /// Configures a callback function to be invoked before capturing and attaching a screenshot to an event. @@ -239,14 +239,14 @@ public sealed class SentryUnityOptions : SentryOptions /// This callback will get invoked right before a screenshot gets taken. If the screenshot should not /// be taken return `false`. /// - public void SetBeforeAttachScreenshot(Func beforeAttachScreenshot) + public void SetBeforeCaptureScreenshot(Func beforeAttachScreenshot) { - _beforeAttachScreenshot = beforeAttachScreenshot; + _beforeCaptureScreenshot = beforeAttachScreenshot; } - private Func? _beforeAttachViewHierarchy; + private Func? _beforeCaptureViewHierarchy; - internal Func? BeforeAttachViewHierarchyInternal => _beforeAttachViewHierarchy; + internal Func? BeforeCaptureViewHierarchyInternal => _beforeCaptureViewHierarchy; /// /// Configures a callback function to be invoked before capturing and attaching the view hierarchy to an event. @@ -255,9 +255,9 @@ public void SetBeforeAttachScreenshot(Func beforeAttachScreenshot) /// This callback will get invoked right before the view hierarchy gets taken. If the view hierarchy should not /// be taken return `false`. /// - public void SetBeforeAttachViewHierarchy(Func beforeAttachViewHierarchy) + public void SetBeforeCaptureViewHierarchy(Func beforeAttachViewHierarchy) { - _beforeAttachViewHierarchy = beforeAttachViewHierarchy; + _beforeCaptureViewHierarchy = beforeAttachViewHierarchy; } // Initialized by native SDK binding code to set the User.ID in .NET (UnityEventProcessor). diff --git a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs index 7459b249a..ff372a2e2 100644 --- a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -30,7 +30,7 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) return @event; } - if (_options.BeforeAttachViewHierarchyInternal?.Invoke() is not false) + if (_options.BeforeCaptureViewHierarchyInternal?.Invoke() is not false) { hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json"); } diff --git a/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs b/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs index c6a936d5d..c12101c0d 100644 --- a/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs @@ -70,9 +70,9 @@ public void Process_IsNonMainThread_DoesNotAddScreenshotToHint() [Test] [TestCase(true)] [TestCase(false)] - public void Process_BeforeAddScreenshotCallbackProvided_RespectsScreenshotCaptureDecision(bool captureScreenshot) + public void Process_BeforeCaptureScreenshotCallbackProvided_RespectsScreenshotCaptureDecision(bool captureScreenshot) { - _fixture.Options.SetBeforeAttachScreenshot(() => captureScreenshot); + _fixture.Options.SetBeforeCaptureScreenshot(() => captureScreenshot); var sut = _fixture.GetSut(); var sentryEvent = new SentryEvent(); var hint = new SentryHint(); diff --git a/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs index 798456152..273768375 100644 --- a/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs @@ -61,9 +61,9 @@ public void Process_IsNonMainThread_DoesNotAddViewHierarchyToHint() [Test] [TestCase(true)] [TestCase(false)] - public void Process_BeforeAddViewHierarchyCallbackProvided_RespectViewHierarchyCaptureDecision(bool captureViewHierarchy) + public void Process_BeforeCaptureViewHierarchyCallbackProvided_RespectViewHierarchyCaptureDecision(bool captureViewHierarchy) { - _fixture.Options.SetBeforeAttachViewHierarchy(() => captureViewHierarchy); + _fixture.Options.SetBeforeCaptureViewHierarchy(() => captureViewHierarchy); var sut = _fixture.GetSut(); var sentryEvent = new SentryEvent(); var hint = new SentryHint(); From cb583b43945335c84ab697b6b2b5b8b54e8d05b2 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 17 Feb 2025 14:04:03 +0100 Subject: [PATCH 8/8] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7e1d8cab..5a57f5271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Features +- Added `SetBeforeCaptureScreenshot` and `SetBeforeCaptureViewHierarchy` to the options. Users can now choose whether to capture those as attachment on an individual event basis. ([#2023](https://github.com/getsentry/sentry-unity/pull/2023)) - When capturing events via `Debug.LogError`, the SDK now provides stacktraces. Note, that the SDK is currently not able to provide line numbers for these events. ([#1965](https://github.com/getsentry/sentry-unity/pull/1965)) - Added option to enable/disable automatic capture of `Debug.LogError` as event. ([#2009](https://github.com/getsentry/sentry-unity/pull/2009)) - The `Ignore CLI Errors` checkbox in the Debug Symbols tab now applies to all supported platforms. ([#2008](https://github.com/getsentry/sentry-unity/pull/2008))