Stage 5 · Platform
Azure Monitor & Observability
Application Insights
Instrument apps, track dependencies, set availability tests, and sample traces.
What Is Application Insights?
Application Insights is an Application Performance Management (APM) tool that monitors live applications. It detects performance anomalies, tracks requests and dependencies, and provides distributed tracing across services.
- Request tracking — Duration, status code, and count of incoming requests
- Dependency tracking — Outgoing calls to databases, APIs, and message queues
- Exception tracking — Unhandled exceptions with stack traces
- Performance counters — CPU, memory, and request queue length
- Availability monitoring — Synthetic tests from multiple regions
Auto-Instrumentation
Auto-instrumentation automatically collects telemetry without code changes. Azure App Service and AKS support auto-instrumentation for popular frameworks (.NET, Java, Node.js, Python).
# Enable Application Insights on an App Service
az monitor app-insights component create \
--app ai-payments-prod \
--resource-group rg-payments \
--location eastus \
--kind web
# Link to existing App Service
az webapp config appsettings set \
--resource-group rg-payments \
--name webapp-payments \
--settings "APPINSIGHTS_INSTRUMENTATIONKEY=$(az monitor app-insights component show --app ai-payments-prod --resource-group rg-payments --query instrumentationKey --output tsv)"Once the instrumentation key is set, the App Service automatically collects requests, dependencies, exceptions, and performance counters.
# Enable Container Insights on AKS
az aks enable-addons \
--resource-group rg-aks-prod \
--name aks-payments-prod \
--addons monitoring \
--workspace-resource-id "/subscriptions/{sub-id}/resourceGroups/rg-monitoring/providers/Microsoft.OperationalInsights/workspaces/law-payments"Container Insights installs a DaemonSet that collects container logs and metrics. It automatically correlates container telemetry with AKS cluster resources.
Custom Telemetry
Custom telemetry extends auto-instrumentation with application-specific metrics, events, and traces. Track business metrics like orders processed, payment latency, or user sessions.
using Microsoft.ApplicationInsights;
var telemetryClient = new TelemetryClient();
// Track a custom event
telemetryClient.TrackEvent("OrderProcessed",
new Dictionary<string, string> { {"OrderId", "12345"} },
new Dictionary<string, double> { {"OrderTotal", 99.99} });
// Track a custom metric
telemetryClient.TrackMetric("PaymentLatencyMs", 150.0);
// Track an external dependency
telemetryClient.TrackDependency("PaymentGateway", "ChargeCard", DateTimeOffset.UtcNow, TimeSpan.FromMilliseconds(200), true);Custom telemetry flows through the same pipeline as auto-collected data. Events, metrics, and dependencies are queryable in Log Analytics alongside built-in telemetry.
Dependency Tracking
Dependency tracking captures outgoing calls to SQL databases, HTTP APIs, Azure services, and message queues. It automatically collects call duration, success status, and target information.
// Find slow database queries
dependencies
| where type == "SQL"
| where timestamp > ago(24h)
| summarize avgDuration = avg(duration), count = count() by target
| where count > 50
| order by avgDuration desc
// Find failed external calls
dependencies
| where type == "HTTP"
| where success == false
| where timestamp > ago(1h)
| project timestamp, target, resultCode, duration
| order by timestamp descDependency tracking requires the appropriate SDK. For .NET, most dependencies are tracked automatically. For other languages, you may need to use telemetry initializers.
Availability Tests
Availability tests are synthetic HTTP requests that probe your application from multiple Azure regions. They detect downtime, slow responses, and SSL certificate issues before users report them.
# Create an availability test
az monitor app-insights availability-test create \
--resource-group rg-payments \
--app ai-payments-prod \
--name payments-availability \
--url "https://payments.contoso.com/health" \
--test-type ping \
--frequency 300 \
--locations eastus,westus2,westeurope \
--success-criteria "200-299"
# Check test results
az monitor app-insights availability-test show \
--resource-group rg-payments \
--app ai-payments-prod \
--name payments-availability \
--query "{name:name, status:properties.status}" --output jsonAvailability tests run every --frequency seconds from each --locations. Failed tests generate alerts and appear in the Application Map.
Create availability tests against a /health or /ready endpoint that validates database connectivity, external dependencies, and critical paths. The homepage may return 200 while the app is degraded.
Telemetry Sampling
Sampling reduces telemetry volume while maintaining statistical accuracy. Adaptive sampling automatically adjusts the rate based on traffic volume. Fixed-rate sampling uses a consistent percentage.
# Enable adaptive sampling on an Application Insights resource
az monitor app-insights component update \
--app ai-payments-prod \
--resource-group rg-payments \
--ingestion-mode ApplicationInsights \
--retention-time 90Adaptive sampling is enabled by default in the .NET SDK. It samples up to 5 items/second and correlates all telemetry for the same request using operation_Id.
Adaptive sampling may drop custom events at high throughput. If every event is critical (e.g., financial transactions), disable sampling or use TrackEvent with samplingExcluded flag.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.