您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

实时指标流:以 1 秒的延迟进行监视和诊断Live Metrics Stream: Monitor & Diagnose with 1-second latency

通过使用 Application Insights 中的实时指标流探测实时和生产时的 Web 应用程序的信号。Probe the beating heart of your live, in-production web application by using Live Metrics Stream from Application Insights. 选择并筛选指标和性能计数器进行实时监视,且服务不会受到任何干扰。Select and filter metrics and performance counters to watch in real time, without any disturbance to your service. 从失败请求和异常的样本中检查堆栈跟踪。Inspect stack traces from sample failed requests and exceptions. 连同Profiler快照调试程序Together with Profiler, Snapshot debugger. 实时指标 Stream 实时网站提供了功能强大且非入侵性的诊断工具。Live Metrics Stream provides a powerful and non-invasive diagnostic tool for your live web site.

使用实时指标流可实现以下操作:With Live Metrics Stream, you can:

  • 通过监视性能和故障计数,在修补程序发布时对其进行验证。Validate a fix while it is released, by watching performance and failure counts.
  • 监视负载测试的效果,实时诊断问题。Watch the effect of test loads, and diagnose issues live.
  • 通过选择和筛选想要监视的指标,重点处理特定的测试会话或筛选出已知问题。Focus on particular test sessions or filter out known issues, by selecting and filtering the metrics you want to watch.
  • 发生异常时获取异常跟踪。Get exception traces as they happen.
  • 试用筛选器,查找最相关的 KPI。Experiment with filters to find the most relevant KPIs.
  • 实时监视任何 Windows 性能计数器。Monitor any Windows performance counter live.
  • 轻松识别有问题的服务器,并筛选出只与该服务器相关的所有 KPI/实时源。Easily identify a server that is having issues, and filter all the KPI/live feed to just that server.

实时指标流视频Live Metrics Stream video

目前 ASP.NET、ASP.NET Core、Azure Functions、Java 和 Node.js 应用支持实时指标。Live Metrics are currently supported for ASP.NET, ASP.NET Core, Azure Functions, Java, and Node.js apps.

开始使用Get started

  1. 如果尚未在 Web 应用中安装 Application Insights,现在请进行安装。If you haven't yet install Application Insights in your web app, do that now.

  2. 若要启用实时指标流,除了标准 Application Insights 包之外,还需要 Microsoft.ApplicationInsights.PerfCounterCollectorIn addition to the standard Application Insights packages Microsoft.ApplicationInsights.PerfCounterCollector is required to enable Live Metrics stream.

  3. 更新到最新版本的 Application Insights 包。 Update to the latest version of the Application Insights package. 在 Visual Studio 中右键单击项目,然后选择“管理 NuGet 包”。 In Visual Studio, right-click your project and choose Manage Nuget packages. 打开“更新” 选项卡,并选择所有的 Microsoft.ApplicationInsights.* 包。Open the Updates tab, and select all the Microsoft.ApplicationInsights.* packages.

    重新部署应用。Redeploy your app.

  4. Azure 门户中,打开应用的 Application Insights 资源,然后打开实时流。In the Azure portal, open the Application Insights resource for your app, and then open Live Stream.

  5. 如果可能在筛选器中使用客户名称等敏感数据,请确保控制通道的安全Secure the control channel if you might use sensitive data such as customer names in your filters.


要将实时指标与 Node.js 一起使用,必须更新到 SDK 的 1.30 版或更高版本。To use Live Metrics with Node.js you must update to version 1.30 or greater of the SDK. 默认情况下,Node.js SDK 中禁用了实时指标。By default Live Metrics is disabled in the Node.js SDK. 若要启用实时指标,请在初始化 SDK 时将 setSendLiveMetrics(true) 添加到配置方法To enable Live Metrics add setSendLiveMetrics(true) to your configuration methods as you initialize the SDK.

没有数据?No data? 请检查服务器的防火墙Check your server firewall

请检查实时指标流的传出端口是否在服务器的防火墙中为打开状态。Check the outgoing ports for Live Metrics Stream are open in the firewall of your servers.

实时指标流与指标资源管理器、Analytics 有何差异?How does Live Metrics Stream differ from Metrics Explorer and Analytics?

