使用 Application Insights 进行事件分析和可视化Event analysis and visualization with Application Insights

作为 Azure Monitor 的一部分,Application Insights 是用于应用程序监视和诊断的可扩展平台。Part of Azure Monitor, Application Insights is an extensible platform for application monitoring and diagnostics. 它具有强大的分析和查询工具、可自定义的仪表板和可视化效果,以及包括自动报警在内的其他选项。It includes a powerful analytics and querying tool, customizable dashboard and visualizations, and further options including automated alerting. Application Insights 与 Service Fabric 的集成包括 Visual Studio 和 Azure 门户的工具体验,以及 Service Fabric 特定的指标,从而提供综合性的现成日志记录体验。Application Insights's integration with Service Fabric includes tooling experiences for Visual Studio and Azure portal, as well as Service Fabric specific metrics, providing a comprehensive out-of-the-box logging experience. 尽管使用 Application Insights 时会自动创建和收集许多日志,但我们建议将更多的自定义日志记录添加到应用程序以创建更丰富的诊断体验。Though many logs are automatically created and collected for you with Application Insights, we recommend that you add further custom logging to your applications to create a richer diagnostics experience.

本文帮助解决以下常见问题:This article helps address the following common questions:

  • 如何知道应用程序和服务中发生的情况,如何收集遥测数据?How do I know what is going on inside my application and services and gather telemetry?
  • 如何排查应用程序问题,尤其是服务之间相互通信的问题?How do I troubleshoot my application, especially services communicating with one another?
  • 如何获取与服务性能相关的指标,例如页面加载时间和 HTTP 请求数?How do I get metrics about how my services are performing, for example, page load time, HTTP requests?

本文旨在介绍如何从 Application Insights 获取见解和进行故障排除。The purpose of this article is to show how to gain insights and troubleshoot from within Application Insights. 如果想要了解如何使用 Service Fabric 设置和配置 Application Insights,请查看此教程If you'd like to learn how to set up and configure Application Insights with Service Fabric, check out this tutorial.

Application Insights 中的监视Monitoring in Application Insights

使用 Service Fabric 时,可以直接使用 Application Insights。Application Insights has a rich out of the box experience when using Service Fabric. 在概述页中,Application Insights 提供有关服务的重要信息,例如响应时间和处理的请求数。In the overview page, Application Insights provides key information about your service such as the response time and number of requests processed. 单击顶部的“搜索”按钮可以看到应用程序中最近请求的列表。By clicking the 'Search' button at the top, you can see a list of recent requests in your application. 此外,可在此处查看失败的请求数,以及诊断可能发生的错误。Additionally, you would be able to see failed requests here and diagnose what errors may have occurred.

Application Insights 概述

在上图的右侧面板上,列表中有两种主要类型的条目:请求和事件。On the right panel in the preceding image, there are two main types of entries in the list: requests and events. 在本例中,请求是通过 HTTP 请求对应用 API 发出的调用,事件是自定义事件,充当可以添加到代码中任意位置的遥测数据。Requests are calls made to the app's API through HTTP requests in this case, and events are custom events, which act as telemetry you can add anywhere in your code. 可以在用于处理自定义事件和指标的 Application Insights API 中进一步了解如何检测应用程序。You can further explore instrumenting your applications in Application Insights API for custom events and metrics. 单击某个请求会显示下图所示的更多详细信息,包括 Application Insights Service Fabric Nuget 包中收集的、特定于 Service Fabric 的数据。Clicking on a request would display further details as shown in the following image, including data specific to Service Fabric, which is collected in the Application Insights Service Fabric nuget package. 在排查和了解应用程序的状态时,此信息非常有用;所有这些信息都可以在 Application Insights 中搜索This info is useful for troubleshooting and knowing what the state of your application is, and all of this information is searchable within Application Insights

Application Insights 请求详细信息

Application Insights 提供指定的视图用于查询所有传入的数据。Application Insights has a designated view for querying against all the data that comes in. 单击“概述”页顶部的“指标资源管理器”可导航到 Application Insights 门户。Click "Metrics Explorer" on the top of the Overview page to navigate to the Application Insights portal. 在此处,可以使用 Kusto 查询语言,针对前面所述的自定义事件、请求、异常、性能计数器和其他指标运行查询。Here you can run queries against custom events mentioned before, requests, exceptions, performance counters, and other metrics using the Kusto query language. 以下示例演示过去 1 小时内的所有请求。The following example shows all the requests in the last 1 hour.

Application Insights 请求详细信息

若要进一步了解 Application Insights 门户的功能,请转到 Application Insights 门户文档To further explore the capabilities of the Application Insights portal, head over to the Application Insights portal documentation.

使用 EventFlow 配置 Application InsightsConfiguring Application Insights with EventFlow

