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
- Een Application Insights-resource. Noteer de instrumentatiesleutel.
- Docker Desktop om Docker-installatiekopieën te bouwen.
- .NET 6 SDK is geïnstalleerd.
De omgeving instellen
Kloon en gebruik het volgende voorbeeldproject:
git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
Ga naar het container-app-voorbeeld:
cd examples/EnableServiceProfilerForContainerAppNet6
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)); }
Voeg het NuGet-pakket toe om de Profiler-traceringen te verzamelen:
dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
Schakel Application Insights en Profiler in.
Voeg
builder.Services.AddApplicationInsightsTelemetry()
enbuilder.Services.AddServiceProfiler()
toe na deWebApplication.CreateBuilder()
methode inProgram.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
Ga naar de map .NET Core 6.0-voorbeeld:
cd examples/EnableServiceProfilerForContainerAppNet6
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
Uw Application Insights-sleutel toevoegen
Noteer uw Application Insights-instrumentatiesleutel via uw Application Insights-resource in de Azure Portal.
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
Controleer het Docker-bestand.
Bouw de voorbeeldinstallatiekopieën:
docker build -t profilerapp .
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
Wacht 2 tot 5 minuten, zodat de gebeurtenissen kunnen worden samengevoegd naar Application Insights.
Open het deelvenster Prestaties in uw Application Insights-resource.
Nadat het traceringsproces is voltooid, wordt de knop Profiler Traces weergegeven.
Resources opschonen
Voer de volgende opdracht uit om het voorbeeldproject te stoppen:
docker rm -f testapp