Skip to content

Commit

Permalink
Health Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Dec 16, 2024
1 parent 3d6fe18 commit 94b45f1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Monolith/ClassifiedAds.AspireAppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var background = builder.AddProject<Projects.ClassifiedAds_Background>("ClassifiedAds-Background");
var graphQL = builder.AddProject<Projects.ClassifiedAds_GraphQL>("ClassifiedAds-GraphQL");
var webApi = builder.AddProject<Projects.ClassifiedAds_WebAPI>("ClassifiedAds-WebAPI");
var webMvc = builder.AddProject<Projects.ClassifiedAds_WebMVC>("ClassifiedAds-WebMVC");
var webMvc = builder.AddProject<Projects.ClassifiedAds_WebMVC>("ClassifiedAds-WebMVC").WithHttpsHealthCheck("/healthz");
var blazorServerSide = builder.AddProject<Projects.ClassifiedAds_BlazorServerSide>("ClassifiedAds-BlazorServerSide");
var blazorWebAssembly = builder.AddProject<Projects.ClassifiedAds_BlazorWebAssembly>("ClassifiedAds-BlazorWebAssembly");

Expand Down
7 changes: 3 additions & 4 deletions src/Monolith/ClassifiedAds.Background/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@
failureStatus: HealthStatus.Degraded)
.AddMessageBusHealthCheck(appSettings.MessageBroker);

services.Configure<HealthChecksBackgroundServiceOptions>(x => x.Interval = TimeSpan.FromMinutes(10));
services.AddHostedService<HealthChecksBackgroundService>();

services.AddHostedService<MessageBusConsumerBackgroundService<WebhookConsumer, FileUploadedEvent>>();
services.AddHostedService<MessageBusConsumerBackgroundService<WebhookConsumer, FileDeletedEvent>>();
services.AddHostedService<PublishEventWorker>();
services.AddHostedService<SendEmailWorker>();
services.AddHostedService<SendSmsWorker>();
services.AddHostedService<ScheduleCronJobWorker>();
services.AddHostedService<SyncUsersWorker>();

services.Configure<HealthChecksBackgroundServiceOptions>(x => x.Interval = TimeSpan.FromMinutes(10));
services.AddHostedService<HealthChecksBackgroundService>();

})
.Build()
.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.HealthChecks;

public static class HealthChecksResponseWriter
{
public static Task WriteReponse(HttpContext context, HealthReport healthReport)
{
context.Response.ContentType = "application/json; charset=utf-8";

var options = new JsonWriterOptions { Indented = true };

using var memoryStream = new MemoryStream();
using (var jsonWriter = new Utf8JsonWriter(memoryStream, options))
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("status", healthReport.Status.ToString());
jsonWriter.WriteStartObject("results");

foreach (var healthReportEntry in healthReport.Entries)
{
jsonWriter.WriteStartObject(healthReportEntry.Key);
jsonWriter.WriteString("status", healthReportEntry.Value.Status.ToString());
jsonWriter.WriteString("description", healthReportEntry.Value.Description ?? healthReportEntry.Value.Exception?.Message.ToString());
jsonWriter.WriteStartObject("data");

foreach (var item in healthReportEntry.Value.Data)
{
jsonWriter.WritePropertyName(item.Key);

JsonSerializer.Serialize(jsonWriter, item.Value, item.Value?.GetType() ?? typeof(object));
}

jsonWriter.WriteEndObject();

jsonWriter.WriteEndObject();
}

jsonWriter.WriteEndObject();

jsonWriter.WriteEndObject();
}

return context.Response.WriteAsync(Encoding.UTF8.GetString(memoryStream.ToArray()));
}
}
3 changes: 0 additions & 3 deletions src/Monolith/ClassifiedAds.WebMVC/ClassifiedAds.WebMVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="7.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="7.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="7.0.0" />
<PackageReference Include="IdentityModel" Version="6.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
Expand Down
13 changes: 4 additions & 9 deletions src/Monolith/ClassifiedAds.WebMVC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ClassifiedAds.Domain.Identity;
using ClassifiedAds.Infrastructure.Configuration;
using ClassifiedAds.Infrastructure.HealthChecks;
using ClassifiedAds.Infrastructure.HostedServices;
using ClassifiedAds.Infrastructure.Identity;
using ClassifiedAds.Infrastructure.Logging;
using ClassifiedAds.Infrastructure.Monitoring;
Expand All @@ -12,7 +13,6 @@
using ClassifiedAds.WebMVC.Filters;
using ClassifiedAds.WebMVC.HttpMessageHandlers;
using ClassifiedAds.WebMVC.Middleware;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
Expand Down Expand Up @@ -151,11 +151,8 @@
failureStatus: HealthStatus.Degraded)
.AddStorageManagerHealthCheck(appSettings.Storage);

services.AddHealthChecksUI(setupSettings: setup =>
{
setup.AddHealthCheckEndpoint("Basic Health Check", $"{appSettings.CurrentUrl}/healthz");
setup.DisableDatabaseMigrations();
}).AddInMemoryStorage();
services.Configure<HealthChecksBackgroundServiceOptions>(x => x.Interval = TimeSpan.FromMinutes(10));
services.AddHostedService<HealthChecksBackgroundService>();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ICurrentUser, CurrentWebUser>();
Expand Down Expand Up @@ -204,7 +201,7 @@
app.UseHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
ResponseWriter = HealthChecksResponseWriter.WriteReponse,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
Expand All @@ -213,8 +210,6 @@
},
});

app.UseHealthChecksUI(); // /healthchecks-ui#/healthchecks

app.MapDefaultControllerRoute();
app.MapClassifiedAdsHubs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
}
<feature name="@FeatureManagement.HealthChecksUI">
<li class="nav-item">
<a class="nav-link text-dark" href="/healthchecks-ui#/healthchecks">Health Checks UI</a>
<a class="nav-link text-dark" href="/healthz">Health Checks</a>
</li>
</feature>
<li class="nav-item">
Expand Down

0 comments on commit 94b45f1

Please sign in to comment.