若正在使用 EventFlow 聚合事件,请务必导入 Microsoft.Diagnostics.EventFlow.Outputs.ApplicationInsights NuGet 包。If you are using EventFlow to aggregate events, make sure to import the Microsoft.Diagnostics.EventFlow.Outputs.ApplicationInsightsNuGet package. 需要在 eventFlowConfig.jsonoutputs 节中包含以下代码:The following code is required in the outputs section of the eventFlowConfig.json:

"outputs": [
    {
        "type": "ApplicationInsights",
        "instrumentationKey": "***ADD INSTRUMENTATION KEY HERE***"
    }
]

请确保在筛选器中应用必要的更改,并包含任何其他输入(及其各自的 NuGet 包)。Make sure to make the required changes in your filters, as well as include any other inputs (along with their respective NuGet packages).

Application Insights SDKApplication Insights SDK

建议使用 EventFlow 和 WAD 作为聚合解决方案,因为它们允许使用更加模块化的方法,方便诊断和监视。例如,若要从 EventFlow 更改输出,不需要更改实际检测,仅需对配置文件进行简单修改。It is recommended to use EventFlow and WAD as aggregation solutions, because they allow for a more modular approach to diagnostics and monitoring, i.e. if you want to change your outputs from EventFlow, it requires no change to your actual instrumentation, just a simple modification to your config file. 然而,若决定投资使用 Application Insights,且不太可能更改到其他平台,则应使用 Application Insights 的新 SDK 以聚合事件并将它们发送到 Application Insights。If, however, you decide to invest in using Application Insights and are not likely to change to a different platform, you should look into using Application Insights' new SDK for aggregating events and sending them to Application Insights. 这意味着不再非得配置 EventFlow 将数据发送到 Application Insights,而是安装 Application Insight 的 Service Fabric NuGet 包。This means that you will no longer have to configure EventFlow to send your data to Application Insights, but instead will install the ApplicationInsight's Service Fabric NuGet package. 可在此处找到此包的详细信息。Details on the package can be found here.

微服务和容器的 Application Insights 支持会显示一些开发中的新功能(当前仍为 beta 版本),通过它们可以使用更加丰富的现成 Application Insights 监视选项。Application Insights support for Microservices and Containers shows you some of the new features that are being worked on (currently still in beta), which allow you to have richer out-of-the-box monitoring options with Application Insights. 这包含依赖项跟踪(用于生成群集中所有服务和应用程序的 AppMap 以及它们之间的通信),以及来自服务更好的跟踪关联(有助于更好地查明应用程序或服务的工作流中的问题)。These include dependency tracking (used in building an AppMap of all your services and applications in a cluster and the communication between them), and better correlation of traces coming from your services (helps in better pinpointing an issue in the workflow of an application or service).

若在 .NET 中进行开发,将来可能会使用一些 Service Fabric 编程模型,且愿意使用 Application Insights 作为可视化和分析事件和日志数据的平台,那我们建议选取 Application Insights SDK 途径作为监视和诊断工作流。If you are developing in .NET and will likely be using some of Service Fabric's programming models, and are willing to use Application Insights as your platform for visualizing and analyzing event and log data, then we recommend that you go via the Application Insights SDK route as your monitoring and diagnostics workflow. 请参阅此文此文,开始使用 Application Insights 收集和显示日志。Read this and this to get started with using Application Insights to collect and display your logs.

配置 Application Insights 作为事件和日志输出的数分钟后,Application Insights 资源中应开始显示信息。Once you have configured Application Insights as an output for your events and logs, information should start to show up in your Application Insights resource in a few minutes. 导航到 Application Insights 资源,此时会转到 Application Insights 资源仪表板。Navigate to the Application Insights resource, which will take you to the Application Insights resource dashboard. 单击 Application Insights 任务栏中的“搜索”,查看其最近收到的跟踪,并能够筛选它们。 Click Search in the Application Insights taskbar to see the latest traces that it has received, and to be able to filter through them.

指标资源管理器 是非常有用的工具,能够基于应用程序、服务和群集报告的指标创建自定义仪表板。Metrics Explorer is a useful tool for creating custom dashboards based on metrics that your applications, services, and cluster may be reporting. 请参阅了解 Application Insights 中的指标,基于收集的数据为自身设置数个图表。See Exploring Metrics in Application Insights to set up a few charts for yourself based on the data you are collecting.

单击“分析”会转到 Application Insights 分析门户,可在此处基于更广的范围和可选性查询事件和跟踪。 Clicking Analytics will take you to the Application Insights Analytics portal, where you can query events and traces with greater scope and optionality. 若要阅读详细信息,请转到 Application Insights 中的分析Read more about this at Analytics in Application Insights.

后续步骤Next steps