|
| 1 | +--- |
| 2 | +title: Event counters in Application Insights | Microsoft Docs |
| 3 | +description: Monitor system and custom .NET/.NET Core EventCounters in Application Insights. |
| 4 | +services: application-insights |
| 5 | +documentationcenter: '' |
| 6 | +author: cithomas |
| 7 | +manager: carmonm |
| 8 | +ms.assetid: 5b816f4c-a77a-4674-ae36-802ee3a2f56d |
| 9 | +ms.service: application-insights |
| 10 | +ms.workload: tbd |
| 11 | +ms.tgt_pltfrm: ibiza |
| 12 | +ms.topic: conceptual |
| 13 | +ms.date: 09/20/2019 |
| 14 | +ms.author: cithomas |
| 15 | +--- |
| 16 | +# EventCounters introduction |
| 17 | + |
| 18 | +`EventCounter` is .NET/.NET Core mechanism to publish and consume counters or statistics. [This](https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Tracing/documentation/EventCounterTutorial.md) document gives an overview of `EventCounters` and examples on how to publish and consume them. EventCounters are supported in all OS Platforms, and can be thought of as a cross-platform equivalent for the [PerformanceCounters](https://docs.microsoft.com/dotnet/api/system.diagnostics.performancecounter) which is only supported in Windows systems. |
| 19 | + |
| 20 | +While users can publish any custom `EventCounters` to meet their needs, the .NET Core 3.0 ecosystem publishes a set of these counters out-of-the-box. The document will walk through the steps required to collect and view `EventCounters` (system defined or user defined) in Azure Application Insights. |
| 21 | + |
| 22 | +# Using Application Insights to collect EventCounters |
| 23 | + |
| 24 | +Application Insights supports collecting `EventCounters` with its `EventCounterCollectionModule`, which is part of the newly released nuget package [Microsoft.ApplicationInsights.EventCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.EventCounterCollector). `EventCounterCollectionModule` is automatically enabled when using either [AspNetCore](asp-net-core.md) or [WorkerService](worker-service.md) instructions. `EventCounterCollectionModule` collects counters with a non-configurable collection frequency of 60 seconds. |
| 25 | + |
| 26 | +## Default counters collected |
| 27 | + |
| 28 | +For apps running in .NET Core 3.0 or higher, the following lists the default set of counters collected automatically by the SDK. These counters can be queried either in Metrics Explorer or using Analytics query. The name of the counters will be of the form "Category|Counter". |
| 29 | + |
| 30 | +|Category | Counter| |
| 31 | +|---------------|-------| |
| 32 | +|`System.Runtime` | `cpu-usage` | |
| 33 | +|`System.Runtime` | `working-set` | |
| 34 | +|`System.Runtime` | `gc-heap-size` | |
| 35 | +|`System.Runtime` | `gen-0-gc-count` | |
| 36 | +|`System.Runtime` | `gen-1-gc-count` | |
| 37 | +|`System.Runtime` | `gen-2-gc-count` | |
| 38 | +|`System.Runtime` | `time-in-gc` | |
| 39 | +|`System.Runtime` | `gen-0-size` | |
| 40 | +|`System.Runtime` | `gen-1-size` | |
| 41 | +|`System.Runtime` | `gen-2-size` | |
| 42 | +|`System.Runtime` | `loh-size` | |
| 43 | +|`System.Runtime` | `alloc-rate` | |
| 44 | +|`System.Runtime` | `assembly-count` | |
| 45 | +|`System.Runtime` | `exception-count` | |
| 46 | +|`System.Runtime` | `threadpool-thread-count` | |
| 47 | +|`System.Runtime` | `monitor-lock-contention-count` | |
| 48 | +|`System.Runtime` | `threadpool-queue-length` | |
| 49 | +|`System.Runtime` | `threadpool-completed-items-count` | |
| 50 | +|`System.Runtime` | `active-timer-count` | |
| 51 | +|`Microsoft.AspNetCore.Hosting` | `requests-per-second` | |
| 52 | +|`Microsoft.AspNetCore.Hosting` | `total-requests` | |
| 53 | +|`Microsoft.AspNetCore.Hosting` | `current-requests` | |
| 54 | +|`Microsoft.AspNetCore.Hosting` | `failed-requests` | |
| 55 | + |
| 56 | +Note: Counters of category Microsoft.AspNetCore.Hosting are only added in Asp.Net Core Applications. |
| 57 | + |
| 58 | +## Customizing counters to be collected |
| 59 | + |
| 60 | +The following shows how to add/remove counters. This would be done in `ConfigureServices` of your application after application insights telemetry collection is enabled using either `AddApplicationInsightsTelemetry()` or `AddApplicationInsightsWorkerService()`. Following is an example code from an Asp.Net Core application. For other type of applications, refer to [this](worker-service.md#configuring-or-removing-default-telemetrymodules) document. |
| 61 | + |
| 62 | +```csharp |
| 63 | + using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector; |
| 64 | + |
| 65 | + public void ConfigureServices(IServiceCollection services) |
| 66 | + { |
| 67 | + |
| 68 | + //... other code... |
| 69 | +
|
| 70 | + // The following shows several customizations done to EventCounterCollectionModule. |
| 71 | + services.ConfigureTelemetryModule<EventCounterCollectionModule>( |
| 72 | + (module, o) => |
| 73 | + { |
| 74 | + // The following removes all default counters. |
| 75 | + module.Counters.Clear(); |
| 76 | + |
| 77 | + // The following adds a user defined counter "MyCounter" from EventSource named "MyEventSource" |
| 78 | + module.Counters.Add(new EventCounterCollectionRequest("MyEventSource", "MyCounter")); |
| 79 | + |
| 80 | + // The following adds the system counter "gen-0-size" from "System.Runtime" |
| 81 | + module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "gen-0-size")); |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + // The following removes EventCounterCollectionModule to disable the module completely. |
| 86 | + var eventCounterModule = services.FirstOrDefault<ServiceDescriptor> |
| 87 | + (t => t.ImplementationType == typeof(EventCounterCollectionModule)); |
| 88 | + if (eventCounterModule != null) |
| 89 | + { |
| 90 | + services.Remove(eventCounterModule); |
| 91 | + } |
| 92 | + } |
| 93 | +``` |
| 94 | + |
| 95 | +## Event counters in Metric Explorer |
| 96 | + |
| 97 | +To view EventCounter metrics in [Metric Explorer](https://docs.microsoft.com/azure/azure-monitor/platform/metrics-charts), select Application Insights resource, and chose Log-based metrics as metric namespace. Then EventCounter metrics gets displayed under PerformanceCounter category. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +## Event counters in Analytics |
| 102 | + |
| 103 | +You can also search and display event counter reports in [Analytics](../../azure-monitor/app/analytics.md), in the **performanceCounters** table. |
| 104 | + |
| 105 | +For example, run the following query to see what counters are collected and available to query: |
| 106 | + |
| 107 | +```Kusto |
| 108 | +performanceCounters | summarize count(), avg(value) by name |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +To get a chart of a specific counter (eg:) over the recent period: |
| 114 | + |
| 115 | +```Kusto |
| 116 | +performanceCounters |
| 117 | +| where name contains "System.Runtime|ThreadPool Completed Work Item Count" |
| 118 | +| where timestamp >= ago(1h) |
| 119 | +| summarize avg(value) by cloud_RoleInstance, bin(timestamp, 1m) |
| 120 | +| render timechart |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +Like other telemetry, **performanceCounters** also has a column `cloud_RoleInstance` that indicates the identity of the host server instance on which your app is running. The above query shows the counter value per instance, and hence can be used to compare performance of different server instances. |
| 126 | + |
| 127 | +## Alerts |
| 128 | +Like other metrics, you can [set an alert](../../azure-monitor/app/alerts.md) to warn you if an event counter goes outside a limit you specify. Open the Alerts pane and click Add Alert. |
| 129 | + |
| 130 | +## <a name="next"></a>Next steps |
| 131 | + |
| 132 | +* [Dependency tracking](../../azure-monitor/app/asp-net-dependencies.md) |
0 commit comments