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

使用 Application Insights 探查实时 Azure Web 应用Profile live Azure web apps with Application Insights

Azure Application Insights 的此功能已面向 Azure 应用服务的 Web 应用功能和 Azure 计算资源提供正式版。This feature of Azure Application Insights is generally available for the Web Apps feature of Azure App Service and Azure compute resources. 有关在本地使用 Profiler 的信息。For information regarding on premises use of profiler.

本文介绍了使用 Application Insights 时在实时 Web 应用程序的每个方法上花费的时间。This article discusses the amount of time that's spent in each method of your live web application when you use Application Insights. Application Insights Profiler 工具显示了由应用处理的实时请求的详细配置文件。The Application Insights Profiler tool displays detailed profiles of live requests that were served by your app. Profiler 突出显示使用时间最多的热路径。Profiler highlights the hot path that uses the most time. 将会根据采样分析响应时间各不相同的请求。Requests with various response times are profiled on a sampling basis. 可以通过各种技术尽量降低与应用程序相关联的开销。By using a variety of techniques, you can minimize the overhead that's associated with the application.

Profiler 目前适用于 Web 应用上运行的 ASP.NET 和 ASP.NET Core Web 应用。Profiler currently works for ASP.NET and ASP.NET Core web apps that are running on Web Apps. 若要使用 Profiler,“基本”服务层或更高层是必需的。The Basic service tier or higher is required to use Profiler.

为 Web 应用启用 ProfilerEnable Profiler for your Web Apps

在部署 Web 应用后,无论是否在源代码中包括了 App Insights SDK,都要执行以下操作:Once you have deployed a Web App, regardless of if you included the App Insights SDK in the source code, do the following:

  1. 转到 Azure 门户中的“应用服务”窗格。Go to the App Services pane in the Azure portal.
  2. 导航到“设置”>“监视”窗格。Navigate to Settings > Monitoring pane.

    在应用服务门户上启用 App Insights

  3. 按窗格中的说明创建新资源,或者选择现有的 App Insights 资源,以便监视 Web 应用。Either follow the instructions on the pane to create a new resource or select an existing App Insights resource to monitor your web app. 接受所有默认选项。Accept all default options. 默认情况下会启用代码级诊断,并且它会启用 Profiler。Code level diagnostics is on by default and enables Profiler.

    添加 App Insights 站点扩展

  4. Profiler 现在已随 App Insights 站点扩展安装,并且使用应用服务应用设置启用。Profiler is now installed with the App Insights site extension, and is enabled using an App Services App Setting.

    Profiler 的应用设置

为 Azure 计算资源启用 ProfilerEnable Profiler for Azure compute resources

有关信息,请参阅用于 Azure 计算资源的 Profiler 版本For information, see the version of Profiler for Azure compute resources.

查看探查器数据View profiler data

确保应用程序可以接收流量。Make sure your application is receiving traffic. 如果正在进行试验,可以通过 Application Insights 性能测试生成针对 Web 应用的请求。If you are doing an experiment, you can generate requests to your web app using Application Insights Performance Testing. 如果最近启用了 Profiler,则可以运行简短的负载测试大约 15 分钟,这样就会生成探查器跟踪。If you have newly enabled Profiler, you can run a short load test for about 15 minutes, which should generate profiler traces. 如果已启用 Profiler 相当长一段时间,请记住,Profiler 每小时会随机运行两次,每次运行两分钟。If you have had Profiler enabled for a while already, keep in mind that Profiler runs randomly two times every hour and for a duration of two minutes each time it runs. 建议运行负载测试一个小时,确保获取探查器跟踪样本。We recommend first running the load test for one hour to make sure you get sample profiler traces.

应用程序收到一些流量后,请转到“性能”窗格,选择“采取措施”,以查看 Profiler 跟踪,然后选择“Profiler 跟踪”按钮。After your application receives some traffic, go to the Performance pane, select Take Actions to view profiler traces, and then select the Profiler Traces button.

