Skip to main content

🔎 Deep Dive: Azure Application Insights — Monitoring & Integration with Azure Services

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

Popular posts from this blog

☁️ Azure Key vault Short Notes

🟢 What is Azure Key Vault? A cloud service for securely storing and accessing secrets, keys, and certificates . Removes the need to keep secrets (like connection strings, passwords, API keys) inside code or config files. Provides centralized secret management, encryption, and access control . 👉 Think of it like a secure password manager but for your applications. 🟢 Key Features Secrets → store text values (e.g., DB connection string, API key). Keys → store cryptographic keys (RSA, EC) for encryption, signing. Certificates → store/manage SSL/TLS certificates. Access Control → Access Policies (older model). Azure RBAC (modern, preferred). Integration → works with App Service, Functions, AKS, VMs, SQL DB, etc. Logging → audit who accessed secrets via Azure Monitor / Diagnostic Logs. 🟢 Why Use Key Vault? Security → secrets are encrypted with HSM (Hardware Security Modules). Compliance → meet industry standards (PCI-DSS, ISO, GDPR). Automation → aut...