Path: redb-route/redb.Route/Telemetry/MeteredProcessor.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/MeteredProcessor.cs ---- using System.Diagnostics; using redb.Route.Abstractions; namespace redb.Route.Telemetry; /// /// Wraps a pipeline processor to collect metrics: exchange count, duration, failures, inflight. /// Designed to wrap the top-level pipeline of a compiled route. /// public sealed class MeteredProcessor : IProcessor { private readonly IProcessor _inner; private readonly string _routeId; private readonly KeyValuePair[] _tags; /// Creates a metered processor wrapper. /// Inner processor (typically the route pipeline). /// Route identifier for metric tags. /// Optional From endpoint URI; emitted as redb.route.endpoint. /// Optional endpoint scheme (e.g. kafka, http); emitted as redb.route.scheme. public MeteredProcessor(IProcessor inner, string routeId, string? endpointUri = null, string? endpointScheme = null) { _inner = inner ?? throw new ArgumentNullException(nameof(inner)); _routeId = routeId ?? throw new ArgumentNullException(nameof(routeId)); var tagList = new List>(3) { new("redb.route.id", _routeId) }; if (!string.IsNullOrEmpty(endpointUri)) tagList.Add(new KeyValuePair("redb.route.endpoint", endpointUri)); if (!string.IsNullOrEmpty(endpointScheme)) tagList.Add(new KeyValuePair("redb.route.scheme", endpointScheme)); _tags = tagList.ToArray(); } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { // `ReadOnlySpan<>` is a ref struct and cannot live across `await`. The // instrument overloads also accept `ReadOnlySpan>` // implicitly created from the array, so passing the array argument // directly preserves the same zero-copy semantics without violating // span-safety rules. RouteMetrics.ExchangesInflight.Add(1, _tags); var sw = Stopwatch.StartNew(); try { await _inner.Process(exchange, ct).ConfigureAwait(false); // An error handler that gives up leaves the failure on the exchange rather than throwing it. if (exchange.Exception is not null && !exchange.ExceptionHandled) RouteMetrics.ExchangesFailed.Add(1, _tags); else RouteMetrics.ExchangesProcessed.Add(1, _tags); } catch (OperationCanceledException) { throw; } catch { RouteMetrics.ExchangesFailed.Add(1, _tags); throw; } finally { sw.Stop(); RouteMetrics.ExchangeDuration.Record(sw.Elapsed.TotalMilliseconds, _tags); RouteMetrics.ExchangesInflight.Add(-1, _tags); } } }