다음을 통해 공유


Azure 컨테이너에서 .NET Profiler 사용

코드 없이 컨테이너에서 실행되는 애플리케이션에서 .NET용 Application Insights Profiler를 사용하도록 설정할 수 있습니다. 컨테이너 인스턴스에서 .NET Profiler를 사용하도록 설정하려면 다음을 수행해야 합니다.

  • Microsoft.ApplicationInsights.Profiler.AspNetCore NuGet 패키지에 대한 참조를 추가합니다.
  • .NET용 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 패키지를 추가하여 .NET Profiler 추적을 수집합니다.

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. Application Insights 및 .NET Profiler를 사용하도록 설정합니다.

    builder.Services.AddApplicationInsightsTelemetry()builder.Services.AddServiceProfiler() 메서드 뒤에 WebApplication.CreateBuilder()Program.cs를 추가합니다.

    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 연결 문자열을 기록해 둡니다.

    Azure Portal에서 연결 문자열을 찾는 방법을 보여 주는 스크린샷.

  2. appsettings.json을 열고 Application Insights 연결 문자열을 이 코드 섹션에 추가합니다.

    {
        "ApplicationInsights":
        {
            "InstrumentationKey": "Your connection string"
        }
    }
    

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 connection string: your-connection string # Double check the connection string
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.

문제 해결

앱에서 추적을 찾을 수 없는 경우 이 문제 해결 가이드의 단계를 따르는 것이 좋습니다.

.NET Profiler 추적 보기

  1. 이벤트를 Application Insights에 집계할 수 있도록 2-5분 정도 기다립니다.

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

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

    성능 창의 .NET Profiler 추적 단추를 보여 주는 스크린샷

리소스 정리

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

docker rm -f testapp

다음 단계