Skip to content

Commit bee0387

Browse files
authored
Merge pull request #194869 from hhunter-ms/hh-1929804-chartupdate
[Profiler] Supported services to overview and new doc for linux containers (2)
2 parents 6c60328 + 6517b0a commit bee0387

File tree

5 files changed

+275
-35
lines changed

5 files changed

+275
-35
lines changed
33.6 KB
Loading
125 KB
Loading
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
title: Profile Azure Containers with Application Insights Profiler
3+
description: Enable Application Insights Profiler for Azure Containers.
4+
ms.author: hannahhunter
5+
author: hhunter-ms
6+
ms.contributor: charles.weininger
7+
ms.topic: conceptual
8+
ms.date: 04/25/2022
9+
---
10+
11+
# Profile live Azure containers with Application Insights
12+
13+
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:
14+
15+
* Add the reference to the NuGet package.
16+
* Set the environment variables to enable it.
17+
18+
In this article, you'll learn the various ways you can:
19+
- Install the NuGet package in the project.
20+
- Set the environment variable via the orchestrator (like Kubernetes).
21+
- Learn security considerations around production deployment, like protecting your Application Insights Instrumentation key.
22+
23+
## Pre-requisites
24+
25+
- [An Application Insights resource](./create-new-resource.md). Make note of the instrumentation key.
26+
- [Docker Desktop](https://www.docker.com/products/docker-desktop/) to build docker images.
27+
- [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0) installed.
28+
29+
## Set up the environment
30+
31+
1. Clone and use the following [sample project](https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/tree/main/examples/EnableServiceProfilerForContainerAppNet6):
32+
33+
```bash
34+
git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
35+
```
36+
37+
1. Navigate to the Container App example:
38+
39+
```bash
40+
cd examples/EnableServiceProfilerForContainerAppNet6
41+
```
42+
43+
1. This example is a bare bone project created by calling the following CLI command:
44+
45+
```powershell
46+
dotnet new mvc -n EnableServiceProfilerForContainerApp
47+
```
48+
49+
Note that we've added delay in the `Controllers/WeatherForecastController.cs` project to simulate the bottleneck.
50+
51+
```CSharp
52+
[HttpGet(Name = "GetWeatherForecast")]
53+
public IEnumerable<WeatherForecast> Get()
54+
{
55+
SimulateDelay();
56+
...
57+
// Other existing code.
58+
}
59+
private void SimulateDelay()
60+
{
61+
// Delay for 500ms to 2s to simulate a bottleneck.
62+
Thread.Sleep((new Random()).Next(500, 2000));
63+
}
64+
```
65+
66+
## Pull the latest ASP.NET Core build/runtime images
67+
68+
1. Navigate to the .NET Core 6.0 example directory.
69+
70+
```bash
71+
cd examples/EnableServiceProfilerForContainerAppNet6
72+
```
73+
74+
1. Pull the latest ASP.NET Core images
75+
76+
```shell
77+
docker pull mcr.microsoft.com/dotnet/sdk:6.0
78+
docker pull mcr.microsoft.com/dotnet/aspnet:6.0
79+
```
80+
81+
> [!TIP]
82+
> Find the official images for Docker [SDK](https://hub.docker.com/_/microsoft-dotnet-sdk) and [runtime](https://hub.docker.com/_/microsoft-dotnet-aspnet).
83+
84+
## Add your Application Insights key
85+
86+
1. Via your Application Insights resource in the Azure portal, take note of your Application Insights instrumentation key.
87+
88+
:::image type="content" source="./media/profiler-containerinstances/application-insights-key.png" alt-text="Find instrumentation key in Azure portal":::
89+
90+
1. Open `appsettings.json` and add your Application Insights instrumentation key to this code section:
91+
92+
```json
93+
{
94+
"ApplicationInsights":
95+
{
96+
"InstrumentationKey": "Your instrumentation key"
97+
}
98+
}
99+
```
100+
101+
## Build and run the Docker image
102+
103+
1. Review the `Dockerfile`.
104+
105+
1. Build the example image:
106+
107+
```bash
108+
docker build -t profilerapp .
109+
```
110+
111+
1. Run the container:
112+
113+
```bash
114+
docker run -d -p 8080:80 --name testapp profilerapp
115+
```
116+
117+
## View the container via your browser
118+
119+
To hit the endpoint, either:
120+
121+
- Visit [http://localhost:8080/weatherforecast](http://localhost:8080/weatherforecast) in your browser, or
122+
- Use curl:
123+
124+
```terraform
125+
curl http://localhost:8080/weatherforecast
126+
```
127+
128+
129+
## Inspect the logs
130+
131+
Optionally, inspect the local log to see if a session of profiling finished:
132+
133+
```bash
134+
docker logs testapp
135+
```
136+
137+
In the local logs, note the following events:
138+
139+
```output
140+
Starting application insights profiler with instrumentation key: your-instrumentation key # Double check the instrumentation key
141+
Service Profiler session started. # Profiler started.
142+
Finished calling trace uploader. Exit code: 0 # Uploader is called with exit code 0.
143+
Service Profiler session finished. # A profiling session is completed.
144+
```
145+
146+
## View the Service Profiler traces
147+
148+
1. Wait for 2-5 minutes so the events can be aggregated to Application Insights.
149+
1. Open the **Performance** blade in your Application Insights resource.
150+
1. Once the trace process is complete, you will see the Profiler Traces button like it below:
151+
152+
:::image type="content" source="./media/profiler-containerinstances/profiler_traces.png" alt-text="Profile traces in the performance blade":::
153+
154+
155+
156+
## Clean up resources
157+
158+
Run the following command to stop the example project:
159+
160+
```bash
161+
docker rm -f testapp
162+
```
163+
164+
## Next Steps
165+
166+
- Learn more about [Application Insights Profiler](./profiler-overview.md).
167+
- Learn how to enable Profiler in your [ASP.NET Core applications run on Linux](./profiler-aspnetcore-linux.md).

0 commit comments

Comments
 (0)