|
| 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 - Windows, Linux, and macOS. It can be thought of as a cross-platform equivalent for the [PerformanceCounters](https://docs.microsoft.com/dotnet/api/system.diagnostics.performancecounter) that is only supported in Windows systems. |
| 19 | + |
| 20 | +While users can publish any custom `EventCounters` to meet their needs, the .NET Core 3.0 runtime publishes a set of these counters by default. 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). `EventCounterCollectionModule` collects counters with a non-configurable collection frequency of 60 seconds. There are no special permissions required to collect EventCounters. |
| 25 | + |
| 26 | +## Default counters collected |
| 27 | + |
| 28 | +For apps running in .NET Core 3.0, the following counters are collected automatically by the SDK. 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] |
| 57 | +> Counters of category Microsoft.AspNetCore.Hosting are only added in Asp.Net Core Applications. |
| 58 | +
|
| 59 | +## Customizing counters to be collected |
| 60 | + |
| 61 | +The following example shows how to add/remove counters. This customization would be done in the `ConfigureServices` method 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. |
| 62 | + |
| 63 | +```csharp |
| 64 | + using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector; |
| 65 | + |
| 66 | + public void ConfigureServices(IServiceCollection services) |
| 67 | + { |
| 68 | + //... other code... |
| 69 | +
|
| 70 | + // The following code shows several customizations done to EventCounterCollectionModule. |
| 71 | + services.ConfigureTelemetryModule<EventCounterCollectionModule>( |
| 72 | + (module, o) => |
| 73 | + { |
| 74 | + // This removes all default counters. |
| 75 | + module.Counters.Clear(); |
| 76 | + |
| 77 | + // This adds a user defined counter "MyCounter" from EventSource named "MyEventSource" |
| 78 | + module.Counters.Add(new EventCounterCollectionRequest("MyEventSource", "MyCounter")); |
| 79 | + |
| 80 | + // This 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 code 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 get 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 avg(value) by name |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +To get a chart of a specific counter (for example: `ThreadPool Completed Work Item Count`) over the recent period, run the following query. |
| 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 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 | +## Frequently asked questions |
| 131 | + |
| 132 | +### Can I see EventCounters in Live Metrics? |
| 133 | + |
| 134 | +Live Metrics do not show EventCounters as of today. Use Metric Explorer or Analytics to see the telemetry. |
| 135 | + |
| 136 | +### Which platforms can I see the default list of .NET Core 3.0 counters? |
| 137 | + |
| 138 | +EventCounter doesn't require any special permissions, and is supported in all platforms .NET Core 3.0 is supported. This includes: |
| 139 | + |
| 140 | +* **Operating system**: Windows, Linux, or macOS. |
| 141 | +* **Hosting method**: In process or out of process. |
| 142 | +* **Deployment method**: Framework dependent or self-contained. |
| 143 | +* **Web server**: IIS (Internet Information Server) or Kestrel. |
| 144 | +* **Hosting platform**: The Web Apps feature of Azure App Service, Azure VM, Docker, Azure Kubernetes Service (AKS), and so on. |
| 145 | + |
| 146 | +### I have enabled Application Insights from Azure Web App Portal. But I can't see EventCounters.? |
| 147 | + |
| 148 | + [Application Insights extension](https://docs.microsoft.com/azure/azure-monitor/app/azure-web-apps) for ASP.NET Core doesn't yet support this feature. This document will be updated when this feature is supported. |
| 149 | + |
| 150 | +## <a name="next"></a>Next steps |
| 151 | + |
| 152 | +* [Dependency tracking](../../azure-monitor/app/asp-net-dependencies.md) |
0 commit comments