Application Insights を使用したライブ Azure コンテナーのプロファイリング

コンテナーで実行されている ASP.NET Core アプリケーションに対して Application Insights Profiler をほぼコードなしで有効にできます。 Application Insights Profiler をコンテナー インスタンスで 有効にするには、次の手順を実行する必要があります。

  • Microsoft.ApplicationInsights.Profiler.AspNetCore NuGet パッケージへの参照を追加します。
  • プロファイラーを有効にするようにコードを更新します。
  • 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.csWebApplication.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 portal の Application Insights リソースを介して、Application Insights のインストルメンテーション キーをメモしておきます。

    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
    

コンテナーをブラウザーで表示する

エンドポイントをヒットするには、次の 2 つのオプションがあります。

  • ブラウザーで 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. イベントが Application Insights に集計されるように、2 から 5 分待機します。

  2. Application Insights リソースの [パフォーマンス] ペインを開きます。

  3. トレース プロセスが完了すると、[Profiler トレース] ボタンが表示されます。

    [パフォーマンス] ペインの [Profiler トレース] ボタンを示すスクリーンショット。

リソースをクリーンアップする

次のコマンドを実行して、サンプル プロジェクトを停止します。

docker rm -f testapp

次のステップ