Azure Application Insights Setup Guide | Generated by AI
What is Azure Application Insights?
Azure Application Insights is an application performance management (APM) service in Azure Monitor that helps you monitor live web applications. It provides tools for detecting issues, diagnosing problems, and understanding user behavior through telemetry data like requests, dependencies, traces, metrics, and logs. It’s now powered by OpenTelemetry for broader compatibility.
Prerequisites
- An active Azure subscription.
- Permissions to create resources in Azure (e.g., Owner or Contributor role).
- Your application code (supports .NET, Java, Node.js, Python, and more).
Step 1: Create an Application Insights Resource
- Sign in to the Azure portal.
- Click Create a resource in the top-left menu.
- Search for “Application Insights” and select it from the results under Monitoring + management.
- Fill in the details:
- Subscription: Choose your Azure subscription.
- Resource Group: Select an existing one or create a new one.
- Name: Give your resource a unique name.
- Region: Choose a region close to your users or app.
- Workspace: Optionally link to an existing Log Analytics workspace; otherwise, a new one is created automatically.
- Review and click Create. Deployment takes a few minutes.
- Once created, go to your resource’s Overview page and copy the Connection string (hover over it and click the copy icon). This identifies where your app sends telemetry data.
Tip: Use separate resources for dev, test, and prod environments to avoid mixing data.
Step 2: Instrument Your Application
Add OpenTelemetry support to collect telemetry automatically (requests, exceptions, metrics, etc.). Set the connection string via an environment variable named APPLICATIONINSIGHTS_CONNECTION_STRING (recommended for production).
For .NET (ASP.NET Core)
- Install the NuGet package:
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore - In
Program.cs:using Azure.Monitor.OpenTelemetry.AspNetCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddOpenTelemetry().UseAzureMonitor(); var app = builder.Build(); app.Run(); - Set the environment variable with your connection string and run the app.
For Java
- Download the Azure Monitor OpenTelemetry Distro JAR (e.g.,
applicationinsights-agent-3.x.x.jar). - Create a config file
applicationinsights.jsonin the same directory:{ "connectionString": "Your connection string here" } - Run your app with the agent:
java -javaagent:applicationinsights-agent-3.x.x.jar -jar your-app.jar.
For Node.js
- Install the package:
npm install @azure/monitor-opentelemetry - Configure in your app entry point:
const { AzureMonitorOpenTelemetry } = require('@azure/monitor-opentelemetry'); const provider = new AzureMonitorOpenTelemetry({ connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING }); provider.start(); - Set the environment variable and start your app.
For Python
- Install the package:
pip install azure-monitor-opentelemetry - In your app:
from azure.monitor.opentelemetry import configure_azure_monitor configure_azure_monitor(connection_string="Your connection string here") - Run the app.
For other languages or auto-instrumentation (e.g., for Azure App Service), check the official docs. Test locally first.
Step 3: View and Analyze Data
- Run your instrumented app and generate some activity (e.g., send requests).
- In the Azure portal, open your Application Insights resource.
- Check the Overview page: See live metrics, request counts, and response times (data appears in ~1-2 minutes).
- Explore features:
- Live Metrics: Real-time stream for production apps.
- Failures: Drill into exceptions and traces.
- Performance: Analyze dependencies and slow requests.
- Usage: Track user sessions and custom events.
- Alerts: Set up notifications for issues (e.g., high failure rates).
- Query data using Kusto Query Language (KQL) in Logs for custom analysis.
Best Practices
- Start with basic telemetry; add custom events/metrics as needed.
- Monitor costs via the Usage and estimated costs page (billed through Log Analytics).
- For browser monitoring, add the JavaScript SDK snippet to your web pages.
Create and configure Application Insights resources
Enable OpenTelemetry in Application Insights
Application Insights overview