Consume raw data from Metrics

Component: Metrics
NuGet Package NServiceBus.Metrics (1.1)
Target NServiceBus Version: 6.x

When Performance Counters reporting and Service Control reporting is not enough, it's possible to consume raw metrics data, directly attaching to the public API provided by the package. First, the Metrics themselves need to be enabled. Then, a custom reporter can be attached to send data to any collector e.g. Service Control, Azure Application Insights, etc.

Enabling NServiceBus.Metrics

var metrics = endpointConfiguration.EnableMetrics();

Reporting metrics data

Metrics can be reported in a few different ways.

To any external storage

Custom observers might be registered to access every value reported by probes.

metrics.RegisterObservers(
    register: context =>
    {
        foreach (var duration in context.Durations)
        {
            duration.Register(
                observer: length =>
                {
                    Console.WriteLine($"Duration: '{duration.Name}'. Value: '{length}'");
                });
        }
        foreach (var signal in context.Signals)
        {
            signal.Register(
                observer: () =>
                {
                    Console.WriteLine($"Signal: '{signal.Name}'");
                });
        }
    });
Methods provided below that enable logging metrics data are obsoleted in version 1.1 and will be removed in the next major version.

To NServiceBus log

Metrics data can be written to the NServiceBus Log.

metrics.EnableLogTracing(interval: TimeSpan.FromMinutes(5));
By default metrics will be written to the log at the DEBUG log level. The API allows this parameter to be customized.
metrics.EnableLogTracing(
    interval: TimeSpan.FromMinutes(5),
    logLevel: LogLevel.Info);

To trace log

Metrics data can be written to System.Diagnostics.Trace.

metrics.EnableMetricTracing(interval: TimeSpan.FromSeconds(5));

To custom function

Metrics data can be consumed by a custom function.

metrics.EnableCustomReport(
    func: data =>
    {
        // process metrics
        return Task.CompletedTask;
    },
    interval: TimeSpan.FromSeconds(5));

Samples


Last modified