From 8c739dfb01746bc3ea9be8accb66dc738eb6ec70 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 25 Feb 2025 14:24:41 +0100 Subject: [PATCH 1/4] invoking the callback --- .../ViewHierarchyEventProcessor.cs | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs index ff372a2e2..53d430e88 100644 --- a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -32,7 +32,26 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) if (_options.BeforeCaptureViewHierarchyInternal?.Invoke() is not false) { - hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json"); + var viewHierarchy = CreateViewHierarchy( + _options.MaxViewHierarchyRootObjects, + _options.MaxViewHierarchyObjectChildCount, + _options.MaxViewHierarchyDepth); + + if (_options.BeforeSendViewHierarchyInternal is not null) + { + viewHierarchy = _options.BeforeSendViewHierarchyInternal.Invoke(viewHierarchy); + if (viewHierarchy is null) + { + return @event; + } + } + + using var stream = new MemoryStream(); + using var writer = new Utf8JsonWriter(stream); + viewHierarchy.WriteTo(writer, _options.DiagnosticLogger); + writer.Flush(); + + hint.AddAttachment(stream.ToArray(), "view-hierarchy.json", contentType: "application/json"); } else { @@ -42,21 +61,6 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) return @event; } - internal byte[] CaptureViewHierarchy() - { - using var stream = new MemoryStream(); - using var writer = new Utf8JsonWriter(stream); - - var viewHierarchy = CreateViewHierarchy( - _options.MaxViewHierarchyRootObjects, - _options.MaxViewHierarchyObjectChildCount, - _options.MaxViewHierarchyDepth); - viewHierarchy.WriteTo(writer, _options.DiagnosticLogger); - - writer.Flush(); - return stream.ToArray(); - } - internal ViewHierarchy CreateViewHierarchy(int maxRootGameObjectCount, int maxChildCount, int maxDepth) { var rootGameObjects = new List(); From 8c2c3fd2cf2ed8454980ef352f628f56f8d90736 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 25 Feb 2025 14:24:56 +0100 Subject: [PATCH 2/4] adding callback to options --- src/Sentry.Unity/SentryUnityOptions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Sentry.Unity/SentryUnityOptions.cs b/src/Sentry.Unity/SentryUnityOptions.cs index 7cc9773f5..bce62e557 100644 --- a/src/Sentry.Unity/SentryUnityOptions.cs +++ b/src/Sentry.Unity/SentryUnityOptions.cs @@ -260,6 +260,22 @@ public void SetBeforeCaptureViewHierarchy(Func beforeAttachViewHierarchy) _beforeCaptureViewHierarchy = beforeAttachViewHierarchy; } + private Func? _beforeSendViewHierarchy; + + internal Func? BeforeSendViewHierarchyInternal => _beforeSendViewHierarchy; + + /// + /// 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 SetBeforeSendViewHierarchy(Func beforeSendViewHierarchy) + { + _beforeSendViewHierarchy = beforeSendViewHierarchy; + } + // Initialized by native SDK binding code to set the User.ID in .NET (UnityEventProcessor). internal string? _defaultUserId; internal string? DefaultUserId From 3fb5eb31a118f981e39a794ce3c3060b7f956ce8 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 25 Feb 2025 14:25:04 +0100 Subject: [PATCH 3/4] tests --- .../ViewHierarchyEventProcessorTests.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs index 273768375..70eb66e07 100644 --- a/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs +++ b/test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs @@ -73,16 +73,16 @@ public void Process_BeforeCaptureViewHierarchyCallbackProvided_RespectViewHierar Assert.AreEqual(captureViewHierarchy ? 1 : 0, hint.Attachments.Count); } - [Test] - public void CaptureViewHierarchy_ReturnsNonNullOrEmptyByteArray() - { - var sut = _fixture.GetSut(); - - var byteArray = sut.CaptureViewHierarchy(); - - Assert.That(byteArray, Is.Not.Null); - Assert.That(byteArray.Length, Is.GreaterThan(0)); - } + // [Test] + // public void CaptureViewHierarchy_ReturnsNonNullOrEmptyByteArray() + // { + // var sut = _fixture.GetSut(); + // + // var byteArray = sut.CaptureViewHierarchy(); + // + // Assert.That(byteArray, Is.Not.Null); + // Assert.That(byteArray.Length, Is.GreaterThan(0)); + // } [Test] public void CreateViewHierarchy_CapturesSceneAsRoot() From f2128236a26dd3262daa127d2a680edc89664509 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 25 Feb 2025 15:48:24 +0100 Subject: [PATCH 4/4] fix attachment type --- src/Sentry.Unity/ViewHierarchyEventProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs index 53d430e88..4ce3e1bf7 100644 --- a/src/Sentry.Unity/ViewHierarchyEventProcessor.cs +++ b/src/Sentry.Unity/ViewHierarchyEventProcessor.cs @@ -51,7 +51,7 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions) viewHierarchy.WriteTo(writer, _options.DiagnosticLogger); writer.Flush(); - hint.AddAttachment(stream.ToArray(), "view-hierarchy.json", contentType: "application/json"); + hint.AddAttachment(stream.ToArray(), "view-hierarchy.json", AttachmentType.ViewHierarchy, "application/json"); } else {