实时流Live Stream 指标资源管理器和 AnalyticsMetrics Explorer and Analytics
延迟Latency 在一秒内显示数据Data displayed within one second 在几分钟聚合Aggregated over minutes
无保留期No retention 当数据在图表上显示时会得到保留,不显示时将被丢弃。Data persists while it's on the chart, and is then discarded 数据会保留 90 天Data retained for 90 days
按需On demand 打开实时指标时会流式处理数据Data is streamed while you open Live Metrics 每当安装并启用 SDK 时会发送数据Data is sent whenever the SDK is installed and enabled
免费Free 实时流数据不收取费用There is no charge for Live Stream data 遵从定价中的标准Subject to pricing
采样Sampling 传输所有选择的指标和计数器。All selected metrics and counters are transmitted. 对失败和堆栈跟踪进行采样。Failures and stack traces are sampled. 不应用 TelemetryProcessors。TelemetryProcessors are not applied. 可能会对事件进行采样Events may be sampled
控制通道Control channel 筛选器的控制信号会发送到 SDK。Filter control signals are sent to the SDK. 建议确保此通道的安全。We recommend you secure this channel. 通信为单向通信,即通向门户Communication is one-way, to the portal

选择和筛选指标Select and filter your metrics

(适用于 ASP.NET、ASP.NET Core 和 Azure Functions (v2)。)(Available with ASP.NET, ASP.NET Core, and Azure Functions (v2).)

可以通过在门户中针对任何 Application Insights 遥测数据应用任意筛选器来实时监视自定义 KPI。You can monitor custom KPI live by applying arbitrary filters on any Application Insights telemetry from the portal. 单击筛选器控件(将鼠标悬停在任何图表上时会显示)。Click the filter control that shows when you mouse-over any of the charts. 下图绘制了一个自定义请求计数 KPI,其中的数据已按“URL”和“持续时间”属性进行筛选。The following chart is plotting a custom Request count KPI with filters on URL and Duration attributes. 使用“流预览”部分(显示与在任意时间点指定的条件匹配的实时遥测源)验证筛选器。Validate your filters with the Stream Preview section that shows a live feed of telemetry that matches the criteria you have specified at any point in time.

自定义请求 KPI

可以监视与“计数”不同的值。You can monitor a value different from Count. 可用的选项取决于流的类型,这可能是任何 Application Insights 遥测数据:请求、依赖项、异常、跟踪、事件或指标。The options depend on the type of stream, which could be any Application Insights telemetry: requests, dependencies, exceptions, traces, events, or metrics. 它也可能是自己的自定义度量值It can be your own custom measurement:


除了 Application Insights 遥测数据以外,还可以监视任何 Windows 性能计数器:从流选项中选择该类型,并提供性能计数器的名称。In addition to Application Insights telemetry, you can also monitor any Windows performance counter by selecting that from the stream options, and providing the name of the performance counter.

实时指标分别在以下两个点上聚合:在每个服务器本地,并在所有服务器上。Live metrics are aggregated at two points: locally on each server, and then across all servers. 可以通过在相应的下拉列表中选择其他选项来更改默认选项。You can change the default at either by selecting other options in the respective drop-downs.

样本遥测数据:自定义实时诊断事件Sample Telemetry: Custom Live Diagnostic Events

默认情况下,事件实时源显示已失败的请求和依赖项调用、异常、事件与跟踪的样本。By default, the live feed of events shows samples of failed requests and dependency calls, exceptions, events, and traces. 单击筛选器图标可以查看在任意时间点应用的条件。Click the filter icon to see the applied criteria at any point in time.


与使用指标时一样,可以将任意条件指定为任何 Application Insights 遥测类型。As with metrics, you can specify any arbitrary criteria to any of the Application Insights telemetry types. 在本示例中,我们将选择特定的请求失败、跟踪和事件。In this example, we are selecting specific request failures, traces, and events. 另外,我们还将选择所有异常和依赖项失败。We are also selecting all exceptions and dependency failures.


请注意:目前,对于基于异常消息的条件,请使用外部异常消息。Note: Currently, for Exception message-based criteria, use the outermost exception message. 在前面的示例中,若要筛选出具有内部异常消息“客户端已断开连接”(追随“<--”分隔符查找)的良性异常,In the preceding example, to filter out the benign exception with inner exception message (follows the "<--" delimiter) "The client disconnected." 请使用不包含“读取请求内容时出错”条件的消息。use a message not-contains "Error reading request content" criteria.

单击实时源中的某个项可查看其详细信息。See the details of an item in the live feed by clicking it. 可以通过单击“暂停”、向下滚动或单击某个项来暂停源。 You can pause the feed either by clicking Pause or simply scrolling down, or clicking an item. 在实时源处于暂停状态时,滚回到顶部后,或者单击收集的项的计数器时,该实时源会恢复。Live feed will resume after you scroll back to the top, or by clicking the counter of items collected while it was paused.


