Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
### Fixes

- The HTTP instrumentation uses the span created for the outgoing request in the sentry-trace header, fixing the parent-child relationship between client and server ([#4264](https://github.com/getsentry/sentry-dotnet/pull/4264))
- ExtraData not captured for Breadcrumbs in MauiEventsBinder ([#4254](https://github.com/getsentry/sentry-dotnet/pull/4254))
- NOTE: Required breaking changes to the public API of `Sentry.Maui.BreadcrumbEvent`, while keeping an _Obsolete_ constructor for backward compatibility.
- InvalidOperationException sending attachments on Android with LLVM enabled ([#4276](https://github.com/getsentry/sentry-dotnet/pull/4276))

### Dependencies
Expand Down
71 changes: 71 additions & 0 deletions src/Sentry.Maui/BreadcrumbEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace Sentry.Maui;

/// <summary>
/// Argument to the OnBreadcrumbCreateCallback
/// </summary>
public sealed class BreadcrumbEvent
{
/// <summary>
/// The sender of the event, usually the control that triggered it.
/// </summary>
public object? Sender { get; }

/// <summary>
/// The event name (e.g. "Tapped", "Swiped", etc.)
/// </summary>
public string EventName { get; }

/// <summary>
/// Any extra data to be included in the breadcrumb. This would typically be event specific information (for example
/// it could include the X, Y coordinates of a tap event).
/// </summary>
public IEnumerable<KeyValuePair<string, string>> ExtraData { get; }

/// <summary>
/// Creates a new BreadcrumbEvent
/// </summary>
public BreadcrumbEvent(object? sender, string eventName)
: this(sender, eventName, Array.Empty<KeyValuePair<string, string>>())
{
}

/// <summary>
/// Creates a new BreadcrumbEvent
/// </summary>
public BreadcrumbEvent(
object? sender,
string eventName,
params IEnumerable<KeyValuePair<string, string>> extraData)
{
Sender = sender;
EventName = eventName;
ExtraData = extraData;
}

/// <summary>
/// Creates a new BreadcrumbEvent
/// </summary>
public BreadcrumbEvent(
object? sender,
string eventName,
params IEnumerable<(string key, string value)> extraData) : this(sender, eventName, extraData.Select(
Copy link
Member

@Flash0ver Flash0ver Jun 17, 2025

Choose a reason for hiding this comment

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

final comment: deferred execution

The Select assigned to ExtraData will have deferred execution.
Is this expected / intended?

On the other hand, since we accept IEnumerable in the .ctors, the user could pass IEnumerables with deferred execution anyway.

On second thought ... I believe this is great as it is.
Also considering we're enumerating through it only a single time, so materializing the IEnumerable beforehand would actually be slightly slower and allocate more memory.

e => new KeyValuePair<string, string>(e.key, e.value)))
{
}

/// <summary>
/// This constructor remains for backward compatibility.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventName"></param>
/// <param name="extraData"></param>
[Obsolete("Use one of the other simpler constructors instead.")]
public BreadcrumbEvent(
object? sender,
string eventName,
IEnumerable<(string Key, string Value)>[] extraData) : this(sender, eventName, extraData.SelectMany(
x => x.Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value)))
)
{
}
}
12 changes: 0 additions & 12 deletions src/Sentry.Maui/IMauiElementEventBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,3 @@ public interface IMauiElementEventBinder
/// <param name="element"></param>
public void UnBind(VisualElement element);
}

/// <summary>
/// Breadcrumb arguments
/// </summary>
/// <param name="Sender"></param>
/// <param name="EventName"></param>
/// <param name="ExtraData"></param>
public record BreadcrumbEvent(
object? Sender,
string EventName,
params IEnumerable<(string Key, string Value)>[] ExtraData
);
10 changes: 5 additions & 5 deletions src/Sentry.Maui/Internal/MauiButtonEventsBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ namespace Sentry.Maui.Internal;
/// <inheritdoc />
public class MauiButtonEventsBinder : IMauiElementEventBinder
{
private Action<BreadcrumbEvent>? addBreadcrumbCallback;
private Action<BreadcrumbEvent>? _addBreadcrumbCallback;

/// <inheritdoc />
public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb)
{
addBreadcrumbCallback = addBreadcrumb;
_addBreadcrumbCallback = addBreadcrumb;

if (element is Button button)
{
Expand All @@ -30,11 +30,11 @@ public void UnBind(VisualElement element)
}

private void OnButtonOnClicked(object? sender, EventArgs _)
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Clicked)));
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Clicked)));