“Application Insights 性能”窗格预览 Profiler 跟踪

选择一个样本可以显示执行该请求所花费时间的代码级细节。Select a sample to display a code-level breakdown of time spent executing the request.

Application Insights 跟踪浏览器

跟踪浏览器会显示以下信息:The trace explorer displays the following information:

  • 显示热路径:打开最大的叶节点,或者至少打开某个接近的节点。Show Hot Path: Opens the biggest leaf node, or at least something close. 大多数情况下,此节点与性能瓶颈相邻。In most cases, this node is adjacent to a performance bottleneck.
  • 标签:函数或事件的名称。Label: The name of the function or event. 树中显示了代码与所发生事件(如 SQL 和 HTTP 事件)的混合形式。The tree displays a mix of code and events that occurred (like SQL and HTTP events). 最前面的事件表示请求总持续时间。The top event represents the overall request duration.
  • 所用时间:从操作开始到操作结束之间的时间间隔。Elapsed: The time interval between the start of the operation and the end of the operation.
  • 时间:运行函数或事件的时间,此值相对于运行其他函数的时间。When: The time when the function or event was running in relation to other functions.

如何读取性能数据How to read performance data

Microsoft 服务探查器结合使用采样方法和检测来分析应用程序的性能。The Microsoft service profiler uses a combination of sampling methods and instrumentation to analyze the performance of your application. 当详细收集操作正在进行时,服务探查器将每隔一毫秒对每台计算机的 CPU 指令指针采样。When detailed collection is in progress, the service profiler samples the instruction pointer of each machine CPU every millisecond. 每个样本捕获当前正在执行的线程的完整调用堆栈。Each sample captures the complete call stack of the thread that is currently executing. 它从高级抽象化和低级抽象化两个角度,提供有关线程当前行为的有用详细信息。It gives detailed information about what that thread was doing, both at a high level and at a low level of abstraction. 服务探查器还会收集其他事件(包括上下文切换事件、任务并行库 (TPL) 事件和线程池事件),以跟踪活动相关性和因果关系。The service profiler also collects other events to track activity correlation and causality, including context switching events, Task Parallel Library (TPL) events, and thread pool events.

时间线视图中显示的调用堆栈是采样和检测的结果。The call stack that's displayed in the timeline view is the result of the sampling and instrumentation. 由于每个样本会捕获线程的整个调用堆栈,因此包含 Microsoft .NET Framework 中的代码,以及引用的其他框架中的代码。Because each sample captures the complete call stack of the thread, it includes code from Microsoft .NET Framework, and from other frameworks that you reference.

对象分配(clr!JIT_New 或 clr!JIT_Newarr1)Object allocation (clr!JIT_New or clr!JIT_Newarr1)

clr!JIT_New 和 clr!JIT_Newarr1 是 .NET Framework 中的 helper 函数,用于分配托管堆中的内存。clr!JIT_New and clr!JIT_Newarr1 are helper functions in the .NET Framework that allocate memory from a managed heap. 分配对象时,将调用 clr!JIT_New。clr!JIT_New is invoked when an object is allocated. 分配对象数组时,将调用 clr!JIT_Newarr1。clr!JIT_Newarr1 is invoked when an object array is allocated. 这两个函数通常速度很快,花费的时间相对较短。These two functions are usually fast and take relatively small amounts of time. 如果发现时间线中的 clr!JIT_New 或 clr!JIT_Newarr1 花费了很长时间,则可能表示代码分配了很多对象,从而消耗了大量的内存。If you see clr!JIT_New or clr!JIT_Newarr1 take a substantial amount of time in your timeline, it indicates that the code might be allocating many objects and consuming significant amounts of memory.

加载代码 (clr!ThePreStub)Loading code (clr!ThePreStub)

