feat: Add AI Monitoring Agent with Azure Application Insights integration#23
feat: Add AI Monitoring Agent with Azure Application Insights integration#23devin-ai-integration[bot] wants to merge 1 commit into
Conversation
…tion - New AIMonitoringAgent service (port 5006) under Services/Monitoring/ - Azure Application Insights telemetry: events, metrics, availability, exceptions - AI model performance tracking: latency, token usage, success/failure rates - Service health monitoring with background polling of all microservice endpoints - Statistical anomaly detection using rolling-window standard deviation analysis - Configurable alert rules with threshold conditions and severity levels - REST API with dashboard, monitoring, and alerts controllers - Swagger UI for development - Dockerfile and docker-compose integration - Default alert rules for response time, failures, and AI model metrics
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
| builder.Services.AddSingleton<IAlertingService, AlertingService>(); | ||
|
|
||
| // Health monitor with HttpClient | ||
| builder.Services.AddHttpClient<IServiceHealthMonitor, ServiceHealthMonitor>(); |
There was a problem hiding this comment.
🔴 ServiceHealthMonitor registered as transient via AddHttpClient, causing all instance state to be lost between requests
AddHttpClient<IServiceHealthMonitor, ServiceHealthMonitor>() at Program.cs:28 registers ServiceHealthMonitor with a transient lifetime. However, ServiceHealthMonitor stores critical state in its instance field _latestStatuses (ServiceHealthMonitor.cs:24). Because a new instance is created for every DI resolution:
GET /api/monitoring/health/servicesalways returns an empty list — the controller (MonitoringController.cs:59) gets a freshServiceHealthMonitorwith an empty_latestStatuses.GET /api/monitoring/dashboardalways returns emptyservices— same reason (MonitoringController.cs:95).ConsecutiveFailuresnever accumulates across health check cycles — theHealthCheckBackgroundServicecreates a new scope (and thus a newServiceHealthMonitor) each cycle (HealthCheckBackgroundService.cs:31-32), so the lookup atServiceHealthMonitor.cs:69never finds a previous status.
The _latestStatuses state needs to persist across requests. Either extract the state into a separate singleton, or register ServiceHealthMonitor differently (e.g., using AddHttpClient with an explicit singleton wrapper for the state).
Prompt for agents
The problem is that AddHttpClient<IServiceHealthMonitor, ServiceHealthMonitor>() registers ServiceHealthMonitor as transient, but the class stores state in _latestStatuses (a ConcurrentDictionary) that must persist across requests and background service cycles.
Affected file: src/Services/Monitoring/AIMonitoringAgent/Services/ServiceHealthMonitor.cs (field _latestStatuses on line 24)
Registration: src/Services/Monitoring/AIMonitoringAgent/Program.cs line 28
Consumers: MonitoringController.cs (lines 59, 95) and HealthCheckBackgroundService.cs (lines 31-33)
Approach 1: Extract _latestStatuses into a separate singleton service (e.g. IServiceHealthStateStore) that both ServiceHealthMonitor instances and controllers can share.
Approach 2: Register a named HttpClient and manually create ServiceHealthMonitor as a singleton that takes IHttpClientFactory instead of HttpClient directly. Change the constructor to accept IHttpClientFactory and create clients on demand.
Approach 2 is cleaner because it keeps the state co-located with the health monitor logic while still getting proper HttpClient management via the factory pattern.
Was this helpful? React with 👍 or 👎 to provide feedback.
| environment: | ||
| - ASPNETCORE_ENVIRONMENT=Development | ||
| - Monitoring__ApplicationInsightsConnectionString= |
There was a problem hiding this comment.
🔴 Docker Compose health check endpoints use localhost instead of Docker service names
The ServiceEndpoints in appsettings.json default to localhost URLs (e.g., http://localhost:5001/healthz). The docker-compose.yml adds the ai-monitoring-agent service but does not override these endpoints with Docker Compose service names. In Docker Compose networking, localhost inside the monitoring agent container resolves to the container itself—not the other services. All background health checks (HealthCheckBackgroundService.cs:33 → ServiceHealthMonitor.CheckAllServicesAsync) will fail with connection refused errors every 30 seconds. The endpoints should be http://identity-service:5001/healthz, http://customer-service:5002/healthz, etc.
| environment: | |
| - ASPNETCORE_ENVIRONMENT=Development | |
| - Monitoring__ApplicationInsightsConnectionString= | |
| environment: | |
| - ASPNETCORE_ENVIRONMENT=Development | |
| - Monitoring__ApplicationInsightsConnectionString= | |
| - Monitoring__ServiceEndpoints__identity-service=http://identity-service:5001/healthz | |
| - Monitoring__ServiceEndpoints__customer-service=http://customer-service:5002/healthz | |
| - Monitoring__ServiceEndpoints__order-service=http://order-service:5003/healthz | |
| - Monitoring__ServiceEndpoints__product-service=http://product-service:5004/healthz | |
| - Monitoring__ServiceEndpoints__notification-service=http://notification-service:5005/healthz | |
| - Monitoring__ServiceEndpoints__api-gateway=http://api-gateway:5000/healthz |
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
Adds a new AI Monitoring Agent microservice (
port 5006) undersrc/Services/Monitoring/AIMonitoringAgent/that integrates with Azure Application Insights to provide cross-cutting observability for the microservices platform.Key capabilities:
POST /api/monitoring/ai-model/track/healthzendpoints at configurable intervals, records availability telemetry to App InsightsGreaterThan,LessThan, etc.) and severity levels; alerts fire to App Insights eventsGET /api/monitoring/dashboardreturns aggregated AI model summaries, service health, and recent anomaliesFiles added/modified:
src/Services/Monitoring/AIMonitoringAgent/— Complete .NET 10 Web API project (models, services, controllers, configuration)src/docker-compose.yml— Addedai-monitoring-agentservice definitionREADME.md— Updated architecture table and project structureDefault alert rules included:
Review & Testing Checklist for Human
dotnet build src/Services/Monitoring/AIMonitoringAgent/AIMonitoringAgent.csprojto confirm the project compileshttp://localhost:5006/swaggerby runningdotnet runfrom the project directoryGET /api/monitoring/health/servicesappsettings.jsonand adjust for your environmentNotes
mcr.microsoft.com/dotnet/sdk:10.0-previewbase images to match the existing .NET 10 servicesLink to Devin session: https://partner-workshops.devinenterprise.com/sessions/d05917139ee340c89cb4b7ef82266e3c