Application Insights 中的系统性能计数器System performance counters in Application Insights

Windows 提供了各种性能计数器,例如 CPU 占用、内存、磁盘和网络使用情况。Windows provides a wide variety of performance counters such as CPU occupancy, memory, disk, and network usage. 你还可以定义自己的性能计数器。You can also define your own performance counters. 只要应用程序在你具有管理权限的本地主机或虚拟机上的 IIS 下运行,就支持性能计数器集合。Performance counters collection is supported as long as your application is running under IIS on an on-premises host, or virtual machine to which you have administrative access. 虽然作为 Azure Web 应用运行的应用程序无法直接访问性能计数器,但 Application Insights 仍会收集一部分可用的计数器。Though applications running as Azure Web Apps don't have direct access to performance counters, a subset of available counters are collected by Application Insights.

查看计数器View counters

“指标”窗格显示了一组默认的性能计数器。The Metrics pane shows the default set of performance counters.

Application Insights 中报告的性能计数器

根据配置要为 ASP.NET/ASP.NET Core Web 应用程序收集的当前默认计数器包括:The current default counters that are configured to be collected for ASP.NET/ASP.NET Core web applications are:

  • % Process\Processor Time% Process\Processor Time
  • % Process\Processor Time Normalized% Process\Processor Time Normalized
  • Memory\Available BytesMemory\Available Bytes
  • ASP.NET Requests/SecASP.NET Requests/Sec
  • .NET CLR Exceptions Thrown/sec.NET CLR Exceptions Thrown / sec
  • ASP.NET ApplicationsRequest Execution TimeASP.NET ApplicationsRequest Execution Time
  • Process\Private BytesProcess\Private Bytes
  • Process\IO Data Bytes/secProcess\IO Data Bytes/sec
  • ASP.NET Applications\Requests In Application QueueASP.NET Applications\Requests In Application Queue
  • Processor(_Total)\% Processor TimeProcessor(_Total)\% Processor Time

添加计数器Add counters

如果你需要的性能计数器未包括在指标列表中,则你可以添加它。If the performance counter you want isn't included in the list of metrics, you can add it.

  1. 通过在本地服务器上使用此 PowerShell 命令,了解服务器中有哪些计数器可用:Find out what counters are available in your server by using this PowerShell command on the local server:

    Get-Counter -ListSet *

    (请参阅 Get-Counter。)(See Get-Counter.)

  2. 打开 ApplicationInsights.config。Open ApplicationInsights.config.

    • 如果在开发期间已将 Application Insights 添加到了应用,请在项目中编辑 ApplicationInsights.config,然后将它重新部署到服务器。If you added Application Insights to your app during development, edit ApplicationInsights.config in your project, and then redeploy it to your servers.
  3. 编辑性能收集器指令:Edit the performance collector directive:

    
        <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.AI.PerfCounterCollector">
          <Counters>
            <Add PerformanceCounter="\Objects\Processes"/>
            <Add PerformanceCounter="\Sales(photo)\# Items Sold" ReportAs="Photo sales"/>
          </Counters>
        </Add>
    

备注

ASP.NET Core 应用程序没有 ApplicationInsights.config,因此上述方法对它无效。ASP.NET Core applications do not have ApplicationInsights.config, and hence the above method is not valid for ASP.NET Core Applications.

可以捕获标准计数器以及你自己实现的计数器。You can capture both standard counters and those you've implemented yourself. \Objects\Processes 是标准计数器的一个示例,可在所有 Windows 系统上使用。\Objects\Processes is an example of a standard counter that is available on all Windows systems. \Sales(photo)\# Items Sold 是自定义计数器的一个示例,可在 Web 服务中实现。\Sales(photo)\# Items Sold is an example of a custom counter that might be implemented in a web service.

格式为 \Category(instance)\Counter",而对于不具有实例的类别,仅为 \Category\CounterThe format is \Category(instance)\Counter", or for categories that don't have instances, just \Category\Counter.

ReportAs 对于与 [a-zA-Z()/-_ \.]+ 不匹配的计数器名称而言是必需项,即,它们包含以下设置中没有的字符:字母、圆括号、正斜杠、连字符、下划线、空格、点。ReportAs is required for counter names that do not match [a-zA-Z()/-_ \.]+ - that is, they contain characters that are not in the following sets: letters, round brackets, forward slash, hyphen, underscore, space, dot.

如果指定实例,它以所报告指标的维度“CounterInstanceName”进行收集。If you specify an instance, it will be collected as a dimension "CounterInstanceName" of the reported metric.

为 ASP.NET Web 应用程序或 .NET/.NET Core 控制台应用程序收集代码中的性能计数器Collecting performance counters in code for ASP.NET Web Applications or .NET/.NET Core Console Applications

要收集系统性能计数器并将它们发送到 Application Insights,可以改编下面的片段:To collect system performance counters and send them to Application Insights, you can adapt the snippet below:

    var perfCollectorModule = new PerformanceCollectorModule();
    perfCollectorModule.Counters.Add(new PerformanceCounterCollectionRequest(
      @"\Process([replace-with-application-process-name])\Page Faults/sec", "PageFaultsPerfSec"));
    perfCollectorModule.Initialize(TelemetryConfiguration.Active);

或者,可以执行与所创建自定义指标相同的操作:Or you can do the same thing with custom metrics you created:

    var perfCollectorModule = new PerformanceCollectorModule();
    perfCollectorModule.Counters.Add(new PerformanceCounterCollectionRequest(
      @"\Sales(photo)\# Items Sold", "Photo sales"));
    perfCollectorModule.Initialize(TelemetryConfiguration.Active);

