diff --git a/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/Startup.cs b/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/Startup.cs index 51b8456..0095770 100644 --- a/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/Startup.cs +++ b/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/Startup.cs @@ -10,17 +10,30 @@ namespace TransactionProcessor.HealthChecksUI { + using HealthChecks.UI.Core; + using Microsoft.Extensions.Diagnostics.HealthChecks; + using System.IO; + using System.Net; using System.Net.Http; + using System.Net.Http.Headers; + using System.Text; + using System.Text.Json; + using System.Text.Json.Serialization; + using System.Threading; + using Newtonsoft.Json; + using JsonSerializer = System.Text.Json.JsonSerializer; public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 - public void ConfigureServices(IServiceCollection services) - { + public void ConfigureServices(IServiceCollection services){ + services.AddTransient(); + services.AddHealthChecksUI(settings => { settings.UseApiEndpointHttpMessageHandler(ApiEndpointHttpHandler); + settings.UseApiEndpointDelegatingHandler(); }).AddInMemoryStorage(); } @@ -47,11 +60,40 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } app.UseRouting(); - + app.UseEndpoints(endpoints => { endpoints.MapHealthChecksUI(); }); } } + + public class CustomHealthReportDelegatingHandler : DelegatingHandler + { + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + request.Headers.Add("Accept", new[] { "application/json" }); + HttpResponseMessage response = null; + try + { + response = await base.SendAsync(request, cancellationToken); + } + catch(HttpRequestException ex){ + // Just ignore this + JsonSerializerOptions _options = new(JsonSerializerDefaults.Web) + { + Converters = + { + new JsonStringEnumConverter(namingPolicy: null, allowIntegerValues: false) + } + }; + UIHealthReport report = UIHealthReport.CreateFrom(ex); + report.Status = UIHealthStatus.Unhealthy; + String json = JsonSerializer.Serialize(report, _options); + response = new HttpResponseMessage(HttpStatusCode.InternalServerError); + response.Content = new StringContent(json, Encoding.UTF8, "application/json"); + } + return response; + } + } } diff --git a/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.json b/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.json index eec71de..bc95e78 100644 --- a/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.json +++ b/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.json @@ -12,35 +12,7 @@ { "Name": "Estate Management Service", "Uri": "http://192.168.0.133:5000/healthui" - }, - { - "Name": "Security Service", - "Uri": "https://192.168.0.133:5001/healthui" - }, - { - "Name": "Transaction Processor Service", - "Uri": "http://192.168.0.133:5002/healthui" - }, - { - "Name": "Transaction Processor ACL Service", - "Uri": "http://192.168.0.133:5003/healthui" - }, - { - "Name": "Estate Management UI", - "Uri": "https://192.168.0.133:5004/healthui" - }, - { - "Name": "Messaging Service", - "Uri": "http://192.168.0.133:5006/healthui" - }, - { - "Name": "Voucher Management ACL Service", - "Uri": "http://192.168.0.133:5008/healthui" - }, - { - "Name": "File Processor Service", - "Uri": "http://192.168.0.133:5009/healthui" - } + } ], "EvaluationTimeInSeconds": 300, "MinimumSecondsBetweenFailureNotifications": 60 diff --git a/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.staging.json b/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.staging.json index 1ba065f..7a2d683 100644 --- a/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.staging.json +++ b/TransactionProcessor.HealthChecksUI/TransactionProcessor.HealthChecksUI/appsettings.staging.json @@ -8,48 +8,48 @@ }, "AllowedHosts": "*", "HealthChecksUI": { - // "HealthChecks": [ - // { - // "Name": "Estate Management Service", - // "Uri": "http://192.168.0.133:5000/health" - // }, - // { - // "Name": "Security Service", - // "Uri": "http://192.168.0.133:5001/health" - // }, - // { - // "Name": "Transaction Processor Service", - // "Uri": "http://192.168.0.133:5002/health" - // }, - // { - // "Name": "Transaction Processor ACL Service", - // "Uri": "http://192.168.0.133:5003/health" - // }, - // { - // "Name": "Estate Management UI", - // "Uri": "http://192.168.0.133:5004/health" - // }, - // { - // "Name": "Estate Reporting Service", - // "Uri": "http://192.168.0.133:5005/health" - // }, - // { - // "Name": "Messaging Service", - // "Uri": "http://192.168.0.133:5006/health" - // }, - // { - // "Name": "Voucher Management Service", - // "Uri": "http://192.168.0.133:5007/health" - // }, - // { - // "Name": "Voucher Management ACL Service", - // "Uri": "http://192.168.0.133:5008/health" - // }, - // { - // "Name": "File Processor Service", - // "Uri": "http://192.168.0.133:5009/health" - // } - // ], + "HealthChecks": [ + { + "Name": "Estate Management Service", + "Uri": "http://192.168.0.133:5000/health" + }, + { + "Name": "Security Service", + "Uri": "http://192.168.0.133:5001/health" + }, + { + "Name": "Transaction Processor Service", + "Uri": "http://192.168.0.133:5002/health" + }, + { + "Name": "Transaction Processor ACL Service", + "Uri": "http://192.168.0.133:5003/health" + }, + { + "Name": "Estate Management UI", + "Uri": "http://192.168.0.133:5004/health" + }, + { + "Name": "Estate Reporting Service", + "Uri": "http://192.168.0.133:5005/health" + }, + { + "Name": "Messaging Service", + "Uri": "http://192.168.0.133:5006/health" + }, + { + "Name": "Voucher Management Service", + "Uri": "http://192.168.0.133:5007/health" + }, + { + "Name": "Voucher Management ACL Service", + "Uri": "http://192.168.0.133:5008/health" + }, + { + "Name": "File Processor Service", + "Uri": "http://192.168.0.133:5009/health" + } + ], "EvaluationTimeInSeconds": 300, "MinimumSecondsBetweenFailureNotifications": 60 }