使用 Application Insights 探查实时 Azure 容器

几乎无需代码即可对容器中运行的 ASP.NET Core 应用程序启用 Application Insights Profiler。 若要在容器实例上启用 Application Insights Profiler,需要:

  • 添加对 Microsoft.ApplicationInsights.Profiler.AspNetCore NuGet 包的引用。
  • 更新代码以启用 Profiler。
  • 设置 Application Insights 检测密钥。

在本文中,你会了解可以执行以下操作的各种方式:

  • 在项目中安装 NuGet 包。
  • 通过业务流程协调程序(例如 Kubernetes)设置环境变量。
  • 了解有关生产部署的安全注意事项,例如保护 Application Insights 检测密钥。

先决条件

设置环境

  1. 克隆并使用以下示例项目

    git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
    
  2. 转到容器应用示例:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  3. 此示例是通过调用以下 CLI 命令创建的基本功能项目:

    dotnet new mvc -n EnableServiceProfilerForContainerApp
    

    我们已在 Controllers/WeatherForecastController.cs 项目中添加了延迟来模拟瓶颈。

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        SimulateDelay();
        ...
        // Other existing code.
    }
    private void SimulateDelay()
    {
        // Delay for 500ms to 2s to simulate a bottleneck.
        Thread.Sleep((new Random()).Next(500, 2000));
    }
    
  4. 添加用于收集探查器跟踪的 NuGet 包:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. 启用 Application Insights 和 Profiler。

    Program.cs 中的 WebApplication.CreateBuilder() 方法后面添加 builder.Services.AddApplicationInsightsTelemetry()builder.Services.AddServiceProfiler()

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights.
    builder.Services.AddServiceProfiler(); // Add this line of code to enable Profiler
    builder.Services.AddControllersWithViews();
    
    var app = builder.Build();
    

拉取最新的 ASP.NET Core 生成/运行时映像

  1. 转到 .NET Core 6.0 示例目录:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  2. 拉取最新的 ASP.NET Core 映像:

    docker pull mcr.microsoft.com/dotnet/sdk:6.0
    docker pull mcr.microsoft.com/dotnet/aspnet:6.0
    

提示

查找 Docker SDK运行时的官方映像。

添加 Application Insights 密钥

  1. 通过 Azure 门户中的 Application Insights 资源,记下你的 Application Insights 检测密钥。

    Screenshot that shows finding the instrumentation key in the Azure portal.

  2. 打开 appsettings.json 并将 Application Insights 检测密钥添加到此代码部分:

    {
        "ApplicationInsights":
        {
            "InstrumentationKey": "Your instrumentation key"
        }
    }
    

生成并运行 Docker 映像

  1. 查看 Docker 文件。

  2. 生成示例映像:

    docker build -t profilerapp .
    
  3. 运行容器:

    docker run -d -p 8080:80 --name testapp profilerapp
    

通过浏览器查看容器

若要命中终结点,有两个选项:

  • 在浏览器中,访问 http://localhost:8080/weatherforecast

  • 使用 curl:

    curl http://localhost:8080/weatherforecast
    

检查日志

(可选)检查本地日志,查看探查会话是否已完成:

docker logs testapp

在本地日志中,请注意以下事件:

Starting application insights profiler with instrumentation key: your-instrumentation key # Double check the instrumentation key
Service Profiler session started.               # Profiler started.
Finished calling trace uploader. Exit code: 0   # Uploader is called with exit code 0.
Service Profiler session finished.              # A profiling session is completed.

查看 Service Profiler 跟踪

  1. 等待 2 到 5 分钟,以便可以将事件聚合到 Application Insights。

  2. 在 Application Insights 资源中打开“性能”窗格。

  3. 跟踪过程完成后,将显示“Profiler 跟踪”按钮。

    Screenshot that shows the Profiler traces button in the Performance pane.

清理资源

运行以下命令停止示例项目:

docker rm -f testapp

后续步骤