Instrument an app or service to use Application Insights
5 minutes
5 Questions
Application Insights is a powerful Application Performance Management (APM) service within Azure Monitor that enables developers to monitor live applications, detect performance anomalies, and diagnose issues effectively.
To instrument an application for Application Insights, you have two primary …Application Insights is a powerful Application Performance Management (APM) service within Azure Monitor that enables developers to monitor live applications, detect performance anomalies, and diagnose issues effectively.
To instrument an application for Application Insights, you have two primary approaches: automatic instrumentation and manual instrumentation.
**Automatic Instrumentation (Auto-Instrumentation)**
This approach requires minimal code changes. For supported platforms like .NET, Java, Node.js, and Python, you can enable Application Insights through configuration. In Azure App Service, you can enable it through the Azure Portal by navigating to your app, selecting Application Insights, and enabling the feature. The SDK is automatically injected and begins collecting telemetry data.
**Manual Instrumentation**
For more control, you can add the Application Insights SDK to your project. For .NET applications, install the Microsoft.ApplicationInsights.AspNetCore NuGet package. Then configure it in your Startup.cs or Program.cs by adding services.AddApplicationInsightsTelemetry() and providing your connection string from the Azure Portal.
**Key Telemetry Types Collected:**
- Requests: Incoming HTTP requests to your application
- Dependencies: Calls to databases, REST APIs, and external services
- Exceptions: Server and browser exceptions
- Page Views: Client-side browser telemetry
- Custom Events and Metrics: Business-specific telemetry you define
**Connection String Configuration**
Store your Application Insights connection string in application settings or environment variables. The connection string contains the instrumentation key and ingestion endpoint information.
**Custom Telemetry**
Use TelemetryClient to track custom events, metrics, and traces. Inject TelemetryClient through dependency injection and call methods like TrackEvent(), TrackMetric(), or TrackException() to send custom data.
**Best Practices**
- Use sampling to reduce telemetry volume in high-traffic applications
- Configure appropriate retention periods
- Set up alerts for critical metrics
- Use availability tests to monitor endpoint health proactively
Instrument an App or Service to Use Application Insights
Why is Application Insights Important?
Application Insights is a critical component of Azure Monitor that enables developers to monitor live applications, detect performance anomalies, diagnose issues, and understand user behavior. For the AZ-204 exam, understanding how to instrument applications is essential because it demonstrates your ability to build observable, production-ready applications that can be effectively monitored and troubleshot.
What is Application Insights?
Application Insights is an extensible Application Performance Management (APM) service for developers and DevOps professionals. It provides:
• Automatic telemetry collection - Request rates, response times, failure rates • Dependency tracking - Calls to databases, REST APIs, and external services • Exception tracking - Stack traces and context information • Custom events and metrics - Business-specific telemetry • User analytics - Page views, user sessions, and browser information
How Application Insights Works
Application Insights works by installing an instrumentation package in your application that sends telemetry data to an Azure resource. The process involves:
1. Create an Application Insights resource in Azure Portal 2. Obtain the Connection String (previously Instrumentation Key) 3. Install the SDK - Add the appropriate NuGet package (Microsoft.ApplicationInsights.AspNetCore for ASP.NET Core) 4. Configure the application - Add services.AddApplicationInsightsTelemetry() in Program.cs or Startup.cs 5. Set the connection string - Via configuration or environment variable APPLICATIONINSIGHTS_CONNECTION_STRING
Instrumentation Methods:
Auto-instrumentation: Enable monitoring through Azure Portal for supported platforms like App Service, Azure Functions, and VMs with minimal code changes.
SDK-based instrumentation: Add the Application Insights SDK to your code for full control over telemetry collection.
Key Code Examples:
For ASP.NET Core: builder.Services.AddApplicationInsightsTelemetry();
For custom telemetry: var telemetryClient = new TelemetryClient(); telemetryClient.TrackEvent("CustomEvent"); telemetryClient.TrackMetric("CustomMetric", value); telemetryClient.TrackException(exception);
Exam Tips: Answering Questions on Application Insights Instrumentation
• Connection String vs Instrumentation Key: Remember that Connection String is the modern approach and is preferred over the legacy Instrumentation Key. Questions may test this distinction.
• Know the SDK package names: Microsoft.ApplicationInsights.AspNetCore for ASP.NET Core, Microsoft.ApplicationInsights.WorkerService for background services and console apps.
• Sampling: Understand that sampling reduces telemetry volume while maintaining statistical accuracy. Know the difference between adaptive sampling (default) and fixed-rate sampling.
• TelemetryClient methods: Be familiar with TrackEvent(), TrackMetric(), TrackException(), TrackTrace(), TrackRequest(), and TrackDependency().
• Auto-collection: Know what is collected automatically (requests, dependencies, exceptions) versus what requires custom code.
• Live Metrics: Understand this provides real-time telemetry with 1-second latency for production debugging.
• Application Map: Visualizes application topology and dependencies - useful for identifying bottlenecks.
• Availability Tests: Know the difference between URL ping tests, multi-step web tests, and custom track availability tests.
• Configuration priority: Environment variables override appsettings.json configuration. APPLICATIONINSIGHTS_CONNECTION_STRING is the key environment variable.
• Kubernetes and containers: For containerized apps, connection strings should be passed as environment variables rather than hardcoded in configuration files.