Path: redb-route/redb.Route/Telemetry/InstrumentedProcessor.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/redb.Route/Telemetry/InstrumentedProcessor.cs
----
using System.Diagnostics;
using redb.Route.Abstractions;
using redb.Route.Expressions;
namespace redb.Route.Telemetry;
///
/// Wraps an with OpenTelemetry tracing.
/// Creates an span per call,
/// recording exceptions and enriching tags from exchange metadata.
///
public sealed class InstrumentedProcessor : IProcessor
{
private readonly IProcessor _inner;
private readonly string _operationName;
private readonly Func? _nameTemplate;
/// Creates an instrumented processor wrapper.
/// The processor to instrument.
/// Operation name for the activity span (e.g., "route.process", "direct://output send"). Supports ${...} templates resolved at runtime.
public InstrumentedProcessor(IProcessor inner, string operationName)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
_operationName = operationName ?? throw new ArgumentNullException(nameof(operationName));
if (operationName.Contains("${"))
_nameTemplate = ExpressionResolver.GetCompiledTemplate(operationName);
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
var name = _nameTemplate != null ? _nameTemplate(exchange) : _operationName;
using var activity = RouteActivitySource.Source.StartActivity(name, ActivityKind.Internal);
if (activity is { IsAllDataRequested: true })
{
activity.SetTag("redb.route.id", exchange.RouteId);
activity.SetTag("redb.exchange.pattern", exchange.Pattern.ToString());
if (exchange.Properties.TryGetValue(ExchangePropertyKeys.CorrelationId, out var correlationId))
activity.SetTag("redb.correlation.id", correlationId?.ToString());
}
try
{
await _inner.Process(exchange, ct).ConfigureAwait(false);
// The route id is stamped by the first step of the route pipeline, which this span already
// wraps, so at start it was still null and the tag was empty on every route span. Set it
// again now that it is known — the span is open until this method returns.
if (activity is { IsAllDataRequested: true } && !string.IsNullOrEmpty(exchange.RouteId))
activity.SetTag("redb.route.id", exchange.RouteId);
if (activity != null && exchange.Exception != null && !exchange.ExceptionHandled)
{
activity.SetStatus(ActivityStatusCode.Error, exchange.Exception.Message);
activity.RecordException(exchange.Exception);
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Same late stamp as the success path, and this is where attribution matters most: a failing
// span with no route id is the one an operator is actually looking for.
if (activity is { IsAllDataRequested: true } && !string.IsNullOrEmpty(exchange.RouteId))
activity.SetTag("redb.route.id", exchange.RouteId);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
activity?.RecordException(ex);
throw;
}
}
}
///
/// Extension methods for OpenTelemetry activity enrichment.
///
internal static class ActivityExtensions
{
///
/// Records an exception event on the activity. Uses the framework-built-in
/// Activity.AddException (OTel semconv 1.6+). The resulting span event is interoperable with
/// Jaeger / Tempo / OTLP collectors.
///
internal static void RecordException(this Activity activity, Exception ex)
{
activity.AddException(ex);
}
}