Capture and visualize metrics using NewRelic

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

Introduction

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

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 in Version 2 and later

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

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 NServiceBus.Metrics. This is configured in the NewRelicFeature:

metricsOptions = settings.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 = settings.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 ResponseTimeMetric 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));
        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));
        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_Metrics2" />
</appSettings>

Dashboard

Create

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 1 dashboard

Metric Version 2 and higher dashboard

Multiple metrics combined


Last modified