TurboMediator
Observability

Metrics

Detailed request metrics with System.Diagnostics.Metrics

The metrics behavior provides detailed request-level metrics using System.Diagnostics.Metrics, independent of the telemetry behavior.

Configuration

builder.Services.AddTurboMediator(m =>
{
    m.WithMetrics<IMessage, object>(options =>
    {
        options.MeterName = "MyApp.Mediator";
        options.MeterVersion = "1.0.0";
    });
});

MetricsOptions

OptionDefaultDescription
EnableLatencyHistogramtrueRecord request duration histogram
EnableThroughputCountertrueCount total requests
EnableErrorCountertrueCount errors
EnableInFlightGaugetrueTrack in-flight requests
LatencyBucketsDefaultCustom histogram buckets
CustomLabels[]Additional label names (IList<string>)
CustomLabelValuesProvidernullProvider for custom label values
MeterName"TurboMediator"Meter name for filtering
MeterVersion"1.0.0"Meter version

Instruments Created

InstrumentTypeDescription
turbomediator.handler.durationHistogramHandler execution duration (ms)
turbomediator.handler.requestsCounterTotal handler invocations
turbomediator.handler.errorsCounterHandler errors
turbomediator.handler.inflightUpDownCounterCurrently executing handlers

All metrics include message_type as a label.

Practical Example

builder.Services.AddTurboMediator(m =>
{
    m.WithMetrics<IMessage, object>(options =>
    {
        options.LatencyBuckets = new double[] { 5, 10, 25, 50, 100, 250, 500, 1000 };
        options.CustomLabels = new List<string> { "service", "environment" };
        options.CustomLabelValuesProvider = () => new Dictionary<string, object?>
        {
            ["service"] = "order-service",
            ["environment"] = builder.Environment.EnvironmentName
        };
    });
});

// Export to Prometheus
builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics =>
    {
        metrics
            .AddMeter("TurboMediator")
            .AddPrometheusExporter();
    });

On this page