Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Capture and visualize metrics using NewRelic

Component: Metrics
NuGet Package: NServiceBus.Metrics (5.x)
Target Version: NServiceBus 9.x

Introduction

This sample demonstrates how to capture, store, and visualize NServiceBus metrics in NewRelic, a monitoring solution for storing application performance data, custom events, etc.

NewRelic NServiceBus processing time

This sample reports the following metrics to NewRelic:

  • Fetched messages per second
  • Failed messages per second
  • Successful messages per second
  • Critical time in seconds
  • Processing time seconds
  • Retries

For a detailed explanation of these metrics refer to the metrics captured section in the metrics documentation section.

The New Relic NServiceBus integration can be used to get started quickly.

Prerequisites

To run this sample, create a NewRelic account, then download and run the NewRelic agent. See the Introduction to New Relic Insights guide for information on how to get started with NewRelic monitoring.

Code overview

The sample simulates messages load with a random 10% failure rate using the LoadSimulator class:

var simulator = new LoadSimulator(endpointInstance, TimeSpan.Zero, TimeSpan.FromSeconds(10));
simulator.Start();

Capturing metric values

Custom observers need to be registered for the metric probes provided via the NServiceBus.Metrics package:

var metricsOptions = endpointConfiguration.EnableMetrics();

The names provided by the NServiceBus.Metrics probes do not follow the naming conventions recommended by NewRelic. The names can be aligned with the naming conventions defined by NewRelic using the following mapping:

var endpointName = endpointConfiguration.GetSettings().EndpointName();

var nameMapping = new Dictionary<string, string>
{
    // https://docs.newrelic.com/docs/agents/manage-apm-agents/agent-data/collect-custom-metrics
    {"# of msgs successfully processed / sec", FormatMetric("Success_Total", endpointName)},
    {"# of msgs pulled from the input queue /sec", FormatMetric("Fetched_Total", endpointName)},
    {"# of msgs failures / sec", FormatMetric("Failure_Total", endpointName)},
    {"Critical Time", FormatMetric("CriticalTime_Seconds", endpointName)},
    {"Processing Time", FormatMetric("ProcessingTime_Seconds", endpointName)},
};

The registered observers convert NServiceBus.Metric Signals to NewRelic ResponseTimeMetrics and NServiceBus.Metric Durations to NewRelic Metrics.

metricsOptions.RegisterObservers(
    register: probeContext =>
    {
        RegisterProbes(probeContext, endpointName, nameMapping);
    });

During the metric registration, the following steps are required:

  • Map metric names including the endpoint name and message type, if available
  • Register observer callbacks
  • Record response times and metrics in the observer callback
foreach (var duration in context.Durations)
{
    duration.Register((ref DurationEvent @event) =>
    {
        nameMapping.TryGetValue(duration.Name, out var mappedName);
        var newRelicName = string.Format(mappedName ?? FormatMetric(duration.Name, endpointName), Normalize(@event.MessageType));
        NewRelic.RecordResponseTimeMetric(newRelicName, Convert.ToInt64(@event.Duration.TotalMilliseconds));
    });
}

foreach (var signal in context.Signals)
{
    signal.Register((ref SignalEvent @event) =>
    {
        nameMapping.TryGetValue(signal.Name, out var mappedName);
        var newRelicName = string.Format(mappedName ?? FormatMetric(signal.Name, endpointName), Normalize(@event.MessageType));
        NewRelic.RecordMetric(newRelicName, 1);
    });
}

The NewRelic agent needs to be configured to monitor the application by modifying the app.config file:

<appSettings>
  <add key="NewRelic.AgentEnabled" value="true" />
  <add key="NewRelic.AppName" value="NewRelic_Metrics4" />
</appSettings>

Dashboard

A ready-to-use dashboard is available in the official New Relic NServiceBus integration.

Create a custom dashboard

Custom dashboards can be created by using NewRelic Insights. The following steps have to be performed:

  • Create a new dashboard by using the Create a dashboard button under All Dashboards.
  • Open up the Data Explorer, select the corresponding application name and filter for Custom metrics.
  • Click the custom metric to be added and click on Add to an Insights dashboard to add the metric to the previously created dashboard
  • Edit titles if desired

NewRelic Insights Data Explorer

Samples

Metric Version 2 and higher dashboard

Multiple metrics combined