Path: redb-route/redb.Route/Definitions/MeteredDefinition.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/Definitions/MeteredDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Processors;
using redb.Route.Telemetry;
namespace redb.Route.Definitions;
///
/// Scope-opener definition for per-step metrics collection.
/// All child steps run inside a single that records
/// redb.route.step.processed, redb.route.step.failed,
/// and redb.route.step.duration tagged with .
/// Inherits the leaf DSL from ;
/// close with or the universal .
///
public class MeteredDefinition : RouteDefinitionBase, IRouteScope
{
/// Step name used as a metric tag value for redb.route.step.
public string StepName { get; }
private readonly List<(string Name, Func Resolver)> _tagProviders = new();
/// Tag providers evaluated per message to enrich the recorded metrics with low-cardinality dimensions.
public IReadOnlyList<(string Name, Func Resolver)> TagProviders => _tagProviders;
internal MeteredDefinition(string stepName)
{
ArgumentException.ThrowIfNullOrWhiteSpace(stepName);
StepName = stepName;
}
///
/// Adds a dimension/label evaluated per message. Use only with bounded value domains
/// (status codes, tenant ids from a small set, etc.) to keep cardinality under control.
///
public MeteredDefinition Tag(string name, Func resolver)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(resolver);
_tagProviders.Add((name, resolver));
return this;
}
/// Convenience: tag the metric with the value of a header.
public MeteredDefinition TagFromHeader(string tagName, string headerName)
{
ArgumentException.ThrowIfNullOrWhiteSpace(headerName);
return Tag(tagName, e => e.In.Headers.TryGetValue(headerName, out var v) ? v : null);
}
// ── Navigation ─────────────────────────────────────────────────────────────
/// Closes this metered scope and returns the parent route definition.
/// Thrown if no parent route is set.
public IRouteDefinition EndMetered()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndMetered() called without a parent route. Ensure Metered() was called on a route definition."));
///
public IRouteDefinition End() => EndMetered();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
IProcessor body = BuildPipeline(Outputs, context);
return new MeteredStepProcessor(body, StepName, _tagProviders.Count > 0 ? _tagProviders.ToArray() : null);
}
private static IProcessor BuildPipeline(IList outputs, IRouteContext context)
=> NodePipeline.Body(context, outputs);
}