feat(api): expose /health endpoint and health dashboard

Wire the infrastructure health checks into ASP.NET Core:

- /health returns JSON via UIResponseWriter and maps Unhealthy to HTTP 503
- /health-ui renders a Razor Pages dashboard with per-check status, description, tags and duration
- Tag health checks (rabbitmq, workers, ready, live) for monitoring filters
- Link the dashboard from the Swagger description and the page layout
This commit is contained in:
2026-11-02 10:11:14 +01:00
parent 2fdc8982aa
commit 292b6e9f36
6 changed files with 164 additions and 2 deletions

View File

@@ -1,6 +1,10 @@
using DigitalData.MessagingService.API.Middleware;
using DigitalData.MessagingService.Application;
using DigitalData.MessagingService.Infrastructure;
using DigitalData.MessagingService.Infrastructure.Services.Background;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Serilog;
using Serilog.Ui.Core.Extensions;
using Serilog.Ui.SqliteDataProvider.Extensions;
@@ -66,7 +70,14 @@ try
// Register Infrastructure layer (RabbitMQ, Repositories, etc.)
builder.Services.AddInfrastructure(builder.Configuration);
// Health checks — each check corresponds to a singleton registered in the Infrastructure layer
builder.Services.AddHealthChecks()
.AddCheck<ConsumerInitHealthCheck>("rabbitmq-consumers", tags: ["rabbitmq", "ready"])
.AddCheck<RabbitMqConnectionHealthCheck>("rabbitmq-connection", tags: ["rabbitmq", "live"])
.AddCheck<EmailSyncWorkerHealthCheck>("email-sync-worker", tags: ["workers", "live"]);
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
@@ -95,6 +106,7 @@ try
## Weiterführende Links
- [Serilog Log-Viewer &nearr;](/serilog-ui)
- [Health Status &nearr;](/health-ui)
"""
});
});
@@ -155,6 +167,22 @@ try
app.MapControllers();
// Health check endpoint — raw JSON, used by HealthChecks UI and monitoring tools
// Returns HTTP 503 if any check tagged 'ready' is Unhealthy
app.MapHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
}
});
app.MapRazorPages();
Log.Information("MessagingService API started successfully");
app.Run();