| | | 1 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 4 | | |
| | | 5 | | namespace ProjectTemplate.Web.HealthChecks; |
| | | 6 | | |
| | 0 | 7 | | public sealed class ApplicationAuditIntegrityHealthCheck( |
| | 0 | 8 | | IServiceScopeFactory scopeFactory, |
| | 0 | 9 | | IOptions<ApplicationAuditReconciliationOptions> options) |
| | | 10 | | : IHealthCheck |
| | | 11 | | { |
| | 0 | 12 | | private readonly IServiceScopeFactory _scopeFactory = |
| | 0 | 13 | | scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); |
| | 0 | 14 | | private readonly ApplicationAuditReconciliationOptions _options = |
| | 0 | 15 | | options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | | 16 | | |
| | | 17 | | public async Task<HealthCheckResult> CheckHealthAsync( |
| | | 18 | | HealthCheckContext context, |
| | | 19 | | CancellationToken cancellationToken = default) |
| | | 20 | | { |
| | 0 | 21 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 22 | | if (!_options.Enabled) |
| | | 23 | | { |
| | 0 | 24 | | return HealthCheckResult.Healthy("Audit reconciliation is disabled."); |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | using IServiceScope scope = _scopeFactory.CreateScope(); |
| | 0 | 28 | | IApplicationAuditReconciler reconciler = scope.ServiceProvider |
| | 0 | 29 | | .GetRequiredService<IApplicationAuditReconciler>(); |
| | 0 | 30 | | ApplicationAuditReconciliationSummary summary = await reconciler |
| | 0 | 31 | | .GetSummaryAsync(cancellationToken) |
| | 0 | 32 | | .ConfigureAwait(false); |
| | | 33 | | |
| | 0 | 34 | | ApplicationAuditCompletionOutboxHealth? deliveryHealth = null; |
| | 0 | 35 | | IApplicationAuditCompletionOutboxQuery? outboxQuery = scope.ServiceProvider |
| | 0 | 36 | | .GetService<IApplicationAuditCompletionOutboxQuery>(); |
| | 0 | 37 | | if (outboxQuery is not null) |
| | | 38 | | { |
| | 0 | 39 | | deliveryHealth = await outboxQuery.GetHealthAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 40 | | scope.ServiceProvider |
| | 0 | 41 | | .GetRequiredService<ApplicationAuditReconciliationMetrics>() |
| | 0 | 42 | | .UpdateDelivery(deliveryHealth); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | var data = new Dictionary<string, object> |
| | 0 | 46 | | { |
| | 0 | 47 | | ["openFindings"] = summary.OpenFindingCount, |
| | 0 | 48 | | ["errorFindings"] = summary.ErrorFindingCount, |
| | 0 | 49 | | ["criticalFindings"] = summary.CriticalFindingCount, |
| | 0 | 50 | | ["manifestVerificationFailures"] = summary.ManifestVerificationFailureCount, |
| | 0 | 51 | | ["missingCompletions"] = summary.MissingCompletionCount, |
| | 0 | 52 | | ["staleDeliveryFindings"] = summary.StaleDeliveryCount, |
| | 0 | 53 | | ["deadLetterFindings"] = summary.DeadLetterCount |
| | 0 | 54 | | }; |
| | | 55 | | |
| | 0 | 56 | | if (summary.LastRunUtc.HasValue) |
| | | 57 | | { |
| | 0 | 58 | | data["lastReconciliationUtc"] = summary.LastRunUtc.Value; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | if (deliveryHealth is not null) |
| | | 62 | | { |
| | 0 | 63 | | data["outboxBacklog"] = deliveryHealth.BacklogCount; |
| | 0 | 64 | | data["outboxRetryCount"] = deliveryHealth.TotalRetryCount; |
| | 0 | 65 | | data["outboxDeadLetters"] = deliveryHealth.DeadLetterCount; |
| | 0 | 66 | | if (deliveryHealth.OldestPendingAge.HasValue) |
| | | 67 | | { |
| | 0 | 68 | | data["oldestPendingAgeSeconds"] = deliveryHealth.OldestPendingAge.Value.TotalSeconds; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | bool unhealthy = summary.CriticalFindingCount > 0 || |
| | 0 | 73 | | summary.ManifestVerificationFailureCount > 0 || |
| | 0 | 74 | | summary.OpenFindingCount >= _options.HealthUnhealthyFindingCount; |
| | 0 | 75 | | if (unhealthy) |
| | | 76 | | { |
| | 0 | 77 | | return HealthCheckResult.Unhealthy( |
| | 0 | 78 | | "Critical audit-integrity findings require operator review.", |
| | 0 | 79 | | data: data); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | bool degraded = summary.OpenFindingCount >= _options.HealthWarningFindingCount || |
| | 0 | 83 | | summary.StaleDeliveryCount > 0 || |
| | 0 | 84 | | summary.DeadLetterCount > 0 || |
| | 0 | 85 | | deliveryHealth?.DeadLetterCount > 0; |
| | 0 | 86 | | return degraded |
| | 0 | 87 | | ? HealthCheckResult.Degraded( |
| | 0 | 88 | | "Audit reconciliation or delivery findings require attention.", |
| | 0 | 89 | | data: data) |
| | 0 | 90 | | : HealthCheckResult.Healthy("Audit integrity and delivery state are within configured thresholds.", data); |
| | 0 | 91 | | } |
| | | 92 | | } |