Delen via


Live Azure-containers profilen met Application Insights

U kunt de Application Insights Profiler inschakelen voor ASP.NET Core toepassing die bijna zonder code in uw container wordt uitgevoerd. Als u de Application Insights Profiler wilt inschakelen voor uw containerinstantie, moet u het volgende doen:

  • Voeg de verwijzing toe aan het Microsoft.ApplicationInsights.Profiler.AspNetCore NuGet-pakket.
  • Werk de code bij om profiler in te schakelen.
  • Stel de Application Insights-instrumentatiesleutel in.

In dit artikel vindt u informatie over de verschillende manieren waarop u het volgende kunt doen:

  • Installeer het NuGet-pakket in het project.
  • Stel de omgevingsvariabele in via de orchestrator (zoals Kubernetes).
  • Meer informatie over beveiligingsoverwegingen bij productie-implementatie, zoals het beveiligen van uw Application Insights-instrumentatiesleutel.

Vereisten

De omgeving instellen

  1. Kloon en gebruik het volgende voorbeeldproject:

    git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
    
  2. Ga naar het container-app-voorbeeld:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  3. Dit voorbeeld is een barebones-project dat is gemaakt door de volgende CLI-opdracht aan te roepen:

    dotnet new mvc -n EnableServiceProfilerForContainerApp
    

    We hebben vertraging toegevoegd in het Controllers/WeatherForecastController.cs project om het knelpunt te simuleren.

    [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. Voeg het NuGet-pakket toe om de Profiler-traceringen te verzamelen:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. Schakel Application Insights en Profiler in.

    Voeg builder.Services.AddApplicationInsightsTelemetry() en builder.Services.AddServiceProfiler() toe na de WebApplication.CreateBuilder() methode in 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();
    

De meest recente ASP.NET Core build/runtime-installatiekopieën ophalen

  1. Ga naar de map .NET Core 6.0-voorbeeld:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  2. Haal de meest recente ASP.NET Core installatiekopieën op:

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

Tip

Zoek de officiële installatiekopieën voor de Docker SDK en runtime.

Uw Application Insights-sleutel toevoegen

  1. Noteer uw Application Insights-instrumentatiesleutel via uw Application Insights-resource in de Azure Portal.

    Schermopname van het vinden van de instrumentatiesleutel in de Azure Portal.

  2. Open appsettings.json en voeg uw Application Insights-instrumentatiesleutel toe aan deze codesectie:

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

De Docker-installatiekopieën bouwen en uitvoeren

  1. Controleer het Docker-bestand.

  2. Bouw de voorbeeldinstallatiekopieën:

    docker build -t profilerapp .
    
  3. Voer de container uit:

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

De container weergeven via uw browser

Als u het eindpunt wilt bereiken, hebt u twee opties:

  • Ga naar http://localhost:8080/weatherforecast in uw browser.

  • Curl gebruiken:

    curl http://localhost:8080/weatherforecast
    

De logboeken inspecteren

Controleer eventueel het lokale logboek om te zien of een profileringssessie is voltooid:

docker logs testapp

Noteer de volgende gebeurtenissen in de lokale logboeken:

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.

De Service Profiler-traceringen weergeven

  1. Wacht 2 tot 5 minuten, zodat de gebeurtenissen kunnen worden samengevoegd naar Application Insights.

  2. Open het deelvenster Prestaties in uw Application Insights-resource.

  3. Nadat het traceringsproces is voltooid, wordt de knop Profiler Traces weergegeven.

    Schermopname van de knop Profiler-traceringen in het deelvenster Prestaties.

Resources opschonen

Voer de volgende opdracht uit om het voorbeeldproject te stoppen:

docker rm -f testapp

Volgende stappen