clr!ThePreStub 是 .NET Framework 中的 helper 函数,用于准备首次执行的代码。clr!ThePreStub is a helper function in the .NET Framework that prepares the code to execute for the first time. 这通常包括但不限于恰时 (JIT) 编译。This usually includes, but is not limited to, just-in-time (JIT) compilation. 对于每个 C# 方法,在进程的生存期内,最多只应调用 clr!ThePreStub 一次。For each C# method, clr!ThePreStub should be invoked at most once during the lifetime of a process.

如果 clr!ThePreStub 针对某个请求花费了很长时间,则表示这是第一个执行该方法的请求。If clr!ThePreStub takes a substantial amount of time for a request, this indicates that the request is the first one that executes that method. .NET Framework 运行时加载该方法的时间非常重要。The time for the .NET Framework runtime to load the first method is significant. 可以考虑在用户访问该代码部分之前使用执行该代码部分的预热进程,或者考虑在程序集中运行本机映像生成器 (ngen.exe)。You might consider using a warmup process that executes that portion of the code before your users access it, or consider running Native Image Generator (ngen.exe) on your assemblies.

锁争用(clr!JITutil_MonContention 或 clr!JITutil_MonEnterWorker)Lock contention (clr!JITutil_MonContention or clr!JITutil_MonEnterWorker)

clr!JITutil_MonContention 或 clr!JITutil_MonEnterWorker 指示当前线程正在等待释放锁。clr!JITutil_MonContention or clr!JITutil_MonEnterWorker indicates that the current thread is waiting for a lock to be released. 执行 C# LOCK 语句、调用 Monitor.Enter 方法或者结合 MethodImplOptions.Synchronized 属性调用某个方法时,通常会显示此文本。This text is often displayed when you execute a C# LOCK statement, when invoking the Monitor.Enter method, or when invoking a method with the MethodImplOptions.Synchronized attribute. 如果线程 A 获取了某个锁,而线程 B 在线程 A 释放该锁之前尝试获取同一个锁,此时通常会发生锁争用。Lock contention usually occurs when thread A acquires a lock, and thread B tries to acquire the same lock before thread A releases it.

加载代码 ([COLD])Loading code ([COLD])

如果方法名称包含 [COLD](例如 mscorlib.ni![COLD]System.Reflection.CustomAttribute.IsDefined),则表示 .NET Framework 运行时首次执行的代码未经过按配置优化功能的优化。If the method name contains [COLD], such as mscorlib.ni![COLD]System.Reflection.CustomAttribute.IsDefined, the .NET Framework runtime is executing code for the first time that is not optimized by profile-guided optimization. 对于每个方法,在进程的生存期内,它最多只应显示一次。For each method, it should be displayed at most once during the lifetime of the process.

如果针对某个请求加载代码花费的时间很长,这表示这是第一个执行该方法的未优化部分的请求。If loading code takes a substantial amount of time for a request, this indicates that the request is the first one to execute the unoptimized portion of the method. 请考虑在用户访问该代码部分之前使用执行该代码部分的预热进程。Consider using a warmup process that executes that portion of the code before your users access it.

发送 HTTP 请求Send HTTP request

HttpClient.Send 等方法指示代码正在等待某个 HTTP 请求完成。Methods like HttpClient.Send indicate that the code is waiting for an HTTP request to be completed.

数据库操作Database operation

SqlCommand.Execute 等方法指示代码正在等待某个数据库操作完成。Methods like SqlCommand.Execute indicate that the code is waiting for a database operation to finish.

等待 (AWAIT_TIME)Waiting (AWAIT_TIME)

AWAIT_TIME 指示代码正在等待另一个任务完成。AWAIT_TIME indicates that the code is waiting for another task to finish. 这种情况通常发生在 C# AWAIT 语句上。This usually happens with the C# AWAIT statement. 当代码执行 C# AWAIT 时,线程会回退并将控制权返回给线程池,此时,不会有任何阻塞的线程等待 AWAIT 完成。When the code does a C# AWAIT, the thread unwinds and returns control to the thread pool, and there is no thread that is blocked waiting for the AWAIT to finish. 但是,从逻辑上讲,执行 AWAIT 的线程会被“阻塞”并等待该操作完成。However, logically, the thread that did the AWAIT is "blocked," and is waiting for the operation to finish. AWAIT_TIME 语句指示等待任务完成的阻塞时间。The AWAIT_TIME statement indicates the blocked time waiting for the task to finish.

