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 94b45f1 commit ace5077
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/Monolith/ClassifiedAds.AspireAppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var migrator = builder.AddProject<Projects.ClassifiedAds_Migrator>("ClassifiedAds-Migrator");
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 graphQL = builder.AddProject<Projects.ClassifiedAds_GraphQL>("ClassifiedAds-GraphQL").WithHttpsHealthCheck("/healthz");
var webApi = builder.AddProject<Projects.ClassifiedAds_WebAPI>("ClassifiedAds-WebAPI").WithHttpsHealthCheck("/healthz");
var webMvc = builder.AddProject<Projects.ClassifiedAds_WebMVC>("ClassifiedAds-WebMVC").WithHttpsHealthCheck("/healthz");
var blazorServerSide = builder.AddProject<Projects.ClassifiedAds_BlazorServerSide>("ClassifiedAds-BlazorServerSide");
var blazorServerSide = builder.AddProject<Projects.ClassifiedAds_BlazorServerSide>("ClassifiedAds-BlazorServerSide").WithHttpsHealthCheck("/healthz");
var blazorWebAssembly = builder.AddProject<Projects.ClassifiedAds_BlazorWebAssembly>("ClassifiedAds-BlazorWebAssembly");

var identityServer = builder
Expand Down
24 changes: 24 additions & 0 deletions src/Monolith/ClassifiedAds.BlazorServerSide/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
using ClassifiedAds.Blazor.Modules.Users.Services;
using ClassifiedAds.BlazorServerSide.ConfigurationOptions;
using ClassifiedAds.BlazorServerSide.Services;
using ClassifiedAds.Infrastructure.HealthChecks;
using ClassifiedAds.Infrastructure.HostedServices;
using ClassifiedAds.Infrastructure.HttpMessageHandlers;
using ClassifiedAds.Infrastructure.Logging;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using System;

Expand Down Expand Up @@ -116,6 +121,13 @@
options.RequireHttpsMetadata = appSettings.OpenIdConnect.RequireHttpsMetadata;
});

services.AddHealthChecks()
.AddHttp(appSettings.OpenIdConnect.Authority, name: "Identity Server", failureStatus: HealthStatus.Degraded)
.AddHttp(appSettings.ResourceServer.Endpoint, name: "Resource (Web API) Server", failureStatus: HealthStatus.Degraded);

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

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down Expand Up @@ -147,4 +159,16 @@
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.UseHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = HealthChecksResponseWriter.WriteReponse,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status500InternalServerError,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
},
});

app.Run();
95 changes: 56 additions & 39 deletions src/Monolith/ClassifiedAds.GraphQL/Program.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,78 @@
using ClassifiedAds.Infrastructure.Logging;
using ClassifiedAds.GraphQL;
using ClassifiedAds.Infrastructure.HealthChecks;
using ClassifiedAds.Infrastructure.HostedServices;
using ClassifiedAds.Infrastructure.Logging;
using GraphQL;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;

namespace ClassifiedAds.GraphQL;
var builder = WebApplication.CreateBuilder(args);

public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var services = builder.Services;
var configuration = builder.Configuration;
// Add services to the container.
var services = builder.Services;
var configuration = builder.Configuration;

builder.WebHost.UseClassifiedAdsLogger(configuration =>
builder.WebHost.UseClassifiedAdsLogger(configuration =>
{
return new LoggingOptions();
});

// If using Kestrel:
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});

// If using IIS:
services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
// If using Kestrel:
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});

services.AddLogging(builder => builder.AddConsole());
services.AddHttpContextAccessor();
// If using IIS:
services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});

services.AddGraphQL(b => b.AddAutoSchema<ClassifiedAdsQuery>(s => s.WithMutation<ClassifiedAdsMutation>())
.AddSystemTextJson());
services.AddLogging(builder => builder.AddConsole());
services.AddHttpContextAccessor();

// Configure the HTTP request pipeline.
var app = builder.Build();
services.AddGraphQL(b => b.AddAutoSchema<ClassifiedAdsQuery>(s => s.WithMutation<ClassifiedAdsMutation>())
.AddSystemTextJson());

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
services.AddHealthChecks()
.AddHttp(configuration["ResourceServer:Endpoint"], name: "Resource (Web API) Server", failureStatus: HealthStatus.Degraded);

// add http for Schema at default url /graphql
app.UseGraphQL();
services.Configure<HealthChecksBackgroundServiceOptions>(x => x.Interval = TimeSpan.FromMinutes(10));
services.AddHostedService<HealthChecksBackgroundService>();

// use graphql-playground at default url /ui/playground
app.UseGraphQLPlayground();
// Configure the HTTP request pipeline.
var app = builder.Build();

app.Run();
}
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = HealthChecksResponseWriter.WriteReponse,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status500InternalServerError,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
},
});

// add http for Schema at default url /graphql
app.UseGraphQL();

// use graphql-playground at default url /ui/playground
app.UseGraphQLPlayground();

app.Run();
29 changes: 29 additions & 0 deletions src/Monolith/ClassifiedAds.WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using ClassifiedAds.Domain.Identity;
using ClassifiedAds.Infrastructure.Csv;
using ClassifiedAds.Infrastructure.Excel.ClosedXML;
using ClassifiedAds.Infrastructure.HealthChecks;
using ClassifiedAds.Infrastructure.HostedServices;
using ClassifiedAds.Infrastructure.Identity;
using ClassifiedAds.Infrastructure.Localization;
using ClassifiedAds.Infrastructure.Logging;
Expand All @@ -20,10 +22,12 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -221,6 +225,19 @@

services.AddCaches(appSettings.Caching);

services.AddHealthChecks()
.AddSqlServer(connectionString: appSettings.ConnectionStrings.ClassifiedAds,
healthQuery: "SELECT 1;",
name: "Sql Server",
failureStatus: HealthStatus.Degraded)
.AddHttp(appSettings.IdentityServerAuthentication.Authority,
name: "Identity Server",
failureStatus: HealthStatus.Degraded)
.AddStorageManagerHealthCheck(appSettings.Storage);

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

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ICurrentUser, CurrentWebUser>();

Expand Down Expand Up @@ -284,6 +301,18 @@

app.UseMonitoringServices(appSettings.Monitoring);

app.UseHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = HealthChecksResponseWriter.WriteReponse,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status500InternalServerError,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
},
});

app.MapControllers();
app.MapHub<NotificationHub>("/hubs/notification").RequireCors("SignalRHubs");

Expand Down

0 comments on commit ace5077

Please sign in to comment.