Skip to content

Metrics

Note

This applies to the latest version (v2.x) of the library. If you are using an earlier version, please refer to the migration guide.

Metrics are emitted automatically for all channels. Exporting them is handled by the host application using OpenTelemetry or any compatible metrics exporter.

Channels emits metrics under the channels meter name. When configuring OpenTelemetry, make sure to include this meter name in the configuration.

builder.Services.AddOpenTelemetry()
    .WithMetrics( metrics =>
    {
        metrics.AddMeter( "channels" );
    } );

Note

Channels emits metrics only. Exporting is handled by the host application.

Available Metrics

The following metrics are emitted:

  • bytes.received: Total number of bytes received.
  • bytes.sent: Total number of bytes sent.
  • active: Current number of active channels.
  • duration: Channel lifetime duration in milliseconds (histogram).
  • middleware.exceptions: Total number of middleware execution failures.
  • idle.timeouts: Total number of channels closed due to idle timeout.

Custom Tags

You can attach additional tags to emitted metrics by configuring a tag factory.

services.AddChannels( channel =>
{
    channel.Configure( options =>
    {
        options.ConfigureMetricsTags( channelInfo =>
        {
            var tags = new TagList
            {
                { "channel.id", channelInfo.Id }
            };

            if ( channelInfo.Data.TryGetValue( "device_id", out var deviceId ) )
            {
                tags.Add( "device.id", deviceId );
            }

            return tags;
        } );
    } );
} );

Important

Be careful when adding high-cardinality tags (such as dynamic identifiers). High-cardinality tags can significantly increase memory usage and storage costs in metrics backends.

Exporting Metrics

Channels does not include an exporter.

To export metrics, use OpenTelemetry with the exporter of your choice.

Example: Prometheus

Install the required packages:

dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore

Then configure OpenTelemetry to use the Prometheus exporter:

builder.Services.AddOpenTelemetry()
    .WithMetrics( metrics =>
    {
        metrics
            .AddMeter( "channels" )
            .AddPrometheusExporter();
    } );
...

app.UseOpenTelemetryPrometheusScrapingEndpoint();