diff --git a/src/Akka.HealthCheck.Hosting.Web.Example/Program.cs b/src/Akka.HealthCheck.Hosting.Web.Example/Program.cs index c612f13..696831e 100644 --- a/src/Akka.HealthCheck.Hosting.Web.Example/Program.cs +++ b/src/Akka.HealthCheck.Hosting.Web.Example/Program.cs @@ -31,7 +31,7 @@ public static async Task Main(string[] args) // Automatically detects which health checks were registered inside the health check middleware and maps their routes app.MapAkkaHealthCheckRoutes( prependPath:"/health", - optionConfigure: opt => + optionConfigure: (tags, opt) => { // Use a custom response writer to output a json of all reported statuses opt.ResponseWriter = Helper.JsonResponseWriter; diff --git a/src/Akka.HealthCheck.Hosting.Web/AkkaWebHostingExtensions.cs b/src/Akka.HealthCheck.Hosting.Web/AkkaWebHostingExtensions.cs index aea391d..dbd76f6 100644 --- a/src/Akka.HealthCheck.Hosting.Web/AkkaWebHostingExtensions.cs +++ b/src/Akka.HealthCheck.Hosting.Web/AkkaWebHostingExtensions.cs @@ -6,6 +6,7 @@ using System; +using System.Collections.Generic; using System.Linq; using Akka.HealthCheck.Hosting.Web.Probes; using Akka.Hosting; @@ -101,14 +102,15 @@ public static IServiceCollection WithAkkaClusterReadinessProbe(this IServiceColl #region WebApplication extension methods - public static IEndpointConventionBuilder MapAkkaHealthCheckService( + private static IEndpointConventionBuilder MapAkkaHealthCheckService( this IEndpointRouteBuilder builder, + ISet tags, string prependPath = "/healthz", - Action? optionConfigure = null) where T: IAkkaHealthcheck + Action, HealthCheckOptions>? optionConfigure = null) where T: IAkkaHealthcheck { var path = prependPath.SanitizePath(); var opt = new HealthCheckOptions(); - optionConfigure?.Invoke(opt); + optionConfigure?.Invoke(tags, opt); var type = typeof(T); return type switch @@ -130,8 +132,8 @@ public static IEndpointConventionBuilder MapAkkaHealthCheckService( public static IEndpointRouteBuilder MapAkkaHealthCheckRoutes( this IEndpointRouteBuilder builder, string prependPath = "/healthz", - Action? optionConfigure = null, - Action? endpointConfigure = null) + Action, HealthCheckOptions>? optionConfigure = null, + Action, IEndpointConventionBuilder>? endpointConfigure = null) { var services = builder.ServiceProvider; var hcOpt = services.GetService>(); @@ -141,60 +143,70 @@ public static IEndpointRouteBuilder MapAkkaHealthCheckRoutes( var containsLive = false; var containsReady = false; var regs = hcOpt.Value.Registrations.ToHashSet(); - if (regs.Any(r => r.Name == Helper.Names.Liveness)) + + var reg = regs.FirstOrDefault(r => r.Name == Helper.Names.Liveness); + if (reg is { }) { containsLive = true; - var epBuilder = builder.MapAkkaHealthCheckService(prependPath, optionConfigure); - endpointConfigure?.Invoke(epBuilder); + var epBuilder = builder.MapAkkaHealthCheckService(reg.Tags, prependPath, optionConfigure); + endpointConfigure?.Invoke(reg.Tags, epBuilder); } - if (regs.Any(r => r.Name == Helper.Names.ClusterLiveness)) + reg = regs.FirstOrDefault(r => r.Name == Helper.Names.ClusterLiveness); + if (reg is { }) { containsLive = true; - var epBuilder = builder.MapAkkaHealthCheckService(prependPath, optionConfigure); - endpointConfigure?.Invoke(epBuilder); + var epBuilder = builder.MapAkkaHealthCheckService(reg.Tags, prependPath, optionConfigure); + endpointConfigure?.Invoke(reg.Tags, epBuilder); } - if (regs.Any(r => r.Name == Helper.Names.PersistenceLiveness)) + reg = regs.FirstOrDefault(r => r.Name == Helper.Names.PersistenceLiveness); + if (reg is { }) { containsLive = true; - var epBuilder = builder.MapAkkaHealthCheckService(prependPath, optionConfigure); - endpointConfigure?.Invoke(epBuilder); + var epBuilder = builder.MapAkkaHealthCheckService(reg.Tags, prependPath, optionConfigure); + endpointConfigure?.Invoke(reg.Tags, epBuilder); } - if (regs.Any(r => r.Name == Helper.Names.Readiness)) + reg = regs.FirstOrDefault(r => r.Name == Helper.Names.Readiness); + if (reg is { }) { containsReady = true; - var epBuilder = builder.MapAkkaHealthCheckService(prependPath, optionConfigure); - endpointConfigure?.Invoke(epBuilder); + var epBuilder = builder.MapAkkaHealthCheckService(reg.Tags, prependPath, optionConfigure); + endpointConfigure?.Invoke(reg.Tags, epBuilder); } - if (regs.Any(r => r.Name == Helper.Names.ClusterReadiness)) + reg = regs.FirstOrDefault(r => r.Name == Helper.Names.ClusterReadiness); + if (reg is { }) { containsReady = true; - var epBuilder = builder.MapAkkaHealthCheckService(prependPath, optionConfigure); - endpointConfigure?.Invoke(epBuilder); + var epBuilder = builder.MapAkkaHealthCheckService(reg.Tags, prependPath, optionConfigure); + endpointConfigure?.Invoke(reg.Tags, epBuilder); } var path = prependPath.SanitizePath(); - var opt = new HealthCheckOptions(); - optionConfigure?.Invoke(opt); if (containsLive) { + var opt = new HealthCheckOptions(); + optionConfigure?.Invoke(Helper.Tags.Live.ToHashSet(), opt); var epBuilder = builder.MapHealthChecks($"{path}/{Helper.Tags.Live.ToPath()}", opt.WithPredicate(Helper.Filters.AllLiveness)); - endpointConfigure?.Invoke(epBuilder); + endpointConfigure?.Invoke(Helper.Tags.Live.ToHashSet(), epBuilder); } if (containsReady) { + var opt = new HealthCheckOptions(); + optionConfigure?.Invoke(Helper.Tags.Ready.ToHashSet(), opt); var epBuilder = builder.MapHealthChecks($"{path}/{Helper.Tags.Ready.ToPath()}", opt.WithPredicate(Helper.Filters.AllReadiness)); - endpointConfigure?.Invoke(epBuilder); + endpointConfigure?.Invoke(Helper.Tags.Ready.ToHashSet(), epBuilder); } if (containsReady || containsLive) { + var opt = new HealthCheckOptions(); + optionConfigure?.Invoke(Helper.Tags.Akka.ToHashSet(), opt); var epBuilder = builder.MapHealthChecks($"{path}/{Helper.Tags.Akka.ToPath()}", opt.WithPredicate(Helper.Filters.All)); - endpointConfigure?.Invoke(epBuilder); + endpointConfigure?.Invoke(Helper.Tags.Akka.ToHashSet(), epBuilder); } return builder; diff --git a/src/Akka.HealthCheck.Hosting.Web/Helper.cs b/src/Akka.HealthCheck.Hosting.Web/Helper.cs index d8bb7dd..8b7160c 100644 --- a/src/Akka.HealthCheck.Hosting.Web/Helper.cs +++ b/src/Akka.HealthCheck.Hosting.Web/Helper.cs @@ -17,11 +17,11 @@ public static class Helper { public static class Names { - public const string Liveness = "akka-liveness"; - public const string Readiness = "akka-readiness"; - public const string ClusterLiveness = "akka-cluster-liveness"; - public const string ClusterReadiness = "akka-cluster-readiness"; - public const string PersistenceLiveness = "akka-persistence-liveness"; + public static readonly string Liveness = string.Join("-", Tags.Liveness); + public static readonly string Readiness = string.Join("-", Tags.Readiness); + public static readonly string ClusterLiveness = string.Join("-", Tags.ClusterLiveness); + public static readonly string ClusterReadiness = string.Join("-", Tags.ClusterReadiness); + public static readonly string PersistenceLiveness = string.Join("-", Tags.PersistenceLiveness); } public static class Tags