阻塞时间Blocked time

BLOCKED_TIME 指示代码正在等待另一个资源变为可用。BLOCKED_TIME indicates that the code is waiting for another resource to be available. 例如,它可能会等待同步对象或线程变为可用,或等待请求完成。For example, it might be waiting for a synchronization object, for a thread to be available, or for a request to finish.

CPU 时间CPU time

CPU 正忙于执行指令。The CPU is busy executing the instructions.

磁盘时间Disk time

应用程序正在执行磁盘操作。The application is performing disk operations.

网络时间Network time

应用程序正在执行网络操作。The application is performing network operations.

“时间”列When column

“时间”列是针对节点收集的非独占样本在各个时间发生的变化的可视化效果。The When column is a visualization of how the INCLUSIVE samples collected for a node vary over time. 请求的总范围划分成 32 个时间存储桶。The total range of the request is divided into 32 time buckets. 该节点的非独占样本会在这 32 个存储桶中累积。The inclusive samples for that node are accumulated in those 32 buckets. 每个存储桶用一个条形表示。Each bucket is represented as a bar. 条形的高度表示缩放后的值。The height of the bar represents a scaled value. 如果节点带有 CPU_TIME 或 BLOCKED_TIME 标记,或者跟资源(例如,CPU、磁盘、线程)的消耗存在某种明显关系,则条形表示在该 Bucket 的时间段内消耗了其中的某个资源。For nodes that are marked CPU_TIME or BLOCKED_TIME, or where there is an obvious relationship to consuming a resource (for example, a CPU, disk, or thread), the bar represents the consumption of one of the resources during the period of that bucket. 如果消耗多个资源,这些指标的值可能大于 100%。For these metrics, it's possible to get a value of greater than 100 percent by consuming multiple resources. 例如,如果在某个时间间隔内平均使用两个 CPU,则指标值为 200%。For example, if you use, on average, two CPUs during an interval, you get 200 percent.

限制Limitations

默认数据保留期为 5 天。The default data retention period is five days. 每天引入的最大数据量为 10 GB。The maximum data that's ingested per day is 10 GB.

使用 Profiler 服务没有任何费用。There are no charges for using the Profiler service. 若要使用 Profiler 服务,Web 应用必须至少托管在 Web 应用的“基本”层中。For you to use the Profiler service, your web app must be hosted in at least the Basic tier of Web Apps.

开销和采样算法Overhead and sampling algorithm

在托管的应用程序已启用 Profiler 来捕获跟踪的每个虚拟机上,Profiler 每小时随机运行 2 分钟。Profiler randomly runs two minutes every hour on each virtual machine that hosts the application that has Profiler enabled for capturing traces. Profiler 在运行时会给服务器增添 5% 到 15% 的 CPU 开销。When Profiler is running, it adds from 5 percent to 15 percent CPU overhead to the server.

可用于托管应用程序的服务器越多,Profiler 对总体应用程序性能的影响越小。The more servers that are available for hosting the application, the less impact Profiler has on the overall application performance. 这是因为采样算法会使 Profiler 在任何时候都只在 5% 的服务器上运行。This is because the sampling algorithm results in Profiler running on only 5 percent of servers at any time. 有更多服务器可用于处理 Web 请求,以抵消运行 Profiler 所产生的服务器开销。More servers are available to serve web requests to offset the server overhead caused by running Profiler.

禁用 ProfilerDisable Profiler

若要为单个 Web 应用实例停止或重启 Profiler,请在“Web 作业”下转到“Web 应用资源”。To stop or restart Profiler for an individual web app's instance, under Web Jobs, go to the Web Apps resource. 若要删除 Profiler,请转到“扩展”。To delete Profiler, go to Extensions.

