When you deploy apps to the cloud, monitoring and observability are critical. Azure Application Insights (App Insights) is a powerful tool that helps you monitor, diagnose, and optimize your applications.
In this blog, we’ll cover:
✅ What Azure App Insights is
✅ Key concepts (Telemetry, KQL, Instrumentation Key vs Connection String, Sampling)
✅ Integration with various Azure services
✅ Real-world setup examples (App Service, Function App, API, Containers, Key Vault, Cosmos DB, Service Bus)
✅ Best practices for developers
🌟 What is Azure Application Insights?
Azure Application Insights is an Application Performance Monitoring (APM) service under Azure Monitor.
It allows you to:
- Collect telemetry: requests, dependencies, exceptions, traces, custom events
- Diagnose performance issues (slow requests, failed calls, memory leaks)
- Visualize health in dashboards
- Write powerful queries using Kusto Query Language (KQL)
- Integrate with DevOps pipelines (alerts, dashboards, release health)
Think of it as “X-ray vision for your app” 🚀.
🏗️ Key Concepts
1. Telemetry
The data collected:
- Request telemetry → incoming HTTP calls
- Dependency telemetry → calls to SQL, Cosmos DB, REST APIs
- Exception telemetry → handled/unhandled errors
- Trace telemetry → logging events (info/warning/error)
- Metric telemetry → performance counters (CPU, memory)
2. Instrumentation Key / Connection String
- Instrumentation Key → Old way (GUID) to connect your app to App Insights.
- Connection String → New recommended way; includes Instrumentation Key + Endpoint info.
3. Sampling
- Reduces telemetry volume (e.g., log 10% of requests).
- Adaptive Sampling automatically adjusts based on traffic.
4. KQL (Kusto Query Language)
- Query language to explore telemetry.
- Example:
requests
| where success == false
| summarize count() by resultCode
⚙️ Integration with Azure Services
App Insights integrates deeply with multiple Azure services. Let’s explore one by one.
🔹 1. Azure App Service (Web Apps, APIs)
- Integration: Enable App Insights directly from App Service → Monitoring → Application Insights.
- Automatically collects: requests, dependencies, exceptions.
- Example: Enable via code (ASP.NET Core)
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);
- Benefit: Full visibility into your web app’s performance.
🔹 2. Azure Function Apps
- Native integration: Go to Function App → Application Insights and enable it.
- Collects: function executions, failures, dependency calls.
- Example: Add manually in
host.json
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"maxTelemetryItemsPerSecond" : 5
}
}
}
}
- Benefit: Monitor serverless workloads (cold starts, retries, errors).
🔹 3. Azure Cosmos DB
- Integration: Cosmos DB automatically emits diagnostic logs → forward to App Insights via Diagnostic Settings.
- You can track query RU consumption, latency, throttling.
- Example:
cosmosClientOptions.Diagnostics.IsTelemetryEnabled = true;
- Benefit: Detect slow queries, RU bottlenecks, and failed calls.
🔹 4. Azure Key Vault
- Key Vault access logs (e.g., secrets retrieval) can be sent to Log Analytics / App Insights using Diagnostic Settings.
- Example telemetry:
- Who accessed a secret
- Failed authentication attempts
- Benefit: Detect unauthorized access patterns.
🔹 5. Azure Service Bus
- Integration: Add App Insights SDK in your .NET worker/processor that consumes messages.
- Example:
services.AddApplicationInsightsTelemetryWorkerService();
- Track: Queue/Topic message latency, dead-letter counts, failed deliveries.
- Benefit: Monitor message-driven systems in real time.
🔹 6. Azure SQL Database
- Track DB calls as dependencies automatically when using Entity Framework / ADO.NET.
- Example:
services.AddApplicationInsightsTelemetry();
- Benefit: See slow queries directly in App Insights performance blade.
🔹 7. Azure AKS / Containers
- Use Azure Monitor for containers to forward telemetry into App Insights.
- Collects: pod restarts, node CPU/memory, container logs.
- Benefit: End-to-end view of microservices.
🔹 8. Azure API Management (APIM)
- APIM diagnostic logs can be forwarded to App Insights.
- Example: Enable in APIM → Diagnostics → Application Insights.
- Benefit: Monitor API usage, latency, backend errors.
🔹 9. Azure Application Gateway + App Insights
- Integration: Enable diagnostics in App Gateway → Send to App Insights.
- Collects: WAF logs, request/response times.
- Benefit: Detect malicious traffic patterns, latency issues.
🖥️ Example: ASP.NET Core API + App Insights Integration
Step 1: Add NuGet
dotnet add package Microsoft.ApplicationInsights.AspNetCore
Step 2: Configure in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApplicationInsightsTelemetry(
builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);
var app = builder.Build();
app.MapGet("/hello", () => "Hello World with App Insights!");
app.Run();
Step 3: View telemetry in Portal
- Go to Application Insights resource → Logs.
- Run KQL queries like:
requests
| where name == "GET /hello"
| project timestamp, duration, success
📊 Example: Function App with App Insights
host.json
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"maxTelemetryItemsPerSecond": 5
}
}
}
}
Function Code
[FunctionName("HelloFunction")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
ILogger log)
{
log.LogInformation("HelloFunction processed a request");
return new OkObjectResult("Hello from Azure Function with App Insights!");
}
📌 Best Practices
- ✅ Always use Connection String (not just Instrumentation Key).
- ✅ Use sampling to avoid cost explosion in high-traffic apps.
- ✅ Define custom telemetry for business events:
var telemetry = new TelemetryClient();
telemetry.TrackEvent("OrderPlaced", new Dictionary<string, string> { { "OrderId", "123" } });
- ✅ Integrate App Insights with Azure Monitor Alerts for proactive monitoring.
- ✅ Use Distributed Tracing to track requests across microservices.
- ✅ Secure telemetry data with RBAC (role-based access control).
🎯 Conclusion
Azure Application Insights is not just a logging tool — it’s a full observability platform that integrates seamlessly with almost every Azure service: App Service, Function Apps, Cosmos DB, Service Bus, Key Vault, SQL DB, Containers, and more.
By setting up App Insights correctly, you gain:
- End-to-end visibility
- Faster root cause analysis
- Proactive monitoring and alerts
- Insights into both technical health and business events
🚀 If you’re running workloads in Azure, App Insights should be a default part of your architecture.
Comments
Post a Comment