查看 TrackAvailability() 测试结果

本文介绍如何在 Azure 门户中查看 TrackAvailability() 测试结果并使用 Log Analytics 查询数据。 应尽可能使用标准测试,因为标准测试要求的投资很少、无需进行维护,而且需要的先决条件也不多。

先决条件

重要

TrackAvailability () 需要开发人员在编写和维护可能较复杂的自定义代码方面投入精力。

检查可用性

首先查看 Application Insights 资源的“可用性”选项卡上的图。

注意

使用 TrackAvailability() 创建的测试显示时会在测试名称旁边带有“CUSTOM”字样。

Screenshot that shows the Availability tab with successful results.

若要查看端到端事务详细信息,请在“深入钻取”下选择“成功”或“失败”。 然后选择示例。 还可以通过选择图上的数据点来访问端到端事务详细信息。

Screenshot that shows selecting a sample availability test.

Screenshot that shows end-to-end transaction details.

Log Analytics 中的查询

可以使用 Log Analytics 查看可用性结果、依赖项等。 若要详细了解 Log Analytics,请参阅日志查询概述

Screenshot that shows availability results.

Screenshot that shows the New Query tab with dependencies limited to 50.

基本代码示例

注意

此示例只是为了向你展示 TrackAvailability() API 调用在 Azure 函数中的工作机制, 而不是为了展示如何编写基础 HTTP 测试代码或业务逻辑,后者是将此示例转变为功能完全正常的可用性测试所需的。 默认情况下,如果逐步完成此示例,你将创建一个基本的可用性 HTTP GET 测试。

若要按照这些说明操作,必须使用专用计划才能在应用服务编辑器中编辑代码。

创建计时器触发器函数

  1. 创建 Azure Functions 资源。

    • 如果已有 Application Insights 资源:

      • 默认情况下,Azure Functions 创建 Application Insights 资源。 但是,如果要使用之前创建的资源,则必须在创建期间进行相应的指定。

      • 按照有关如何创建 Azure Functions 资源的说明进行以下修改:

        在“监视”选项卡上,选择“Application Insights”下拉框,然后输入或选择资源的名称。

        Screenshot that shows selecting your existing Application Insights resource on the Monitoring tab.

    • 如果尚未为计时器触发的函数创建 Application Insights 资源:

      • 默认情况下,在创建 Azure Functions 应用程序时,它将为你创建一个 Application Insights 资源。 按照有关如何创建 Azure Functions 资源的说明进行操作。

    注意

    可以在消耗、高级或应用服务计划中托管函数。 如果要在虚拟网络后面进行测试,或者要测试非公共终结点,则需使用高级计划来代替消耗计划。 在“托管”选项卡上选择计划。确保在创建函数应用时选择最新的 .NET 版本。

  2. 创建计时器触发器函数。

    1. 在函数应用中,选择“函数”选项卡。
    2. 选择 添加 。 在“添加函数”窗格中,选择以下配置:
      1. 开发环境:在门户中开发
      2. 选择模板:计时器触发器
    3. 选择“添加”以创建计时器触发器函数。

    Screenshot that shows how to add a timer trigger function to your function app.

在应用服务编辑器中添加和编辑代码

转到已部署的函数应用,然后在“开发工具”下选择“应用服务编辑器”选项卡。

若要创建新文件,请在计时器触发器函数(例如“TimerTrigger1”)下右键单击,并选择“新建文件”。 然后输入文件的名称,并选择 Enter。

  1. 创建名为“function.proj”的新文件,然后将以下代码粘贴到其中:

    <Project Sdk="Microsoft.NET.Sdk"> 
        <PropertyGroup> 
            <TargetFramework>netstandard2.0</TargetFramework> 
        </PropertyGroup> 
        <ItemGroup> 
            <PackageReference Include="Microsoft.ApplicationInsights" Version="2.15.0" /> <!-- Ensure you’re using the latest version --> 
        </ItemGroup> 
    </Project> 
    

     Screenshot that shows function.proj in the App Service Editor.

  2. 创建名为“runAvailabilityTest.csx”的新文件,然后将以下代码粘贴到其中:

    using System.Net.Http; 
    
    public async static Task RunAvailabilityTestAsync(ILogger log) 
    { 
        using (var httpClient = new HttpClient()) 
        { 
            // TODO: Replace with your business logic 
            await httpClient.GetStringAsync("https://www.bing.com/"); 
        } 
    } 
    
  3. REGION_NAME 环境变量定义为有效的 Azure 可用性位置。

    Azure CLI 中运行以下命令,列出可用区域。

    az account list-locations -o table
    
  4. 将以下代码复制到 run.csx 文件中。 (替换预先存在的代码。)

    #load "runAvailabilityTest.csx" 
    
    using System; 
    
    using System.Diagnostics; 
    
    using Microsoft.ApplicationInsights; 
    
    using Microsoft.ApplicationInsights.Channel; 
    
    using Microsoft.ApplicationInsights.DataContracts; 
    
    using Microsoft.ApplicationInsights.Extensibility; 
    
    private static TelemetryClient telemetryClient; 
    
    // ============================================================= 
    
    // ****************** DO NOT MODIFY THIS FILE ****************** 
    
    // Business logic must be implemented in RunAvailabilityTestAsync function in runAvailabilityTest.csx 
    
    // If this file does not exist, please add it first 
    
    // ============================================================= 
    
    public async static Task Run(TimerInfo myTimer, ILogger log, ExecutionContext executionContext) 
    
    { 
        if (telemetryClient == null) 
        { 
            // Initializing a telemetry configuration for Application Insights based on connection string 
    
            var telemetryConfiguration = new TelemetryConfiguration(); 
            telemetryConfiguration.ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"); 
            telemetryConfiguration.TelemetryChannel = new InMemoryChannel(); 
            telemetryClient = new TelemetryClient(telemetryConfiguration); 
        } 
    
        string testName = executionContext.FunctionName; 
        string location = Environment.GetEnvironmentVariable("REGION_NAME"); 
        var availability = new AvailabilityTelemetry 
        { 
            Name = testName, 
    
            RunLocation = location, 
    
            Success = false, 
        }; 
    
        availability.Context.Operation.ParentId = Activity.Current.SpanId.ToString(); 
        availability.Context.Operation.Id = Activity.Current.RootId; 
        var stopwatch = new Stopwatch(); 
        stopwatch.Start(); 
    
        try 
        { 
            using (var activity = new Activity("AvailabilityContext")) 
            { 
                activity.Start(); 
                availability.Id = Activity.Current.SpanId.ToString(); 
                // Run business logic 
                await RunAvailabilityTestAsync(log); 
            } 
            availability.Success = true; 
        } 
    
        catch (Exception ex) 
        { 
            availability.Message = ex.Message; 
            throw; 
        } 
    
        finally 
        { 
            stopwatch.Stop(); 
            availability.Duration = stopwatch.Elapsed; 
            availability.Timestamp = DateTimeOffset.UtcNow; 
            telemetryClient.TrackAvailability(availability); 
            telemetryClient.Flush(); 
        } 
    } 
    
    

后续步骤