禁用针对 Web 作业的 Profiler

建议在所有 Web 应用上都启用 Profiler,以便尽早发现任何性能问题。We recommend that you have Profiler enabled on all your web apps to discover any performance issues as early as possible.

如果使用 WebDeploy 将更改部署到 Web 应用程序,请确保排除 App_Data 文件夹,以防在部署期间将它删除。If you use WebDeploy to deploy changes to your web application, ensure that you exclude the App_Data folder from being deleted during deployment. 否则,下一次将 Web 应用程序部署到 Azure 时,会删除 Profiler 扩展文件。Otherwise, the Profiler extension's files are deleted the next time you deploy the web application to Azure.

故障排除Troubleshooting

活动分析会话太多Too many active profiling sessions

目前,最多可以对同一服务计划中运行的 4 个 Azure Web 应用和部署槽启用 Profiler。Currently, you can enable Profiler on a maximum of four Azure web apps and deployment slots that are running in the same service plan. 如果 Profiler Web 作业报告活动探查会话太多,请将一些 Web 应用移到另一服务计划。If the Profiler web job is reporting too many active profiling sessions, move some web apps to a different service plan.

如何确定 Application Insights Profiler 是否正在运行?How do I determine whether Application Insights Profiler is running?

Profiler 在 Web 应用中以连续 Web 作业的形式运行。Profiler runs as a continuous web job in the web app. 可以在 Azure 门户中打开 Web 应用资源。You can open the web app resource in the Azure portal. 在“WebJobs”窗格中,查看 ApplicationInsightsProfiler 的状态。In the WebJobs pane, check the status of ApplicationInsightsProfiler. 如果探查器未运行,请打开“日志”获取详细信息。If it isn't running, open Logs to get more information.

为何运行 Profiler 后仍找不到任何堆栈示例?Why can't I find any stack examples, even though Profiler is running?

下面是可以检查的几个因素:Here are a few things that you can check:

  • 确保 Web 应用服务计划属于“基本”层或更高层。Make sure that your web app service plan is Basic tier or higher.
  • 确保 Web 应用中已启用 Application Insights SDK 2.2 Beta 或更高版本。Make sure that your web app has Application Insights SDK 2.2 Beta or later enabled.
  • 确保配置 Web 应用中的 APPINSIGHTS_INSTRUMENTATIONKEY 设置所采用的检测密钥与 Application Insights SDK 使用的检测密钥相同。Make sure that your web app has the APPINSIGHTS_INSTRUMENTATIONKEY setting configured with the same instrumentation key that's used by the Application Insights SDK.
  • 确保 Web 应用在 .NET Framework 4.6 上运行。Make sure that your web app is running on .NET Framework 4.6.
  • 如果 Web 应用是 ASP.NET Core 应用程序,则必须至少运行 ASP.NET Core 2.0。If your web app is an ASP.NET Core application, it must be running at least ASP.NET Core 2.0.

启动 Profiler 后,将有短暂的一段预热时间,在此期间,Profiler 会积极收集多种性能跟踪。After Profiler is started, there is a short warmup period during which Profiler actively collects several performance traces. 此后,Profiler 会每隔一小时收集性能跟踪两分钟。After that, Profiler collects performance traces for two minutes every hour.

备注

探查器代理中有一个 bug,使其无法上传从 ASP.NET Core 2.1 上运行的应用程序中获取的跟踪。There is a bug in the profiler agent that prevents it from uploading traces taken from applications running on ASP.NET Core 2.1. 我们正在努力修复,很快就会解决。We are working on a fix and will have it ready soon.

使用 Azure 服务探查器时出现问题,I was using Azure Service profiler. 原因是什么?What happened to it?

启用 Application Insights Profiler 时,会禁用 Azure 服务探查器代理。When you enable Application Insights Profiler, the Azure Service profiler agent is disabled.

并行线程的重复计算Double counting in parallel threads

