Skip to content

feat: Added BeforeSendViewHierarchy callback #2035

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ public void SetBeforeCaptureViewHierarchy(Func<bool> beforeAttachViewHierarchy)
_beforeCaptureViewHierarchy = beforeAttachViewHierarchy;
}

private Func<ViewHierarchy, ViewHierarchy?>? _beforeSendViewHierarchy;

internal Func<ViewHierarchy, ViewHierarchy?>? BeforeSendViewHierarchyInternal => _beforeSendViewHierarchy;

/// <summary>
/// Configures a callback function to be invoked before capturing and attaching the view hierarchy to an event.
/// </summary>
/// <remarks>
/// This callback will get invoked right before the view hierarchy gets taken. If the view hierarchy should not
/// be taken return `false`.
/// </remarks>
public void SetBeforeSendViewHierarchy(Func<ViewHierarchy, ViewHierarchy?> beforeSendViewHierarchy)
{
_beforeSendViewHierarchy = beforeSendViewHierarchy;
}

// Initialized by native SDK binding code to set the User.ID in .NET (UnityEventProcessor).
internal string? _defaultUserId;
internal string? DefaultUserId
Expand Down
36 changes: 20 additions & 16 deletions src/Sentry.Unity/ViewHierarchyEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,26 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions)

if (_options.BeforeCaptureViewHierarchyInternal?.Invoke() is not false)
{
hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", AttachmentType.ViewHierarchy, "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", AttachmentType.ViewHierarchy, "application/json");
}
else
{
Expand All @@ -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<GameObject>();
Expand Down
20 changes: 10 additions & 10 deletions test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,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()
Expand Down
Loading