Path: redb/redb.Tests.Integration/Tests/Diagnostics/RedbTelemetryTests.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Diagnostics/RedbTelemetryTests.cs
----
using System.Diagnostics;
using AwesomeAssertions;
using redb.Core.Diagnostics;
using Xunit;
namespace redb.Tests.Integration.Tests.Diagnostics;
///
/// The redb.Core ActivitySource: one span per database command, carrying the operation name and
/// the database system only. The source is inert without a subscriber, so nothing is emitted by default.
/// Asserted through an , the way redb.Route reads its own spans.
///
public class RedbTelemetryTests
{
private static (ActivityListener Listener, List Spans) Listen()
{
var spans = new List();
var listener = new ActivityListener
{
ShouldListenTo = source => source.Name == RedbActivitySource.SourceName,
Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllDataAndRecorded,
ActivityStopped = spans.Add,
};
ActivitySource.AddActivityListener(listener);
return (listener, spans);
}
[Fact]
public void WithNoSubscriber_TheSourceIsOff()
{
RedbActivitySource.SourceName.Should().Be("redb.Core");
RedbTelemetry.Enabled.Should().BeFalse("nothing subscribes, so a command is run straight through");
}
[Fact]
public async Task AQuery_OpensOneSpan_CarryingOnlyTheOperation()
{
var (listener, spans) = Listen();
try
{
var rows = await RedbTelemetry.RunAsync(
RedbTelemetry.Query, () => Task.FromResult(new List { 1, 2, 3 }));
rows.Should().Equal(1, 2, 3);
var span = spans.Should().ContainSingle().Which;
span.DisplayName.Should().Be("redb.query");
span.GetTagItem("db.system").Should().Be("redb");
span.GetTagItem("db.operation").Should().Be("query");
span.GetTagItem("db.statement").Should().BeNull("the statement text is never captured");
}
finally
{
listener.Dispose();
}
}
[Fact]
public async Task AFailingCommand_MarksTheSpanAnError_AndRethrows()
{
var (listener, spans) = Listen();
try
{
var act = () => RedbTelemetry.RunAsync(
RedbTelemetry.Execute, () => Task.FromException(new InvalidOperationException("boom")));
(await act.Should().ThrowAsync()).WithMessage("boom");
spans.Should().ContainSingle().Which.Status.Should().Be(ActivityStatusCode.Error);
}
finally
{
listener.Dispose();
}
}
[Fact]
public async Task ACancelledCommand_DoesNotMarkTheSpanAnError()
{
var (listener, spans) = Listen();
try
{
var act = () => RedbTelemetry.RunAsync(
RedbTelemetry.Query, () => Task.FromException(new OperationCanceledException()));
await act.Should().ThrowAsync();
spans.Should().ContainSingle().Which.Status.Should().NotBe(ActivityStatusCode.Error);
}
finally
{
listener.Dispose();
}
}
}