在某些情况下,堆栈查看器中的总时间指标大于请求的持续时间。In some cases, the total time metric in the stack viewer is more than the duration of the request.

如果存在与请求关联的两个或更多个线程并且它们正并行运行,则可能会发生这种情况。This situation might occur when two or more threads are associated with a request, and they are operating in parallel. 在这种情况下,总线程时间就会超过已用时间。In that case, the total thread time is more than the elapsed time. 一个线程可能会等待另一个线程完成。One thread might be waiting on the other to be completed. 查看器会尝试检测此数据并省略不相关的等待时间,但是,它会倾向于显示过多信息,而不是省略关键信息。The viewer tries to detect this and omits the uninteresting wait, but it errs on the side of displaying too much information rather than omit what might be critical information.

如果看到跟踪中出现并行线程,请确定哪些线程处于等待状态,以便可以确定请求的关键路径。When you see parallel threads in your traces, determine which threads are waiting so you can ascertain the critical path for the request. 在大多数情况下,快速进入等待状态的线程只是在等待其他线程完成。In most cases, the thread that quickly goes into a wait state is simply waiting on the other threads. 请专注于其他线程,忽略等待中线程花费的时间。Concentrate on the other threads, and ignore the time in the waiting threads.

无探查数据No profiling data

下面是可以检查的几个因素:Here are a few things that you can check:

  • 如果要查看的数据的期限超过了好几周,请尝试限制时间筛选器并重试。If the data you are trying to view is older than a couple of weeks, try limiting your time filter and try again.
  • 确保代理或防火墙未阻止对 https://gateway.azureserviceprofiler.net 的访问。Ensure that proxies or a firewall have not blocked access to https://gateway.azureserviceprofiler.net.
  • 确保在应用中使用的 Application Insights 检测密钥与用来启用探查的 Application Insights 资源使用的检测密钥相同。Ensure that the Application Insights instrumentation key you are using in your app is the same as the Application Insights resource that you used to enable profiling. 密钥通常在 ApplicationInsights.config 文件中,但也可能在 web.config 或 app.config 文件中。The key is usually in the ApplicationInsights.config file, but it might also be in the web.config or app.config file.

探查查看器中的错误报告Error report in the profiling viewer

在门户中提交支持票证。Submit a support ticket in the portal. 请务必包含错误消息中的相关性 ID。Be sure to include the correlation ID from the error message.

部署错误:目录不为空“D:\home\site\wwwroot\App_Data\jobs”Deployment error: Directory Not Empty 'D:\home\site\wwwroot\App_Data\jobs'

如果在已启用 Profiler 的情况下将 Web 应用重新部署到 Web 应用资源,可能会看到如下消息:If you are redeploying your web app to a Web Apps resource with Profiler enabled, you might see a message like the following:

目录不为空“D:\home\site\wwwroot\App_Data\jobs”Directory Not Empty 'D:\home\site\wwwroot\App_Data\jobs'

如果通过脚本或通过 Azure DevOps 部署管道运行 Web 部署,则会发生此错误。This error occurs if you run Web Deploy from scripts or from the Azure DevOps Deployment Pipeline. 解决方法是将以下附加部署参数添加到 Web 部署任务:The solution is to add the following additional deployment parameters to the Web Deploy task:

-skip:Directory='.*\\App_Data\\jobs\\continuous\\ApplicationInsightsProfiler.*' -skip:skipAction=Delete,objectname='dirPath',absolutepath='.*\\App_Data\\jobs\\continuous$' -skip:skipAction=Delete,objectname='dirPath',absolutepath='.*\\App_Data\\jobs$'  -skip:skipAction=Delete,objectname='dirPath',absolutepath='.*\\App_Data$'

这些参数将删除 Application Insights Profiler 所用的文件夹,并取消阻止重新部署进程。These parameters delete the folder that's used by Application Insights Profiler and unblock the redeploy process. 它们不影响当前运行的 Profiler 实例。They don't affect the Profiler instance that's currently running.

手动安装Manual installation

