Profile live Azure containers with Application Insights

You can enable the Application Insights Profiler for ASP.NET Core application running in your container almost without code. To enable the Application Insights Profiler on your container instance, you'll need to:

  • Add the reference to the NuGet package.
  • Set the environment variables to enable it.

In this article, you'll learn the various ways you can:

  • Install the NuGet package in the project.
  • Set the environment variable via the orchestrator (like Kubernetes).
  • Learn security considerations around production deployment, like protecting your Application Insights Instrumentation key.

Pre-requisites

Set up the environment

  1. Clone and use the following sample project:

    git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
    
  2. Navigate to the Container App example:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  3. This example is a bare bone project created by calling the following CLI command:

    dotnet new mvc -n EnableServiceProfilerForContainerApp
    

    Note that we've added delay in the Controllers/WeatherForecastController.cs project to simulate the bottleneck.

    [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));
    }
    

Pull the latest ASP.NET Core build/runtime images

  1. Navigate to the .NET Core 6.0 example directory.

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  2. Pull the latest ASP.NET Core images

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

Tip

Find the official images for Docker SDK and runtime.

Add your Application Insights key

  1. Via your Application Insights resource in the Azure portal, take note of your Application Insights instrumentation key.

    Find instrumentation key in Azure portal

  2. Open appsettings.json and add your Application Insights instrumentation key to this code section:

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

Build and run the Docker image

  1. Review the Dockerfile.

  2. Build the example image:

    docker build -t profilerapp .
    
  3. Run the container:

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

View the container via your browser

To hit the endpoint, either:

Inspect the logs

Optionally, inspect the local log to see if a session of profiling finished:

docker logs testapp

In the local logs, note the following events:

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.

View the Service Profiler traces

  1. Wait for 2-5 minutes so the events can be aggregated to Application Insights.

  2. Open the Performance blade in your Application Insights resource.

  3. Once the trace process is complete, you will see the Profiler Traces button like it below:

    Profile traces in the performance blade

Clean up resources

Run the following command to stop the example project:

docker rm -f testapp

Next Steps