Path: redb-route/tests/redb.Route.Tests.SignalR/SignalRTraceTests.cs
Snapshot: redb-route, 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-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.SignalR/SignalRTraceTests.cs
----
using System.Diagnostics;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using redb.Route.Abstractions;
using redb.Route.Configuration;
using redb.Route.Core;
using redb.Route.SignalR;
using redb.Route.Tests.Telemetry;
namespace redb.Route.Tests.SignalR;
///
/// A hub message carries no headers: the consumer opens a root span per exchange that reached the route, linked to the
/// trace context of the connection request when the client sent one, never a child of the connection's activity or of
/// whoever started the routes. A failed route marks it red; the connection's own abort does not.
/// EnableTelemetry=false opens no span on either side.
///
public sealed class SignalRTraceTests
{
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10);
private readonly int _port = global::redb.Route.Tests.Shared.TestPorts.Next();
private string HubUri => $"signalr://127.0.0.1:{_port}/traced";
private string HubUrl => $"http://127.0.0.1:{_port}/traced";
private RouteTelemetryProbe Spans(ActivityKind kind)
=> new(a => a.Kind == kind && RouteTelemetryProbe.Tag(a, RouteTelemetryProbe.EndpointTag)?.Contains($":{_port}", StringComparison.Ordinal) == true);
private async Task StartHub(Func step, bool telemetry = true, string query = "")
{
var ctx = new RouteContext(options: new RouteEngineOptions { EnableTelemetry = telemetry });
ctx.AddComponent(new SignalRComponent());
ctx.AddRoutes(r => r.From(HubUri + query).Process(step));
using (new Activity("host startup").SetIdFormat(ActivityIdFormat.W3C).Start())
await ctx.Start();
return ctx;
}
private async Task Connect(string? traceparent = null)
{
var connection = new HubConnectionBuilder()
.WithUrl(HubUrl, o =>
{
if (traceparent is not null)
o.Headers["traceparent"] = traceparent;
})
.Build();
await connection.StartAsync();
return connection;
}
private static async Task Until(Func done)
{
var deadline = DateTime.UtcNow + Wait;
while (!done() && DateTime.UtcNow < deadline)
await Task.Delay(50);
}
[Fact]
public async Task Each_exchange_opens_a_root_span_linked_to_the_handshake_context()
{
// The host traces its requests: the connection request's server span is the ambient activity of the hub.
using var aspNet = new ActivityListener
{
ShouldListenTo = s => s.Name == "Microsoft.AspNetCore",
Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllDataAndRecorded,
};
ActivitySource.AddActivityListener(aspNet);
using var probe = Spans(ActivityKind.Consumer);
var handshake = new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded);
await using (await StartHub((_, _) => Task.CompletedTask))
{
await using var connection = await Connect($"00-{handshake.TraceId}-{handshake.SpanId}-01");
await connection.InvokeAsync