Net.Observability.Metrics.Windows 1.3.1

dotnet add package Net.Observability.Metrics.Windows --version 1.3.1
NuGet\Install-Package Net.Observability.Metrics.Windows -Version 1.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Net.Observability.Metrics.Windows" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Net.Observability.Metrics.Windows --version 1.3.1
#r "nuget: Net.Observability.Metrics.Windows, 1.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Net.Observability.Metrics.Windows as a Cake Addin
#addin nuget:?package=Net.Observability.Metrics.Windows&version=1.3.1

// Install Net.Observability.Metrics.Windows as a Cake Tool
#tool nuget:?package=Net.Observability.Metrics.Windows&version=1.3.1

Abstract

This nuget provides you with all the logic for custom Windows counters registration to be hosted on a dedicated console app. The produced artifact need to be packed on target service project that is using the counters.

Whenever you install this nuget, you get:

  • the logic for counters registration you need to extend (see more about the needed customizations below)
  • a Program.cs already with the launch of counter registration
    • Note: it's not added to you project but will be compiled on build
    • Important: if you receive error MSB4018 during package installation on phase ProduceContentAssets, this is caused by a too long project path. Try move up the solution on the file system hierarchy and then retry installation.

How to set/reset counter value from calling target service code

    ...
    //global IPerformanceCounter counter type
    PerformanceCounters.ProcessingTasksCurrentTotal.Increment();

    //resetting value (for instance at initialization)
    PerformanceCounters.ProcessingTasksCurrentTotal.RawValue = 0;


    //instance based IPerformanceCounter counter type
    PerformanceCounters.GetCounterFor(PerformanceCounters.ProcessingTasksCurrentTotal, instanceName).Increment();

    //resetting value (for instance at initialization)
    PerformanceCounters.GetCounterFor(PerformanceCounters.ProcessingTasksCurrentTotal, instanceName).RawValue = 0;

    ...
    ...

    //global AverageCounter counter type
    PerformanceCounters.OutboundMessageProcessingTimeTotal.RegisterExact(stopwatchInstance.ElapsedMilliseconds);

    //resetting value (for instance at initialization)
    PerformanceCounters.OutboundMessageProcessingTimeTotal.Reset();


    //instance based AverageCounter counter type
    PerformanceCounters.GetCounterFor(PerformanceCounters.OutboundMessageProcessingTimeTotal, statefulQueueName).RegisterExact(stopwatchInstance.ElapsedMilliseconds);

    //resetting value (for instance at initialization)
    PerformanceCounters.GetCounterFor(PerformanceCounters.OutboundMessageProcessingTimeTotal, statefulQueueName).Reset();
    ...

Customizations required

1st needed customization

Following you see the code to add to your console app, for configuring counters to create and then made available for target calling service. Substitute $rootnamespace$ placeholder with console app name (generally equals to default namespace).

using System.Diagnostics;
using Net.Observability.Metrics.Windows.Instrumentation;

namespace $rootnamespace$.Instrumentation
{

    public static partial class PerformanceCounters
    {
        public static readonly string CategoryName = "YOUR_SPECIFIC_COUNTER_CATEGORY";//this must not be shared between different hosting projects
        public static readonly string CategoryHelp = "YOUR_SPECIFIC_COUNTER_CATEGORY_HELP";


    #region CONFIGURE REGIONS - LOOK AT INNNER REGIONS

        #region CONFIGURE - Custom Counter Name Definition

            const string ProcessingTasksCurrentCounterName = "Processing Tasks Current";
            const string OutboundMessageProcessingTimeCounterName = "Outbound Message Processing Time, msec";
            const string OutboundMessageProcessingTimeBaseCounterName = "Outbound Message Processing Time Base";

        #endregion



        #region CONFIGURE - Custom Counter To Use Externally

            public static readonly IPerformanceCounter ProcessingTasksCurrentTotal = ManagerInstance.GetCounter(CategoryName, ProcessingTasksCurrentCounterName);
        
            public static readonly IAveragePerformanceCounter OutboundMessageProcessingTimeTotal = new AveragePerformanceCounter(
               ManagerInstance.GetCounter(CategoryName, OutboundMessageProcessingTimeCounterName),
               ManagerInstance.GetCounter(CategoryName, OutboundMessageProcessingTimeBaseCounterName));

        #endregion



        #region CONFIGURE - Custom Counter Creation Information

            private static CounterCreationData[] CreateCounterCreationDataArray()
            {
                return new[]
                {
                    new CounterCreationData(ProcessingTasksCurrentCounterName, "", PerformanceCounterType.NumberOfItems64),
                    new CounterCreationData(OutboundMessageProcessingTimeCounterName, "", PerformanceCounterType.AverageCount64),
                    new CounterCreationData(OutboundMessageProcessingTimeBaseCounterName, "", PerformanceCounterType.AverageBase),
                };
            }

        #endregion

    #endregion
    }
}

2nd needed customization (based on scenario)

Following you see the code to add to a basic batch file to be invoked with administrative privileges (counter registration requirement) by the consuming target calling service. Note The counter registration batch must be called before the target service goes ready and/or start using custom counters. For instance for Service Fabric applications this can be accomplished by using startup tasks. Alternatively, for a standard application or service, we could fork a new System.Diagnostics.Process to launch it and wait for script execution or ensure to be manually invoked by an administrator whenever the counters need to be registered from scratch or new ones are added Substitute $rootnamespace$ placeholder with console app name (generally equal to default namespace).

REM this to avoid BOM issue

echo %cd%
dotnet $rootnamespace$.dll

REM Evaluate to check error level

:Finish

REM Exit

Exit /B 0
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.1 71 6/19/2024
1.3.0 65 6/19/2024
1.2.7 77 5/29/2024
1.2.6 78 5/29/2024
1.2.5 91 3/25/2024
1.2.4 79 3/25/2024
1.2.2 138 10/6/2023
1.1.4 142 5/18/2023
1.0.6 129 5/11/2023
1.0.5 421 7/26/2022