为 ASP.NET Core Web 应用程序收集代码中的性能计数器Collecting performance counters in code for ASP.NET Core Web Applications

修改 Startup.cs 类中的 ConfigureServices 方法,如下所示。Modify ConfigureServices method in your Startup.cs class as below.

using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry();

        // The following configures PerformanceCollectorModule.
  services.ConfigureTelemetryModule<PerformanceCollectorModule>((module, o) =>
            {
                // the application process name could be "dotnet" for ASP.NET Core self-hosted applications.
                module.Counters.Add(new PerformanceCounterCollectionRequest(
    @"\Process([replace-with-application-process-name])\Page Faults/sec", "DotnetPageFaultsPerfSec"));
            });
    }

分析中的性能计数器Performance counters in Analytics

可以在分析中搜索并显示性能计数器报告。You can search and display performance counter reports in Analytics.

PerformanceCounters 架构公开每个性能计数器的 categorycounter 名称和 instance 名称。The performanceCounters schema exposes the category, counter name, and instance name of each performance counter. 在每个应用程序的遥测中,将仅看到该应用程序的计数器。In the telemetry for each application, you'll see only the counters for that application. 例如,若要查看哪些计数器可用:For example, to see what counters are available:

Application Insights 分析中的性能计数器

(此处“实例”是指性能计数器实例,而不是角色或服务器计算机实例。('Instance' here refers to the performance counter instance, not the role, or server machine instance. 通常,性能计数器实例名称会按进程或应用程序的名称对计数器(如处理器时间)进行分段。)The performance counter instance name typically segments counters such as processor time by the name of the process or application.)

若要获取最近一段时间内可用内存的图表:To get a chart of available memory over the recent period:

Application Insights 分析中的内存时间图表

与其他遥测一样,performanceCounters 同样也具有列 cloud_RoleInstance,指示正在其上运行应用的主机服务器实例的标识。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. 例如,若要应用在不同计算机上的性能:For example, to compare the performance of your app on the different machines:

Application Insights 分析中按角色实例分段的性能

ASP.NET 和 Application Insights 计数ASP.NET and Application Insights counts

异常率和异常指标之间的区别是什么?What's the difference between the Exception rate and Exceptions metrics?

  • 异常率是系统性能计数器。Exception rate is a system performance counter. CLR 会对所有引发的已处理和未经处理异常进行计数,并将总采样间隔除以间隔长度。The CLR counts all the handled and unhandled exceptions that are thrown, and divides the total in a sampling interval by the length of the interval. Application Insights SDK 会收集此结果,并将其发送到门户。The Application Insights SDK collects this result and sends it to the portal.

  • 异常是在图表的采样间隔内门户所接收的 TrackException 报告的计数。Exceptions is a count of the TrackException reports received by the portal in the sampling interval of the chart. 它仅包括已将 TrackException 调用写入代码的已处理异常,并不包括所有未经处理的异常It includes only the handled exceptions where you have written TrackException calls in your code, and doesn't include all unhandled exceptions.

在 Azure Web 应用运行的应用程序的性能计数器Performance counters for applications running in Azure Web Apps

部署到 Azure Web 应用的 ASP.NET 和 ASP.NET Core 应用程序在特殊的沙盒环境中运行。Both ASP.NET and ASP.NET Core applications deployed to Azure Web Apps run in a special sandbox environment. 此环境不允许直接访问系统性能计数器。This environment does not allow direct access to system performance counters. 但是,一组有限数量的计数器将作为环境变量公开,如此处所述。However, a limited subset of counters are exposed as environment variables as described here. 适用于 ASP.NET 和 ASP.NET Core 的 Application Insights SDK 会从这些特殊环境变量中收集 Azure Web 应用的性能计数器。Application Insights SDK for ASP.NET and ASP.NET Core collects performance counters from Azure Web Apps from these special environment variables. 此环境中只提供一部分的计数器,完整列表可参见此处Only a subset of counters are available in this environment, and the full list can be found here.

ASP.NET Core 应用程序中的性能计数器Performance counters in ASP.NET Core applications

对 ASP.Net Core 中性能计数器的支持是有限的:Support for performance counters in ASP.NET Core is limited:

  • 如果应用程序在 Azure Web 应用 (Windows) 中运行,SDK 2.4.1 及更高版本将收集性能计数器。SDK versions 2.4.1 and later collect performance counters if the application is running in Azure Web Apps (Windows).
  • 如果应用程序在 Windows 中运行且面向的是 NETSTANDARD2.0 或更高版本,则 SDK 版本 2.7.1 及更高版本将收集性能计数器。SDK versions 2.7.1 and later collect performance counters if the application is running in Windows and targets NETSTANDARD2.0 or later.
  • 对于面向 .NET Framework 的应用程序,所有版本的 SDK 都支持性能计数器。For applications targeting the .NET Framework, all versions of the SDK support performance counters.
  • SDK 2.8.0 及更高版本在 Linux 中支持 cpu/memory 计数器,SDK Versions 2.8.0 and later support cpu/memory counter in Linux. 但不在 Linux 中支持其他计数器。No other counter is supported in Linux. 在 Linux(及其他非 Windows 环境)中,建议使用 EventCounters 来获取系统计数器The recommended way to get system counters in Linux (and other non-Windows environments) is by using EventCounters

警报Alerts

与其他指标一样,可以设置警报以便在性能计数器超出指定的限制时收到警报。Like other metrics, you can set an alert to warn you if a performance counter goes outside a limit you specify. 打开“警报”窗格,并单击“添加警报”。Open the Alerts pane and click Add Alert.

后续步骤Next steps