Path: redb-route/redb.Route/Extensions/RouteHealthCheck.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Extensions/RouteHealthCheck.cs ---- #pragma warning disable CS0619 using Microsoft.Extensions.Diagnostics.HealthChecks; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.Extensions; /// /// Health check that reports per-route status and endpoint health. /// Aggregates route lifecycle state and endpoint statistics into an overall health result. /// /// Unhealthy: an Errored route, or a route whose source endpoint is Critical or Failed. /// Degraded: a stopped or suspended route, a source endpoint with a Warning, or an endpoint the routes send to /// that is Critical or Failed (a broker that blocks it, a high error rate); those are listed under /// endpoints by masked URI, at most . URIs in the result never carry secrets. /// /// public sealed class RouteHealthCheck : IHealthCheck { /// How many failing send-side endpoints the result lists; the rest are counted in endpointsNotListed. public const int MaxListedEndpoints = 10; private readonly IRouteContext _context; /// Creates a route context health check. /// The route context instance. public RouteHealthCheck(IRouteContext context) { _context = context ?? throw new ArgumentNullException(nameof(context)); } /// public Task CheckHealthAsync( HealthCheckContext context, CancellationToken cancellationToken = default) { var routes = _context.Routes; if (routes.Count == 0) { return Task.FromResult(HealthCheckResult.Degraded( "No routes are registered.")); } var overallStatus = HealthStatus.Healthy; var routeDetails = new List(routes.Count); foreach (var route in routes) { var endpointHealth = GetEndpointHealth(route); routeDetails.Add(new Dictionary { ["routeId"] = route.RouteId, ["fromUri"] = route.FromUri, ["status"] = route.Status.ToString(), ["endpointHealth"] = endpointHealth?.ToString(), ["errors"] = GetErrorCount(route), ["lastError"] = GetLastError(route), }); // Route status aggregation // An Errored route is Unhealthy unless ConsumerStartRetry's UnhealthyOnRestarting / UnhealthyOnExhausted // say Degraded for its phase (Camel's supervising route controller switches). if (route.Status == RouteStatus.Errored && (_context is not RouteContext routeContext || routeContext.IsErroredRouteUnhealthy(route))) { overallStatus = HealthStatus.Unhealthy; } else if (route.Status == RouteStatus.Errored) { if (overallStatus != HealthStatus.Unhealthy) overallStatus = HealthStatus.Degraded; } else if (route.Status is RouteStatus.Stopped or RouteStatus.Suspended or RouteStatus.Suspending or RouteStatus.Stopping && overallStatus != HealthStatus.Unhealthy) { overallStatus = HealthStatus.Degraded; } // Endpoint health aggregation if (endpointHealth is EndpointHealthStatus.Critical or EndpointHealthStatus.Failed) { overallStatus = HealthStatus.Unhealthy; } else if (endpointHealth == EndpointHealthStatus.Warning && overallStatus != HealthStatus.Unhealthy) { overallStatus = HealthStatus.Degraded; } } // The endpoints the routes send to. One that knows it is broken (a broker that blocks it, a high error rate) // makes the context Degraded, never Unhealthy: the routes still run and their consumers still take messages, // so a readiness probe must not read a broker's alarm as a dead node. Only such endpoints are listed, at most // MaxListedEndpoints, under their masked URIs: a context with many dynamic endpoints keeps a small payload. var sources = new HashSet(routes.Select(r => r.Endpoint), ReferenceEqualityComparer.Instance); var failingEndpoints = new List(); var notListed = 0; foreach (var endpoint in _context.GetEndpoints()) { if (sources.Contains(endpoint) || endpoint is not IEndpointStatistics stats) continue; var status = stats.HealthStatus; if (status is not (EndpointHealthStatus.Critical or EndpointHealthStatus.Failed)) continue; if (overallStatus == HealthStatus.Healthy) overallStatus = HealthStatus.Degraded; if (failingEndpoints.Count == MaxListedEndpoints) { notListed++; continue; } failingEndpoints.Add(new Dictionary { ["uri"] = endpoint.Uri.ToMaskedUriString(), ["endpointHealth"] = status.ToString(), ["reason"] = stats.HealthReason, }); } var data = new Dictionary { ["routeCount"] = routes.Count, ["routes"] = routeDetails, }; if (failingEndpoints.Count > 0) { data["endpoints"] = failingEndpoints; if (notListed > 0) data["endpointsNotListed"] = notListed; } var description = overallStatus switch { HealthStatus.Healthy => $"{routes.Count} route(s) running.", HealthStatus.Degraded => $"{routes.Count} route(s), some degraded.", _ => $"{routes.Count} route(s), errors detected." }; var result = new HealthCheckResult(overallStatus, description, data: data); return Task.FromResult(result); } private static EndpointHealthStatus? GetEndpointHealth(CompiledRoute route) { return route.Endpoint is IEndpointStatistics stats ? stats.HealthStatus : null; } private static long GetErrorCount(CompiledRoute route) { return route.Endpoint is IEndpointStatistics stats ? stats.Errors : 0; } private static string? GetLastError(CompiledRoute route) { if (route.Endpoint is IEndpointStatistics stats && !string.IsNullOrEmpty(stats.LastErrorMessage)) return stats.LastErrorMessage; return null; } }