按服务器实例筛选Filter by server instance

如果要监视特定服务器角色实例,则可以按服务器进行筛选。If you want to monitor a particular server role instance, you can filter by server.


SDK 要求SDK Requirements

自定义实时指标流适用于 2.4.0-beta2 或更高版本的 Application Insights SDK for WebCustom Live Metrics Stream is available with version 2.4.0-beta2 or newer of Application Insights SDK for web. 请记得从 NuGet 包管理器中选择“包括预发行版”选项。Remember to select "Include Prerelease" option from NuGet package manager.

确保控制通道的安全Secure the control channel

指定的自定义筛选器条件将发回到 Application Insights SDK 中的“实时指标”组件。The custom filters criteria you specify are sent back to the Live Metrics component in the Application Insights SDK. 筛选器可能包含 customerID 等敏感信息。The filters could potentially contain sensitive information such as customerIDs. 可以使用机密 API 密钥以及检测密钥来保护通道的安全。You can make the channel secure with a secret API key in addition to the instrumentation key.

创建 API 密钥Create an API Key

创建 API 密钥

将 API 密钥添加到配置中Add API key to Configuration


在 applicationinsights.config 文件中,将 AuthenticationApiKey 添加到 QuickPulseTelemetryModule:In the applicationinsights.config file, add the AuthenticationApiKey to the QuickPulseTelemetryModule:

<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector">

或者在代码中的 QuickPulseTelemetryModule 内设置该密钥:Or in code, set it on the QuickPulseTelemetryModule:

using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
using Microsoft.ApplicationInsights.Extensibility;

             TelemetryConfiguration configuration = new TelemetryConfiguration();
            configuration.InstrumentationKey = "YOUR-IKEY-HERE";

            QuickPulseTelemetryProcessor processor = null;

                .Use((next) =>
                    processor = new QuickPulseTelemetryProcessor(next);
                    return processor;

            var QuickPulse = new QuickPulseTelemetryModule()

                AuthenticationApiKey = "YOUR-API-KEY"
            foreach (var telemetryProcessor in configuration.TelemetryProcessors)
                if (telemetryProcessor is ITelemetryModule telemetryModule)

Azure 函数应用Azure Function Apps

对于使用 API 密钥来保护通道的 Azure 函数应用 (v2),可以通过一个环境变量来实现。For Azure Function Apps (v2) securing the channel with an API key can be accomplished with an environment variable.

从 Application Insights 资源中创建一个 API 密钥,并转到你的 Function App 的应用程序设置Create an API key from within your Application Insights resource and go to Application Settings for your Function App. 选择“添加新设置” 并输入名称 APPINSIGHTS_QUICKPULSEAUTHAPIKEY 和与你的 API 密钥对应的值。Select add new setting and enter a name of APPINSIGHTS_QUICKPULSEAUTHAPIKEY and a value that corresponds to your API key.

ASP.NET 核心(需要 Application Insights ASP.NET 核心 SDK 2.3.0 版本或更高版本)ASP.NET Core (Requires Application Insights ASP.NET Core SDK 2.3.0-beta or greater)

如下所述修改 startup.cs 文件:Modify your startup.cs file as follows:

首先添加First add

using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;

然后,在 ConfigureServices 方法内添加:Then within the ConfigureServices method add:

services.ConfigureTelemetryModule<QuickPulseTelemetryModule> ((module, o) => module.AuthenticationApiKey = "YOUR-API-KEY-HERE");

但是,如果认识并信任所有连接的服务器,则可以尝试使用不包含经过身份验证的通道的自定义筛选器。However, if you recognize and trust all the connected servers, you can try the custom filters without the authenticated channel. 可以使用此选项六个月。This option is available for six months. 每建立一个新的会话或者新服务器联机时,都需要进行这种替代。This override is required once every new session, or when a new server comes online.



在筛选条件中输入 CustomerID 等潜在的敏感信息之前,我们强烈建议设置经过身份验证的通道。We strongly recommend that you set up the authenticated channel before entering potentially sensitive information like CustomerID in the filter criteria.


没有数据?No data? 如果应用程序位于受保护的网络中:实时指标流使用不同于其他 Application Insights 遥测功能的 IP 地址。If your application is in a protected network: Live Metrics Stream uses a different IP addresses than other Application Insights telemetry. 请确保在防火墙中开放这些 IP 地址Make sure those IP addresses are open in your firewall.

后续步骤Next steps