Path: redb-route/redb.Route/Telemetry/MeteredStepProcessor.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/MeteredStepProcessor.cs
----
using System.Diagnostics;
using redb.Route.Abstractions;
namespace redb.Route.Telemetry;
///
/// Wraps a processor pipeline to collect per-step metrics: count, duration, failures.
/// Unlike (route-level), this tags metrics with
/// both redb.route.id and redb.route.step.
///
public sealed class MeteredStepProcessor : IProcessor
{
private readonly IProcessor _inner;
private readonly string _stepName;
private readonly IReadOnlyList<(string Name, Func Resolver)>? _tagProviders;
/// Creates a per-step metered processor wrapper.
/// Inner processor (the metered sub-pipeline).
/// Static step name for metric tags.
public MeteredStepProcessor(IProcessor inner, string stepName)
: this(inner, stepName, null) { }
/// Creates a per-step metered processor wrapper with dynamic tag providers.
/// Inner processor (the metered sub-pipeline).
/// Static step name for metric tags.
/// Optional providers evaluated per message for additional metric dimensions.
public MeteredStepProcessor(IProcessor inner, string stepName, IReadOnlyList<(string Name, Func Resolver)>? tagProviders)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
_stepName = stepName ?? throw new ArgumentNullException(nameof(stepName));
_tagProviders = tagProviders;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
var tags = new TagList
{
{ "redb.route.id", exchange.RouteId },
{ "redb.route.step", _stepName }
};
if (_tagProviders != null)
{
for (int i = 0; i < _tagProviders.Count; i++)
{
var (name, resolver) = _tagProviders[i];
object? value;
try { value = resolver(exchange); }
catch { value = null; }
tags.Add(name, value);
}
}
var sw = Stopwatch.StartNew();
try
{
await _inner.Process(exchange, ct).ConfigureAwait(false);
StepMetrics.StepProcessed.Add(1, tags);
}
catch (OperationCanceledException)
{
throw;
}
catch
{
StepMetrics.StepFailed.Add(1, tags);
throw;
}
finally
{
sw.Stop();
StepMetrics.StepDuration.Record(sw.Elapsed.TotalMilliseconds, tags);
}
}
}