Path: redb-route/tests/redb.Route.Tests/Telemetry/RouteTelemetryProbe.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/Telemetry/RouteTelemetryProbe.cs
----
using System.Diagnostics;
using redb.Route.Telemetry;
namespace redb.Route.Tests.Telemetry;
///
/// Collects the spans of one test from the process-wide route .
///
/// An is process state: filtering by source name alone (the obvious
/// spelling, and the one used until 2026-09-01) makes a test see the spans of every route any other
/// test is running at that moment. That is why the telemetry tests flaked under a full parallel run
/// and were always green in isolation — six clean runs of the untouched tree produced two failures.
/// Serialising the tests by collection cannot help: the listener is not per collection.
///
///
/// So the probe keeps the source filter (there is only one source) and adds the real discriminator:
/// a predicate over the finished activity, normally its redb.route.id tag. Spans of other
/// tests are seen and dropped.
///
///
/// One copy: a connector's test project links this file
/// (<Compile Include="../redb.Route.Tests/Telemetry/RouteTelemetryProbe.cs" Link="Shared/RouteTelemetryProbe.cs" />)
/// and imports redb.Route.Tests.Telemetry.
///
///
internal sealed class RouteTelemetryProbe : IDisposable
{
/// Tag every route span carries: the id of the route the exchange was travelling.
public const string RouteIdTag = "redb.route.id";
/// Tag a transport span carries: the (sanitized) endpoint URI.
public const string EndpointTag = "redb.route.endpoint";
private readonly ActivityListener _listener;
private readonly List _collected = [];
// A plain object as the lock.
private readonly object _sync = new();
private readonly Func _isMine;
/// Collects the finished spans accepts.
public RouteTelemetryProbe(Func isMine)
{
_isMine = isMine ?? throw new ArgumentNullException(nameof(isMine));
_listener = new ActivityListener
{
ShouldListenTo = source => source.Name == RouteActivitySource.SourceName,
Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllDataAndRecorded,
ActivityStopped = Collect,
};
ActivitySource.AddActivityListener(_listener);
}
/// Collects spans whose redb.route.id begins with .
public static RouteTelemetryProbe ForRouteIdPrefix(string routeIdPrefix)
=> new(activity => Tag(activity, RouteIdTag)?.StartsWith(routeIdPrefix, StringComparison.Ordinal) == true);
/// Collects spans whose redb.route.endpoint contains (a port, a path).
public static RouteTelemetryProbe ForEndpointContaining(string fragment)
=> new(activity => Tag(activity, EndpointTag)?.Contains(fragment, StringComparison.Ordinal) == true);
/// The spans collected so far, oldest first.
public IReadOnlyList Activities
{
get { lock (_sync) return [.. _collected]; }
}
/// Value of on , or null.
public static string? Tag(Activity activity, string tag) => activity.GetTagItem(tag)?.ToString();
///
public void Dispose() => _listener.Dispose();
// ActivityStopped fires on whatever thread finished the span, and several routes may finish at once.
private void Collect(Activity activity)
{
if (!_isMine(activity)) return;
lock (_sync) _collected.Add(activity);
}
}