When adding the Application Insights SDK to your application, a set of Performance Counters is tracked by default.
This is the list of default counters for ASP.NET web applications:
- % Process\Processor Time
- % Process\Processor Time Normalized
- Memory\Available Bytes
- ASP.NET Requests/Sec
- .NET CLR Exceptions Thrown / sec
- ASP.NET ApplicationsRequest Execution Time
- Process\Private Bytes
- Process\IO Data Bytes/sec
- ASP.NET Applications\Requests In Application Queue
- Processor(_Total)\% Processor Time
However Performance Counters are a windows only feature. With .NET being cross-platform, is there a solution that works on both Windows and Linux?
Of course there is! (Otherwise I wouldn’t be writing this blog post).
Collecting EventCounters with Application Insights
The documentation has the following to say about EventCounters:
EventCounter
is .NET/.NET Core mechanism to publish and consume counters or statistics. EventCounters are supported in all OS platforms - Windows, Linux, and macOS. It can be thought of as a cross-platform equivalent for the PerformanceCounters that is only supported in Windows systems.
By default no EventCounters are collected, but they are easy to add by configuring the EventCounterCollectionModule:
//Add event counters | |
builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>( | |
(module, o) => | |
{ | |
// This removes all default counters, if any. | |
module.Counters.Clear(); | |
module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "gen-0-size")); | |
module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "alloc-rate")); | |
module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "cpu-usage")); | |
module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "exception-count")); | |
} | |
); |
Probably the hardest thing when implementing EventCounters is finding the list of built-in counters.
Here is an example where I added some extra EventCounters from ASP.NET Core:
Viewing EventCounters data in Application Insights
To view the tracked EventCounters, you need to look at the ‘Custom’ category inside the Log-based metrics namespace: