你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:使用 Application Insights 在 Service Fabric 上监视和诊断 ASP.NET Core 应用程序

本教程是系列教程的第五部分, 详细介绍了使用 Application Insights 针对 Service Fabric 群集上运行的 ASP.NET Core 应用程序配置监视和诊断的步骤。 我们会从本教程第一部分(即构建 .NET Service Fabric 应用程序)开发的应用程序中收集遥测数据。

此教程系列的第四部分介绍如何:

  • 为应用程序配置 Application Insights
  • 收集响应遥测数据,跟踪服务之间基于 HTTP 的通信
  • 使用 Application Insights 中的应用映射功能
  • 使用 Application Insights API 添加自定义事件

在此系列教程中,你会学习如何:

先决条件

在开始学习本教程之前:

下载投票示例应用程序

如果未生成本教程系列的第一部分中的投票示例应用程序,还可以下载它。 在命令窗口或终端中运行以下命令,将示例应用存储库克隆到本地计算机。

git clone https://github.com/Azure-Samples/service-fabric-dotnet-quickstart

设置 Application Insights 资源

Application Insights 是 Azure 的应用程序性能管理平台,也是 Service Fabric 建议用于应用程序监视和诊断的平台。

若要创建 Application Insights 资源,请导航到 Azure 门户。 选择左侧导航菜单中的“创建资源”,打开 Azure 市场。 选择“监视 + 管理”,然后选择“Application Insights” 。

创建新的 AI 资源

此时需填写要创建的资源的必要属性信息。 输入适当的“名称”、“资源组”和“订阅”。 设置“位置”,以便在将来部署 Service Fabric 群集。 在本教程中,需将应用部署到本地群集,因此与“位置”字段无关。 “应用程序类型”应保留为“ASP.NET Web 应用程序”。

AI 资源属性

填充所需信息以后,请选择“创建”对资源进行预配,这大约需要一分钟。

将 Application Insights 添加到应用程序的服务

使用提升的权限启动 Visual Studio 2019,方法是:右键单击“开始”菜单中的 Visual Studio 图标,然后选择“以管理员身份运行”。 选择“文件”>“打开”>“项目/解决方案”,导航到 Voting 应用程序(在本教程第一部分创建,也可以从 Git 克隆) 。 打开 Voting.sln。 如果系统提示还原应用程序的 NuGet 包,请选择“是”。

执行以下步骤,为 VotingWeb 和 VotingData 服务配置 Application Insights:

  1. 右键单击服务的名称,然后选择“添加”>“连接的服务”>“使用 Application Insights 进行监视”。

    配置 AI

注意

根据项目类型,在右键单击服务名称时,可能需要选择“添加”->“Application Insights 遥测…”

  1. 选择“开始”。

  2. 登录到用于 Azure 订阅的帐户,选择在其中创建了 Application Insights 资源的订阅。 在“资源”下拉列表的“现有的 Application Insights 资源”下找到该资源。 选择“注册”,将 Application Insights 添加到服务。

    注册 AI

  3. 在弹出的对话框完成操作后,单击“完成”。

注意

请确保针对应用程序中的这两项服务执行上述步骤,完成应用程序的 Application Insights 配置。 将相同的 Application Insights 资源用于这两项服务是为了查看服务之间的传入和传出请求和通信。

将 Microsoft.ApplicationInsights.ServiceFabric.Native NuGet 添加到服务

Application Insights 有两个特定于 Service Fabric 的 NuGet,可以根据方案使用。 一个用于 Service Fabric 的本机服务,另一个用于容器和来宾可执行文件。 在这种情况下,我们将使用 Microsoft.ApplicationInsights.ServiceFabric.Native NuGet,以便充分利用对其自带的服务上下文的理解。 若要详细了解 Application Insights SDK 和特定于 Service Fabric 的 NuGet 包,请参阅适用于 Service Fabric 的 Microsoft Application Insights

下面是设置 NuGet 包的步骤:

  1. 右键单击解决方案资源管理器顶部的“解决方案 'Voting'”,然后单击“为解决方案管理 NuGet 包…” 。
  2. 选择“NuGet - 解决方案”窗口顶部导航菜单中的“浏览”,然后勾选搜索栏旁边的“包括预发行版”框 。

注意

可能需要采用类似的方式安装 Microsoft.ServiceFabric.Diagnostics.Internal 包,前提是此包在安装 Application Insights 包之前未预先安装

  1. 搜索 Microsoft.ApplicationInsights.ServiceFabric.Native,然后选择相应的 NuGet 包。

  2. 在右侧选择应用程序中两项服务“VotingWeb”和“VotingData”旁边的复选框,然后选择“安装” 。 AI sdk Nuget

  3. 在显示的“审阅更改”对话框中选择“确定”,接受“接受许可证”中的条款 。 这样即可将 NuGet 添加到服务。

  4. 现在需在两个服务中设置遥测初始值设定项。 为此,请打开“VotingWeb.cs”和“VotingData.cs”。 对这两个文件执行下述两项步骤:

    1. 在每个 <ServiceName>.cs 的顶部现有 using 语句之后,添加这两个 using 语句:
    using Microsoft.ApplicationInsights.Extensibility;
    using Microsoft.ApplicationInsights.ServiceFabric;
    
    1. 在两个文件的 CreateServiceInstanceListeners() 或 CreateServiceReplicaListeners() 的嵌套式 return 语句中,在已声明其他单一实例服务的 ConfigureServices>services 下添加:
    .AddSingleton<ITelemetryInitializer>((serviceProvider) => FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(serviceContext))
    

    此时会向遥测添加服务上下文,方便用户更好地理解 Application Insights 中遥测的源代码。 VotingWeb.cs 中的嵌套式 return 语句应如下所示:

    return new WebHostBuilder()
        .UseKestrel()
        .ConfigureServices(
            services => services
                .AddSingleton<HttpClient>(new HttpClient())
                .AddSingleton<FabricClient>(new FabricClient())
                .AddSingleton<StatelessServiceContext>(serviceContext)
                .AddSingleton<ITelemetryInitializer>((serviceProvider) => FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(serviceContext)))
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
        .UseUrls(url)
        .Build();
    

    同样,VotingData.cs 中的内容应如下所示:

    return new WebHostBuilder()
        .UseKestrel()
        .ConfigureServices(
            services => services
                .AddSingleton<StatefulServiceContext>(serviceContext)
                .AddSingleton<IReliableStateManager>(this.StateManager)
                .AddSingleton<ITelemetryInitializer>((serviceProvider) => FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(serviceContext)))
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
        .UseUrls(url)
        .Build();
    

仔细进行检查,确保在 VotingWeb.csVotingData.cs 中调用 UseApplicationInsights() 方法,如上所示。

注意

此示例应用使用 http 供服务通信。 如果使用 Service Remoting V2 来开发应用,则需在以前添加代码的位置添加以下代码行

ConfigureServices(services => services
    ...
    .AddSingleton<ITelemetryModule>(new ServiceRemotingDependencyTrackingTelemetryModule())
    .AddSingleton<ITelemetryModule>(new ServiceRemotingRequestTrackingTelemetryModule())
)

此时可以部署应用程序了。 选择顶部的“开始”(或“F5”),Visual Studio 将生成应用程序并将其打包,设置本地群集,然后向其部署应用程序 。

注意

如果未安装 .NET Core SDK 的最新版本,可能会出现生成错误。

应用程序部署完后,请访问 localhost:8080,其中应该可以看到 Voting 示例单页应用程序。 投票选择各种不同的项目,以便创建一些示例数据和遥测 - 我投票选择甜点!

AI 示例投票

添加完投票选项后,也可以随意删除部分投票选项。

在 Application Insights 中查看遥测和应用映射

在 Azure 门户中转到 Application Insights 资源。

选择“概览”,回到资源的登陆页。 然后选择顶部的“搜索”,查看传入的跟踪。 跟踪显示在 Application Insights 中需要数分钟。 如果看不到任何内容,请等待一会儿,然后单击顶部的“刷新”按钮。 AI 查看跟踪

在“搜索”窗口中向下滚动就会看到 Application Insights 自带的所有传入遥测。 在 Voting 应用程序中每执行一个操作,就会有一个来自 VotingWeb 的传出 PUT 请求(PUT 投票/Put [名称])、一个来自 VotingData 的传入 PUT 请求(PUT VoteData/Put [名称]),后跟一对用于刷新所显示数据的 GET 请求。 此外还会在 localhost 上有一个针对 HTTP 的依赖项跟踪,因为这些请求是 HTTP 请求。 下面是一个示例,显示了添加一个投票后的情况:

AI 示例请求跟踪

选择其中一个跟踪即可查看其详细信息。 Application Insights 提供了该请求的有用信息,其中包括“响应时间”和“请求 URL”。 另外,由于添加了特定于 Service Fabric 的 NuGet,因此还会在底部的“自定义数据”部分提供 Service Fabric 群集上下文中的应用程序数据。 这其中包括服务上下文,因此可以查看请求源的“PartitionID”和“ReplicaId”。在对应用程序中的错误进行诊断时,这样能够更好地查找问题。

AI 跟踪详细信息

另外,可以选择“概览”页中左侧菜单上的“应用程序映射”,或者选择“应用映射”图标,转到显示两个服务已连接的应用映射。

屏幕截图突出显示左侧菜单中的“应用程序映射”。

可以通过应用映射更好地了解应用程序拓扑,尤其是在开始添加多个不同的一起运行的服务时。 也可通过它来获取有关请求成功率的基本数据,对失败的请求进行诊断,了解问题之所在。 若要详细了解如何使用应用映射,请参阅 Application Insights 中的应用程序映射

将自定义检测添加到应用程序

虽然 Application Insights 提供了许多现成的遥测,但你可能需要添加更多的自定义检测。 这可能取决于业务需求,或者在应用程序出错时对诊断进行改进的需求。 Application Insights 有一个用于引入自定义事件和指标的 API,详见此文

让我们向 VoteDataController.cs(位于“VotingData”>“控制器”下)添加一些自定义事件,以便跟踪在基础 votesDictionary 中添加和删除投票的时间。

  1. 在其他 using 语句末尾添加 using Microsoft.ApplicationInsights;
  2. 在创建 IReliableStateManager 时,在类的开头声明新的 TelemetryClient:private TelemetryClient telemetry = new TelemetryClient();
  3. 在 Put() 函数中添加一个事件,对已添加的投票进行确认。 在事务完成后将 telemetry.TrackEvent($"Added a vote for {name}"); 直接添加到 return OkResult 语句前面。
  4. 在 Delete() 中有一个基于特定条件(即 votesDictionary 包含给定投票选项的投票)的“if/else”。
    1. 在 if 语句中的 await tx.CommitAsync() 后添加一个确认投票已删除的事件:telemetry.TrackEvent($"Deleted votes for {name}");
    2. 在 else 语句中的 return 语句之前添加一个表明没有进行删除的事件:telemetry.TrackEvent($"Unable to delete votes for {name}, voting option not found");

下面是一个示例,显示在添加事件后 Put() 和 Delete() 函数的情况:

// PUT api/VoteData/name
[HttpPut("{name}")]
public async Task<IActionResult> Put(string name)
{
    var votesDictionary = await this.stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");

    using (ITransaction tx = this.stateManager.CreateTransaction())
    {
        await votesDictionary.AddOrUpdateAsync(tx, name, 1, (key, oldvalue) => oldvalue + 1);
        await tx.CommitAsync();
    }

    telemetry.TrackEvent($"Added a vote for {name}");
    return new OkResult();
}

// DELETE api/VoteData/name
[HttpDelete("{name}")]
public async Task<IActionResult> Delete(string name)
{
    var votesDictionary = await this.stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");

    using (ITransaction tx = this.stateManager.CreateTransaction())
    {
        if (await votesDictionary.ContainsKeyAsync(tx, name))
        {
            await votesDictionary.TryRemoveAsync(tx, name);
            await tx.CommitAsync();
            telemetry.TrackEvent($"Deleted votes for {name}");
            return new OkResult();
        }
        else
        {
            telemetry.TrackEvent($"Unable to delete votes for {name}, voting option not found");
            return new NotFoundResult();
        }
    }
}

进行这些更改以后,请启动应用程序,以便生成和部署最新版本。 应用程序部署完后,请访问 localhost:8080,添加和删除一些投票选项。 然后回到 Application Insights 资源,查看最新运行的跟踪(与前面一样,跟踪可能需要 1-2 分钟才会出现在 Application Insights 中)。 不管是添加的还是删除的投票,此时都会看到一个“自定义事件”\*,以及所有响应遥测。

自定义事件

后续步骤

在本教程中,你了解了如何执行以下操作:

  • 为应用程序配置 Application Insights
  • 收集响应遥测数据,跟踪服务之间基于 HTTP 的通信
  • 使用 Application Insights 中的应用映射功能
  • 使用 Application Insights API 添加自定义事件

设置 ASP.NET 应用程序的监视和诊断以后,请尝试以下操作: