使用 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. 追蹤程序完成之後,[分析工具追蹤] 按鈕隨即出現。

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

清除資源

執行下列命令可停止範例專案:

docker rm -f testapp

下一步