配置 Profiler 时,将对 Web 应用的设置进行更新。When you configure Profiler, updates are made to the web app's settings. 如果你的环境有此要求,则可以手动应用更新。You can apply the updates manually if your environment requires it. 例如,应用程序在适用于 PowerApps 的 Web 应用环境中运行。An example might be that your application is running in a Web Apps environment for PowerApps.

  1. 在“Web 应用控制”窗格中,打开“设置”。In the Web App Control pane, open Settings.
  2. 将“.Net Framework 版本”设置为“v4.6”。Set .Net Framework version to v4.6.
  3. 将“Always On”设置为“打开”。Set Always On to On.
  4. 添加应用设置“APPINSIGHTS_INSTRUMENTATIONKEY”,将值设置为 SDK 使用的同一检测密钥。Add the APPINSIGHTS_INSTRUMENTATIONKEY app setting, and set the value to the same instrumentation key that's used by the SDK.
  5. 打开“高级工具”。Open Advanced Tools.
  6. 选择“转到”打开 Kudu 网站。Select Go to open the Kudu website.
  7. 在 Kudu 网站上,选择“站点扩展”。On the Kudu website, select Site extensions.
  8. 从 Azure Web 应用库中安装“Application Insights”。Install Application Insights from the Azure Web Apps Gallery.
  9. 重新启动 Web 应用。Restart the web app.

手动触发 ProfilerManually trigger Profiler

可以通过单击一个按钮手动触发 Profiler。Profiler can be triggered manually with one button click. 假设你在运行 Web 性能测试。Suppose you are running a web performance test. 你将需要使用跟踪来帮助你了解 Web 应用在负载下的性能。You will need traces to help you understand how your web app is performing under load. 控制何时捕获跟踪至关重要,因为虽然你知道负载测试将在何时运行,但随机采样时间间隔可能会错过它。Having control over when traces are captured is crucial since you know when load test will be running, but the random sampling interval might miss it. 以下步骤展示了此方案如何工作:The following steps illustrate how this scenario works:

(可选)步骤 1:通过启动 Web 性能测试生成发往 Web 应用的流量(Optional) Step 1: Generate traffic to your web app by starting a web performance test

如果 Web 应用已有传入流量或者你就是希望手动生成流量,请跳过本部分并前进到步骤 2。If your web app already has incoming traffic or if you just want to manually generate traffic, skip this section and proceed to Step 2.

导航到 Application Insights 门户中的“配置”>“性能测试”。Navigate to Application Insights portal, Configure > Performance Testing. 单击“新建”按钮以启动新的性能测试。Click on New button to start a new performance test. 创建新的性能测试create new performance test

在“新建性能测试”窗格中,配置测试目标 URL。In the New performance test pane, configure the test target URL. 接受所有默认设置并开始运行负载测试。Accept all default settings and start running the load test.

配置负载测试

你将看到新测试的状态最先为“已排队”,随后状态变为“正在进行”。You will see the new test is queued first, followed by a status of in progress.

负载测试已提交并排队

负载测试正在运行中

步骤 2:按需启动 ProfilerStep 2: Start profiler on-demand

在负载测试开始运行后,可以启动 Profiler 以在 Web 应用接收负载期间捕获该应用上的跟踪。Once the load test is running, we can start profiler to capture traces on the web app while it's receiving load. 导航到“配置 Profiler”窗格:Navigate to Configure Profiler pane:

“配置 Profiler”窗格条目

在“配置 Profiler”窗格中,有一个“立即探查”按钮可用来在链接的 Web 应用的所有实例上触发 Profiler。On the Configure Profiler pane, there is a Profile Now button to trigger profiler on all instances of the linked web apps. 此外,还可以查看 Profiler 在过去什么时间处于运行状态。In addition, you are provided visibility on when profiler was running in the past.

按需 Profiler

你将看到有关 Profiler 运行状态的通知和状态变化。You will see notification and status change on the profiler run status.

步骤 3:查看跟踪Step 3: View traces

