Windows Performance Counters

Source
NuGet Package NServiceBus.Metrics.PerformanceCounters (4.x)
Target NServiceBus Version: 7.x

When a system is broken down into multiple processes - each with its own queue - it allows identifying which process is the bottleneck by examining how many messages (on average) are in each queue. However, it is not possible to know how long messages are waiting in each queue - which is the primary indicator of a bottleneck - without knowing the rate of messages coming into each queue, and the rate at which messages are being processed from each queue.

Despite the many performance counters Microsoft provides for MSMQ (including messages in queues, machine-wide incoming and outgoing messages per second, and the total messages in all queues), there is no built-in performance counter for the time it takes a message to get through each queue.

NServiceBus includes several performance counters to address this. They are installed in the NServiceBus category.

Since all performance counters in Windows are exposed via Windows Management Instrumentation (WMI), it is very straightforward to pull this information into the existing monitoring infrastructure.

While the standard performance counters in the NServiceBus 6 package are marked obsolete, for compatibility reasons they are still enabled by default.

Counters

All counters are defined in the NuGet package dependency NServiceBus.Metrics. The dependency is automatically pulled in.

For more information about the metrics defined consult the Metrics documentation page.

Configuration

The counters can be enabled using the the following code:

var performanceCounters = endpointConfiguration.EnableWindowsPerformanceCounters();

In the NServiceBus host the counters are enabled by default.

Setting up an SLA value can be done using the following code:

var performanceCounters = endpointConfiguration.EnableWindowsPerformanceCounters();
performanceCounters.EnableSLAPerformanceCounters(TimeSpan.FromMinutes(3));

Installing counters

The NServiceBus.Metrics.PerformanceCounter package installs itself into the MSBuild pipeline and generates all performance counter installation scripts at compile time. It does this by interrogating types (in the target assembly) to infer what scripts to create. It is required for any project where performance counter installation scripts are needed.

Performance Counters versions 1.1 and above: depend on NServiceBus.Metrics to calculate counter values.

Script creation

Scripts are created in the directory format of [CurrentProjectDebugDir]\[.NET Version]\NServiceBus.Metrics.PerformanceCounters.

For example, a project named ClassLibrary built in Debug mode will have the following directories created:

  • ClassLibrary\bin\Debug\[.NET Version]\NServiceBus.Metrics.PerformanceCounters

Scripts are also included in the list of project output files. The files that are produced will be copied to the output directory of any project that references it. If a script needs to be copied to a directory for inclusion in source control repository, use a post-build event to copy the output outside the build directory.

Force category recreation

The descriptions of some of the performance counters have been changed in the PowerShell script from previous versions for more clarity when viewing counters in the Performance Monitor UI.

By default, the PowerShell script will not recreate counters just to change the descriptions. To force it to recreate the counters, the -ForceRecreate switch can be used:

PS> .\CreateNSBPerfCounters.ps1 -ForceRecreate

Script usage

To use the Powershell script to create performance counters, use the following call with elevated permissions:

./CreateNSBPerfCounters.ps1

To list the installed counters:

Get-Counter -ListSet NServiceBus | Select-Object -ExpandProperty Counter
Metrics are defined in the NServiceBus.Metrics NuGet package and will be dynamically turned into performance counters. When the NServiceBus.Metrics dependency is updated, new counters might be available in the installation script. Make sure the scripts are executed with elevated permissions on required machines when the scripts have been updated.

Performance Monitor Users local security group

When running installers, the service account will be automatically added to the local Performance Monitor Users group if executed with elevated privileges.

System.InvalidOperationException

If the endpoint instance throws one of the following exceptions at startup, then the performance counters need to be reinstalled

  • InvalidOperationException: The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly.
  • InvalidOperationException: NServiceBus performance counter for 'Critical Time' is not set up correctly.

Corrupted Counters

Corrupted performance counters can cause the endpoint to either hang completely during startup or fail with the following error:

NServiceBus performance counter for '{counterName}' is not set up correctly

Should this happen, try rebuilding the performance counter library using the following steps:

  1. Open an elevated command prompt
  2. Execute the following command to rebuild the performance counter library: lodctr /r

More information

Send-only endpoints are currently not supported since they don't receive messages.

Samples

Related Articles

  • Auditing Messages
    Configure where to send messages and it provides built-in message auditing for every endpoint.
  • Management using PowerShell
    Install the infrastructure for NServiceBus on servers using PowerShell.
  • Operations
    Operations Table of Contents.
  • Recoverability
    Explains how exceptions are handled, and actions retried, during message processing.

Last modified