private void OnButtonOnPressed(object? sender, EventArgs _)
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Pressed)));
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Pressed)));

private void OnButtonOnReleased(object? sender, EventArgs _)
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Released)));
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Released)));
}
9 changes: 8 additions & 1 deletion src/Sentry.Maui/Internal/MauiEventsBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ internal void OnBreadcrumbCreateCallback(BreadcrumbEvent breadcrumb)
breadcrumb.Sender,
breadcrumb.EventName,
UserType,
UserActionCategory
UserActionCategory,
extra =>
{
foreach (var (key, value) in breadcrumb.ExtraData)
{
extra[key] = value;
}
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private static void OnPointerEnteredGesture(object? sender, PointerEventArgs e)
ToPointerData(e)
));

private static IEnumerable<(string Key, string Value)> ToPointerData(PointerEventArgs e) =>
private static (string Key, string Value)[] ToPointerData(PointerEventArgs e) =>
[
#if ANDROID
("MotionEventAction", e.PlatformArgs?.MotionEvent.Action.ToString() ?? string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ namespace Microsoft.Maui.Hosting
}
namespace Sentry.Maui
{
public class BreadcrumbEvent : System.IEquatable<Sentry.Maui.BreadcrumbEvent>
public sealed class BreadcrumbEvent
{
public BreadcrumbEvent(object? Sender, string EventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
public BreadcrumbEvent(object? sender, string eventName) { }
public BreadcrumbEvent(object? sender, string eventName, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> extraData) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"key",
"value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>> extraData) { }
[System.Obsolete("Use one of the other simpler constructors instead.")]
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})] params System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] ExtraData) { }
public string EventName { get; init; }
[System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})]
public System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] ExtraData { get; init; }
public object? Sender { get; init; }
"Value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] extraData) { }
public string EventName { get; }
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> ExtraData { get; }
public object? Sender { get; }
}
public interface IMauiElementEventBinder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ namespace Microsoft.Maui.Hosting
}
namespace Sentry.Maui
{
public class BreadcrumbEvent : System.IEquatable<Sentry.Maui.BreadcrumbEvent>
public sealed class BreadcrumbEvent
{
public BreadcrumbEvent(object? Sender, string EventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
public BreadcrumbEvent(object? sender, string eventName) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.ParamCollection] System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> extraData) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.ParamCollection] [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"key",
"value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>> extraData) { }
[System.Obsolete("Use one of the other simpler constructors instead.")]
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})] params System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] ExtraData) { }
public string EventName { get; init; }
[System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})]
public System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] ExtraData { get; init; }
public object? Sender { get; init; }
"Value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] extraData) { }
public string EventName { get; }
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> ExtraData { get; }
public object? Sender { get; }
}
public interface IMauiElementEventBinder
{
Expand Down
28 changes: 28 additions & 0 deletions test/Sentry.Maui.Tests/BreadcrumbEventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace Sentry.Maui.Tests;

public class BreadcrumbEventTests
{
[Fact]
public void BreadcrumbEvent_OldConstructor_EquivalentToNewConstructor()
{
// Arrange
var sender = new object();
var eventName = "TestEvent";

// Act
IEnumerable<(string Key, string Value)>[] extraData = [[("key1", "value1")], [("key2", "value2")]];
#pragma warning disable CS0618 // Type or member is obsolete
var oldEvent = new BreadcrumbEvent(sender, eventName, extraData);
#pragma warning restore CS0618 // Type or member is obsolete
var newEvent = new BreadcrumbEvent(sender, eventName, ("key1", "value1"), ("key2", "value2"));

// Assert
oldEvent.Sender.Should().Be(newEvent.Sender);
oldEvent.EventName.Should().Be(newEvent.EventName);
oldEvent.ExtraData.Should().BeEquivalentTo(newEvent.ExtraData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Sentry.Maui.Tests;

public partial class MauiEventsBinderTests
public class MauiButtonEventsBinderTests
{
private readonly MauiEventsBinderFixture _fixture = new(new MauiButtonEventsBinder());

[Theory]
[InlineData(nameof(Button.Clicked))]
[InlineData(nameof(Button.Pressed))]
Expand Down
33 changes: 33 additions & 0 deletions test/Sentry.Maui.Tests/MauiEventsBinderFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Sentry.Maui.Internal;

namespace Sentry.Maui.Tests;

internal class MauiEventsBinderFixture
{
public IHub Hub { get; }

public MauiEventsBinder Binder { get; }

public Scope Scope { get; } = new();

public SentryMauiOptions Options { get; } = new();

public MauiEventsBinderFixture(params IEnumerable<IMauiElementEventBinder> elementEventBinders)
{
Hub = Substitute.For<IHub>();
Hub.SubstituteConfigureScope(Scope);

Scope.Transaction = Substitute.For<ITransactionTracer>();

Options.Debug = true;
var logger = Substitute.For<IDiagnosticLogger>();
logger.IsEnabled(Arg.Any<SentryLevel>()).Returns(true);
Options.DiagnosticLogger = logger;
var options = Microsoft.Extensions.Options.Options.Create(Options);
Binder = new MauiEventsBinder(
Hub,
options,
elementEventBinders
);
}
}
54 changes: 24 additions & 30 deletions test/Sentry.Maui.Tests/MauiEventsBinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,35 @@ namespace Sentry.Maui.Tests;

public partial class MauiEventsBinderTests
{
private class Fixture
{
public IHub Hub { get; }
private readonly MauiEventsBinderFixture _fixture = new();

public MauiEventsBinder Binder { get; }
// Most of the tests for this class are in separate partial class files for better organisation

public Scope Scope { get; } = new();
[Fact]
public void OnBreadcrumbCreateCallback_CreatesBreadcrumb()
{
// Arrange
var breadcrumbEvent = new BreadcrumbEvent(new object(), "TestName",
("key1", "value1"), ("key2", "value2")
);

public SentryMauiOptions Options { get; } = new();
// Act
_fixture.Binder.OnBreadcrumbCreateCallback(breadcrumbEvent);

public Fixture()
// Assert
using (new AssertionScope())
{
Hub = Substitute.For<IHub>();
Hub.SubstituteConfigureScope(Scope);

Scope.Transaction = Substitute.For<ITransactionTracer>();

Options.Debug = true;
var logger = Substitute.For<IDiagnosticLogger>();
logger.IsEnabled(Arg.Any<SentryLevel>()).Returns(true);
Options.DiagnosticLogger = logger;
var options = Microsoft.Extensions.Options.Options.Create(Options);
Binder = new MauiEventsBinder(
Hub,
options,
[
new MauiButtonEventsBinder(),
new MauiImageButtonEventsBinder(),
new MauiGestureRecognizerEventsBinder()
]
);
var crumb = Assert.Single(_fixture.Scope.Breadcrumbs);
Assert.Equal("Object.TestName", crumb.Message);
Assert.Equal(BreadcrumbLevel.Info, crumb.Level);
Assert.Equal(MauiEventsBinder.UserType, crumb.Type);
Assert.Equal(MauiEventsBinder.UserActionCategory, crumb.Category);
Assert.NotNull(crumb.Data);
Assert.Equal(breadcrumbEvent.ExtraData.Count(), crumb.Data.Count);
foreach (var (key, value) in breadcrumbEvent.ExtraData)
{
crumb.Data.Should().Contain(kvp => kvp.Key == key && kvp.Value == value);
}
}
}

private readonly Fixture _fixture = new();

// Tests are in partial class files for better organization
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Sentry.Maui.Tests;

public partial class MauiEventsBinderTests
public class MauiGestureRecognizerEventsBinderTests
{
private readonly MauiEventsBinderFixture _fixture = new(new MauiGestureRecognizerEventsBinder());

[SkippableFact]
public void TapGestureRecognizer_LifecycleEvents_AddsBreadcrumb()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Sentry.Maui.Tests;

public partial class MauiEventsBinderTests
public class MauiImageButtonEventsBinderTests
{
private readonly MauiEventsBinderFixture _fixture = new(new MauiImageButtonEventsBinder());

[Theory]
[InlineData(nameof(ImageButton.Clicked))]
[InlineData(nameof(ImageButton.Pressed))]
Expand Down
Loading