Skip to content

Commit 750dff4

Browse files
authored
Merge pull request #525 from JasonYeMSFT/chuye/appinsights-instrumentation-skill
feat: add appinsights-instrumentation skill thanks @JasonYeMSFT
2 parents da58b10 + dde0071 commit 750dff4

File tree

9 files changed

+238
-0
lines changed

9 files changed

+238
-0
lines changed

docs/README.skills.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Skills differ from other primitives by supporting bundled assets (scripts, code
2222

2323
| Name | Description | Bundled Assets |
2424
| ---- | ----------- | -------------- |
25+
| [appinsights-instrumentation](../skills/appinsights-instrumentation/SKILL.md) | Instrument a webapp to send useful telemetry data to Azure App Insights | `LICENSE.txt`<br />`examples/appinsights.bicep`<br />`references/ASPNETCORE.md`<br />`references/AUTO.md`<br />`references/NODEJS.md`<br />`references/PYTHON.md`<br />`scripts/appinsights.ps1` |
2526
| [azure-role-selector](../skills/azure-role-selector/SKILL.md) | When user is asking for guidance for which role to assign to an identity given desired permissions, this agent helps them understand the role that will meet the requirements with least privilege access and how to apply that role. | `LICENSE.txt` |
2627
| [github-issues](../skills/github-issues/SKILL.md) | Create, update, and manage GitHub issues using MCP tools. Use this skill when users want to create bug reports, feature requests, or task issues, update existing issues, add labels/assignees/milestones, or manage issue workflows. Triggers on requests like "create an issue", "file a bug", "request a feature", "update issue X", or any GitHub issue management task. | `references/templates.md` |
2728
| [nuget-manager](../skills/nuget-manager/SKILL.md) | Manage NuGet packages in .NET projects/solutions. Use this skill when adding, removing, or updating NuGet package versions. It enforces using `dotnet` CLI for package management and provides strict procedures for direct file edits only when updating versions. | None |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright 2025 (c) Microsoft Corporation.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
name: appinsights-instrumentation
3+
description: 'Instrument a webapp to send useful telemetry data to Azure App Insights'
4+
---
5+
6+
# AppInsights instrumentation
7+
8+
This skill enables sending telemetry data of a webapp to Azure App Insights for better observability of the app's health.
9+
10+
## When to use this skill
11+
12+
Use this skill when the user wants to enable telemetry for their webapp.
13+
14+
## Prerequisites
15+
16+
The app in the workspace must be one of these kinds
17+
18+
- An ASP.NET Core app hosted in Azure
19+
- A Node.js app hosted in Azure
20+
21+
## Guidelines
22+
23+
### Collect context information
24+
25+
Find out the (programming language, application framework, hosting) tuple of the application the user is trying to add telemetry support in. This determines how the application can be instrumented. Read the source code to make an educated guess. Confirm with the user on anything you don't know. You must always ask the user where the application is hosted (e.g. on a personal computer, in an Azure App Service as code, in an Azure App Service as container, in an Azure Container App, etc.).
26+
27+
### Prefer auto-instrument if possible
28+
29+
If the app is a C# ASP.NET Core app hosted in Azure App Service, use [AUTO guide](references/AUTO.md) to help user auto-instrument the app.
30+
31+
### Manually instrument
32+
33+
Manually instrument the app by creating the AppInsights resource and update the app's code.
34+
35+
#### Create AppInsights resource
36+
37+
Use one of the following options that fits the environment.
38+
39+
- Add AppInsights to existing Bicep template. See [examples/appinsights.bicep](examples/appinsights.bicep) for what to add. This is the best option if there are existing Bicep template files in the workspace.
40+
- Use Azure CLI. See [scripts/appinsights.ps1](scripts/appinsights.ps1) for what Azure CLI command to execute to create the App Insights resource.
41+
42+
No matter which option you choose, recommend the user to create the App Insights resource in a meaningful resource group that makes managing resources easier. A good candidate will be the same resource group that contains the resources for the hosted app in Azure.
43+
44+
#### Modify application code
45+
46+
- If the app is an ASP.NET Core app, see [ASPNETCORE guide](references/ASPNETCORE.md) for how to modify the C# code.
47+
- If the app is a Node.js app, see [NODEJS guide](references/NODEJS.md) for how to modify the JavaScript/TypeScript code.
48+
- If the app is a Python app, see [PYTHON guide](references/PYTHON.md) for how to modify the Python code.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@description('Location for all resources')
2+
param location string = resourceGroup().location
3+
4+
@description('Name for new Application Insights')
5+
param name string
6+
7+
// Create Log Analytics Workspace
8+
resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
9+
name: '${name}-workspace'
10+
location: location
11+
properties: {
12+
sku: {
13+
name: 'PerGB2018'
14+
}
15+
retentionInDays: 30
16+
}
17+
}
18+
19+
// Create Application Insights
20+
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
21+
name: name
22+
location: location
23+
kind: 'web'
24+
properties: {
25+
Application_Type: 'web'
26+
WorkspaceResourceId: logAnalyticsWorkspace.id
27+
}
28+
}
29+
30+
output connectionString string = applicationInsights.properties.ConnectionString
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Modify code
2+
3+
Make these necessary changes to the app.
4+
5+
- Install client library
6+
```
7+
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
8+
```
9+
10+
- Configure the app to use Azure Monitor
11+
An ASP.NET Core app typically has a Program.cs file that "builds" the app. Find this file and apply these changes.
12+
- Add `using Azure.Monitor.OpenTelemetry.AspNetCore;` at the top
13+
- Before calling `builder.Build()`, add this line `builder.Services.AddOpenTelemetry().UseAzureMonitor();`.
14+
15+
> Note: since we modified the code of the app, the app needs to be deployed to take effect.
16+
17+
## Configure App Insights connection string
18+
19+
The App Insights resource has a connection string. Add the connection string as an environment variable of the running app. You can use Azure CLI to query the connection string of the App Insights resource. See [scripts/appinsights.ps1](scripts/appinsights.ps1) for what Azure CLI command to execute for querying the connection string.
20+
21+
After getting the connection string, set this environment variable with its value.
22+
23+
```
24+
"APPLICATIONINSIGHTS_CONNECTION_STRING={your_application_insights_connection_string}"
25+
```
26+
27+
If the app has IaC template such as Bicep or terraform files representing its cloud instance, this environment variable should be added to the IaC template to be applied in each deployment. Otherwise, use Azure CLI to manually apply the environment variable to the cloud instance of the app. See [scripts/appinsights.ps1](scripts/appinsights.ps1) for what Azure CLI command to execute for setting this environment variable.
28+
29+
> Important: Don't modify appsettings.json. It was a deprecated way to configure App Insights. The environment variable is the new recommended way.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Auto-instrument app
2+
3+
Use Azure Portal to auto-instrument a webapp hosted in Azure App Service for App Insights without making any code changes. Only the following types of app can be auto-instrumented. See [supported environments and resource providers](https://learn.microsoft.com/azure/azure-monitor/app/codeless-overview#supported-environments-languages-and-resource-providers).
4+
5+
- ASP.NET Core app hosted in Azure App Service
6+
- Node.js app hosted in Azure App Service
7+
8+
Construct a url to bring the user to the Application Insights blade in Azure Portal for the App Service App.
9+
```
10+
https://portal.azure.com/#resource/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Web/sites/{app_service_name}/monitoringSettings
11+
```
12+
13+
Use the context or ask the user to get the subscription_id, resource_group_name, and the app_service_name hosting the webapp.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Modify code
2+
3+
Make these necessary changes to the app.
4+
5+
- Install client library
6+
```
7+
npm install @azure/monitor-opentelemetry
8+
```
9+
10+
- Configure the app to use Azure Monitor
11+
A Node.js app typically has an entry file that is listed as the "main" property in package.json. Find this file and apply these changes in it.
12+
- Require the client library at the top. `const { useAzureMonitor } = require("@azure/monitor-opentelemetry");`
13+
- Call the setup method. `useAzureMonitor();`
14+
15+
> Note: The setup method should be called as early as possible but it must be after the environment variables are configured since it needs the App Insights connection string from the environment variable. For example, if the app uses dotenv to load environment variables, the setup method should be called after it but before anything else.
16+
> Note: since we modified the code of the app, it needs to be deployed to take effect.
17+
18+
## Configure App Insights connection string
19+
20+
The App Insights resource has a connection string. Add the connection string as an environment variable of the running app. You can use Azure CLI to query the connection string of the App Insights resource. See [scripts/appinsights.ps1] for what Azure CLI command to execute for querying the connection string.
21+
22+
After getting the connection string, set this environment variable with its value.
23+
24+
```
25+
"APPLICATIONINSIGHTS_CONNECTION_STRING={your_application_insights_connection_string}"
26+
```
27+
28+
If the app has IaC template such as Bicep or terraform files representing its cloud instance, this environment variable should be added to the IaC template to be applied in each deployment. Otherwise, use Azure CLI to manually apply the environment variable to the cloud instance of the app. See what Azure CLI command to execute for setting this environment variable.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Modify code
2+
3+
Make these necessary changes to the app.
4+
5+
- Install client library
6+
```
7+
pip install azure-monitor-opentelemetry
8+
```
9+
10+
- Configure the app to use Azure Monitor
11+
Python applications send telemetry via the logger class in Python standard library. Create a module that configures and creates a logger that can send telemetry.
12+
13+
```python
14+
import logging
15+
from azure.monitor.opentelemetry import configure_azure_monitor
16+
17+
configure_azure_monitor(
18+
logger_name="<your_logger_namespace>"
19+
)
20+
logger = logging.getLogger("<your_logger_namespace>")
21+
```
22+
23+
> Note: since we modified the code of the app, it needs to be deployed to take effect.
24+
25+
## Configure App Insights connection string
26+
27+
The App Insights resource has a connection string. Add the connection string as an environment variable of the running app. You can use Azure CLI to query the connection string of the App Insights resource. See [scripts/appinsights.ps1] for what Azure CLI command to execute for querying the connection string.
28+
29+
After getting the connection string, set this environment variable with its value.
30+
31+
```
32+
"APPLICATIONINSIGHTS_CONNECTION_STRING={your_application_insights_connection_string}"
33+
```
34+
35+
If the app has IaC template such as Bicep or terraform files representing its cloud instance, this environment variable should be added to the IaC template to be applied in each deployment. Otherwise, use Azure CLI to manually apply the environment variable to the cloud instance of the app. See what Azure CLI command to execute for setting this environment variable.
36+
37+
## Send data
38+
39+
Create a logger that is configured to send telemetry.
40+
```python
41+
logger = logging.getLogger("<your_logger_namespace>")
42+
logger.setLevel(logging.INFO)
43+
```
44+
45+
Then send telemetry events by calling its logging methods.
46+
```python
47+
logger.info("info log")
48+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Create App Insights resource (3 steps)
2+
## Add the Application Insights extension
3+
az extension add -n application-insights
4+
## Create a Log Analytics workspace
5+
az monitor log-analytics workspace create --resource-group $resourceGroupName --workspace-name $logAnalyticsWorkspaceName --location $azureRegionName
6+
## Create the Application Insights resource
7+
az monitor app-insights component create --app $applicationInsightsResourceName --location $azureRegionName --resource-group $resourceGroupName --workspace $logAnalyticsWorkspaceName
8+
9+
# Query connection string of App Insights
10+
az monitor app-insights component show --app $applicationInsightsResourceName --resource-group $resourceGroupName --query connectionString --output tsv
11+
12+
# Set environment variable of App Service
13+
az webapp config appsettings set --resource-group $resourceGroupName --name $appName --settings $key=$value
14+
15+
# Set environment variable of Container App
16+
# Or update an existing container app
17+
az containerapp update -n $containerAppName -g $resourceGroupName --set-env-vars $key=$value
18+
19+
# Set environment variable of Function App
20+
az functionapp config appsettings set --name $functionName --resource-group $ResourceGroupName --settings $key=$value

0 commit comments

Comments
 (0)