在 Profiler 完成运行后,按照通知中的说明转到“性能”边栏选项卡并查看跟踪。Once the profiler finishes running, follow the instructions on notification to go to Performance blade and view traces.

对按需 Profiler 进行故障排除Troubleshooting on-demand profiler

有时,在按需会话后可能会看到 Profiler 超时错误消息:Sometimes you might see Profiler timeout error message after an on-demand session:

Profiler 超时错误

有两个原因可能会导致你看到此错误:There could be two reasons why you see this error:

  1. 按需 Profiler 会话已成功,但 Application Insights 花费了较长的时间来处理所收集的数据。The on-demand profiler session was successful, but Application Insights took a longer time to process the collected data. 如果数据在 15 分钟内未完成处理,则门户将显示一条超时消息。If data did not finish being processed in 15 minutes, the portal will display a timeout message. 但是,片刻之后将显示 Profiler 跟踪。Though after a while, Profiler traces will show up. 如果发生此情况,目前请直接忽略错误消息。If this happens, please just ignore the error message for now. 我们正在积极开发修补程序。We are actively working on a fix

  2. Web 应用有一个不具备按需功能的较旧版本的 Profiler 代理。Your web app has an older version of Profiler agent that does not have the on-demand feature. 如果之前启用了 Application Insights Profiler,则可能需要更新 Profiler 代理以开始使用按需功能。If you enabled Application Insights Profile previously, chances are you need to update your Profiler agent to start using the on-demand capability.

请执行以下步骤来检查并安装最新的 Profiler:Follow these steps to check and install the latest Profiler:

  1. 转到应用服务的“应用设置”并检查是否设置了以下设置:Go to App Services App Settings and check if the following settings are set:

    • APPINSIGHTS_INSTRUMENTATIONKEY:替换为 Application Insights 的正确检测密钥。APPINSIGHTS_INSTRUMENTATIONKEY: Replace with the proper instrumentation key for Application Insights.
    • APPINSIGHTS_PORTALINFO:ASP.NETAPPINSIGHTS_PORTALINFO: ASP.NET
    • APPINSIGHTS_PROFILERFEATURE_VERSION:1.0.0 如果这些设置有任何一个未设置,请转到 Application Insights 启用窗格来安装最新的站点扩展。APPINSIGHTS_PROFILERFEATURE_VERSION: 1.0.0 If any of these settings are not set, go to the Application Insights enablement pane to install the latest site extension.
  2. 转到应用服务门户中的 Application Insights 窗格。Go to Application Insights pane in App Services portal.

    从应用服务门户中启用 Application Insights

  3. 如果在下面的页面中看到了“更新”按钮,请单击它以更新 Application Insights 站点扩展,这将安装最新的 Profiler 代理。If you see an ‘Update’ button in the following page, click it to update Application Insights site extension which will install the latest Profiler agent. 更新站点扩展Update site extension

  4. 然后单击“更改”来确保启用 Profiler,并选择“确定”来保存更改。Then click change to making sure the Profiler is turned on and select OK to save the changes.

    更改并保存 App Insights

  5. 返回到应用服务的“应用设置”选项卡来复查是否已设置了以下应用设置项:Go back to App Settings tab for the App Service to double-check the following app settings items are set:

    • APPINSIGHTS_INSTRUMENTATIONKEY:替换为 Application Insights 的正确检测密钥。APPINSIGHTS_INSTRUMENTATIONKEY: Replace with the proper instrumentation key for application insights.
    • APPINSIGHTS_PORTALINFO:ASP.NETAPPINSIGHTS_PORTALINFO: ASP.NET
    • APPINSIGHTS_PROFILERFEATURE_VERSION:1.0.0APPINSIGHTS_PROFILERFEATURE_VERSION: 1.0.0

      Profiler 的应用设置

  6. (可选)检查扩展版本,并确保没有任何可用的更新。Optionally, check the extension version and making sure there’s no update available.

    检查是否有扩展更新

后续步骤Next steps