Share via


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.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 SDKruntime의 공식 이미지를 찾습니다.

Application Insights 키 추가

  1. Azure Portal의 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. 이벤트를 Application Insights에 집계할 수 있도록 2-5분 정도 기다립니다.

  2. Application Insights 리소스에서 성능 창을 엽니다.

  3. 추적 프로세스가 완료되면 Profiler 추적 단추가 나타납니다.

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

리소스 정리

다음 명령을 실행하여 예제 프로젝트를 중지합니다.

docker rm -f testapp

다음 단계