EventCounters 简介

EventCounter 是用于发布和使用计数器或统计信息的 .NET/.NET Core 机制。 所有 OS 平台(Windows、Linux 和 macOS)都支持 EventCounters。 可以将其视为仅在 Windows 系统中受支持的 PerformanceCounters 的等效跨平台。

虽然用户可以根据需要发布任何自定义 EventCounters,但默认情况下,.NET 会发布一组此类计数器。 该文档指导你完成在 Azure Application Insights 中收集和查看 EventCounters(系统定义的或用户定义的)所需的步骤。

注意

以下文档依赖于 Application Insights 经典 API。 Application Insights 的长期计划是使用 OpenTelemetry 收集数据。 有关详细信息,请参阅为 .NET、Node.js、Python 和 Java 应用程序启用 Azure Monitor OpenTelemetry

使用 Application Insights 收集 EventCounters

Application Insights 支持使用 EventCounterCollectionModule 来收集 EventCounters,新发布的 NuGet 包 Microsoft.ApplicationInsights.EventCounterCollector 中包含该模块。 使用 AspNetCoreWorkerService 时,将自动启用 EventCounterCollectionModuleEventCounterCollectionModule 会以不可配置的收集频率(60 秒)收集计数器。 收集 EventCounters 时,不需要具备特殊的权限。 对于 ASP.NET Core 应用程序,你还需要添加 Microsoft.ApplicationInsights.AspNetCore 包。

dotnet add package Microsoft.ApplicationInsights.EventCounterCollector
dotnet add package Microsoft.ApplicationInsights.AspNetCore

已收集默认计数器

AspNetCore SDKWorkerService SDK 的 2.15.0 版本开始,默认情况下不收集任何计数器。 模块本身已启用,因此用户可向其添加所需的计数器以收集它们。

若要获取由 .NET 运行时发布的常用计数器列表,请参阅可用计数器文档。

自定义要收集的计数器

以下示例介绍如何添加/删除计数器。 使用 AddApplicationInsightsTelemetry()AddApplicationInsightsWorkerService() 启用 Application Insights 遥测收集后,此自定义将作为应用程序服务配置的一部分完成。 下面是 ASP.NET Core 应用程序的示例代码。 对于其他类型的应用程序,请参阅此文档

using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector;
using Microsoft.Extensions.DependencyInjection;

builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>(
        (module, o) =>
        {
            // Removes all default counters, if any.
            module.Counters.Clear();

            // Adds a user defined counter "MyCounter" from EventSource named "MyEventSource"
            module.Counters.Add(
                new EventCounterCollectionRequest("MyEventSource", "MyCounter"));

            // Adds the system counter "gen-0-size" from "System.Runtime"
            module.Counters.Add(
                new EventCounterCollectionRequest("System.Runtime", "gen-0-size"));
        }
    );

禁用 EventCounter 收集模块

可以使用 ApplicationInsightsServiceOptions 禁用 EventCounterCollectionModule

以下示例使用 ASP.NET Core SDK。

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;

var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

类似的方法也可用于 WorkerService SDK,但必须更改命名空间,如以下示例中所示。

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;

var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

Metric Explorer 中的事件计数器

若要在 Metric Explorer 中查看 EventCounter 指标,请选择 Application Insights 资源,然后选择基于日志的指标作为指标命名空间。 EventCounter 指标随即显示在“自定义”类别下。

Event counters reported in Application Insights Metric Explorer

Analytics 中的事件计数器

还可以在 Analytics 的“customMetrics”表中搜索和显示事件计数器报表。

例如,运行以下查询,以查看收集了哪些计数器并可用于查询:

customMetrics | summarize avg(value) by name

Event counters reported in Application Insights Analytics

若要在最近一段时间内获取特定计数器(例如:ThreadPool Completed Work Item Count)的图表,请运行以下查询。

customMetrics 
| where name contains "System.Runtime|ThreadPool Completed Work Item Count"
| where timestamp >= ago(1h)
| summarize  avg(value) by cloud_RoleInstance, bin(timestamp, 1m)
| render timechart

Chat of a single counter in Application Insights

与其他遥测一样,customMetrics 同样也具有列 cloud_RoleInstance,指示正在其上运行应用的主机服务器实例的标识。 上述查询显示每个实例的计数器值,并可用于比较不同服务器实例的性能。

警报

与其他指标一样,可以设置警报以便在事件计数器超出指定的限制时收到警报。 打开“警报”窗格并选择“添加警报”。

常见问题

是否能在实时指标中看到 EventCounters?

实时指标目前不显示 EventCounters。 使用 Metric Explorer 或 Analytics 来查看遥测数据。

我已从 Azure Web 应用门户启用 Application Insights。 我看不到 EventCounters,这是什么原因?

ASP.NET Core 的 Application Insights 扩展尚不支持此功能。

后续步骤