add health check

This commit is contained in:
meysamhadeli 2022-06-14 02:55:41 +04:30
parent b144ac61a1
commit 489218dc95
8 changed files with 82 additions and 18 deletions

View File

@ -8,6 +8,10 @@
<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="3.3.0" />
<PackageReference Include="AspNetCore.HealthChecks.Elasticsearch" Version="6.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.EventStore" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="6.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.SQLite.Storage" Version="5.0.1" />
<PackageReference Include="Ben.BlockingDetector" Version="0.0.4" />
<PackageReference Include="EasyCaching.Core" Version="1.4.1" />
@ -66,8 +70,8 @@
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="6.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1" />

View File

@ -1,6 +1,6 @@
namespace BuildingBlocks.EFCore;
public class DatabaseOptions
public class SqlOptions
{
public string ConnectionString { get; set; }
public string DefaultConnection { get; set; }
}

View File

@ -0,0 +1,56 @@
using BuildingBlocks.EFCore;
using BuildingBlocks.MassTransit;
using BuildingBlocks.Web;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace BuildingBlocks.HealthCheck;
public static class Extensions
{
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services)
{
var sqlOptions = services.GetOptions<SqlOptions>("ConnectionStrings");
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
services.AddHealthChecks()
.AddSqlServer(sqlOptions.DefaultConnection)
.AddRabbitMQ(rabbitConnectionString:
$"amqp://{rabbitMqOptions.UserName}:{rabbitMqOptions.Password}@{rabbitMqOptions.HostName}");
services.AddHealthChecksUI(setup =>
{
setup.SetEvaluationTimeInSeconds(60); // time in seconds between check
setup.AddHealthCheckEndpoint("Basic Health Check", "/healthz");
}).AddInMemoryStorage();
return services;
}
public static WebApplication UseCustomHealthCheck(this WebApplication app)
{
app.UseHealthChecks("/healthz",
new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status500InternalServerError,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable
}
})
.UseHealthChecksUI(options =>
{
options.ApiPath = "/healthcheck";
options.UIPath = "/healthcheck-ui";
});
return app;
}
}

View File

@ -5,6 +5,7 @@ using Booking.Extensions;
using BuildingBlocks.Domain;
using BuildingBlocks.EFCore;
using BuildingBlocks.EventStoreDB;
using BuildingBlocks.HealthCheck;
using BuildingBlocks.IdsGenerator;
using BuildingBlocks.Jwt;
using BuildingBlocks.Logging;
@ -46,7 +47,7 @@ builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<IEventMapper, EventMapper>();
builder.Services.AddTransient<IBusPublisher, BusPublisher>();
builder.Services.AddCustomHealthCheck();
builder.Services.AddCustomMassTransit(typeof(BookingRoot).Assembly, env);
builder.Services.AddCustomOpenTelemetry();
builder.Services.AddTransient<AuthHeaderHandler>();
@ -76,6 +77,7 @@ app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseProblemDetails();
app.UseCustomHealthCheck();
app.UseEndpoints(endpoints =>
{

View File

@ -3,6 +3,7 @@ using BuildingBlocks.Caching;
using BuildingBlocks.Domain;
using BuildingBlocks.EFCore;
using BuildingBlocks.Exception;
using BuildingBlocks.HealthCheck;
using BuildingBlocks.IdsGenerator;
using BuildingBlocks.Jwt;
using BuildingBlocks.Logging;
@ -17,7 +18,9 @@ using Flight.Data;
using Flight.Data.Seed;
using Flight.Extensions;
using FluentValidation;
using HealthChecks.UI.Client;
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Prometheus;
using Serilog;
@ -48,6 +51,7 @@ builder.Services.AddTransient<IEventMapper, EventMapper>();
builder.Services.AddCustomMassTransit(typeof(FlightRoot).Assembly, env);
builder.Services.AddCustomOpenTelemetry();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddCustomHealthCheck();
builder.Services.AddGrpc(options =>
{
@ -58,10 +62,7 @@ builder.Services.AddMagicOnion();
SnowFlakIdGenerator.Configure(1);
builder.Services.AddCachingRequest(new List<Assembly>
{
typeof(FlightRoot).Assembly
});
builder.Services.AddCachingRequest(new List<Assembly> {typeof(FlightRoot).Assembly});
builder.Services.AddEasyCaching(options => { options.UseInMemory(configuration, "mem"); });
@ -80,6 +81,7 @@ app.UseHttpMetrics();
app.UseMigrations();
app.UseProblemDetails();
app.UseHttpsRedirection();
app.UseCustomHealthCheck();
app.UseAuthentication();
app.UseAuthorization();
@ -93,4 +95,6 @@ app.UseEndpoints(endpoints =>
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));
app.Run();
public partial class Program {}
public partial class Program
{
}

View File

@ -20,11 +20,5 @@
"UserName": "guest",
"Password": "guest"
},
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2",
"Url": "https://localhost:5003"
}
},
"AllowedHosts": "*"
}

View File

@ -1,5 +1,6 @@
using BuildingBlocks.Domain;
using BuildingBlocks.EFCore;
using BuildingBlocks.HealthCheck;
using BuildingBlocks.Logging;
using BuildingBlocks.Mapster;
using BuildingBlocks.MassTransit;
@ -42,7 +43,7 @@ builder.Services.AddValidatorsFromAssembly(typeof(IdentityRoot).Assembly);
builder.Services.AddCustomProblemDetails();
builder.Services.AddCustomMapster(typeof(IdentityRoot).Assembly);
builder.Services.AddScoped<IDataSeeder, IdentityDataSeeder>();
builder.Services.AddCustomHealthCheck();
builder.Services.AddTransient<IEventMapper, EventMapper>();
builder.Services.AddTransient<IBusPublisher, BusPublisher>();
@ -69,6 +70,7 @@ app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseIdentityServer();
app.UseCustomHealthCheck();
app.UseEndpoints(endpoints =>
{

View File

@ -1,6 +1,7 @@
using BuildingBlocks.Domain;
using BuildingBlocks.EFCore;
using BuildingBlocks.Exception;
using BuildingBlocks.HealthCheck;
using BuildingBlocks.IdsGenerator;
using BuildingBlocks.Jwt;
using BuildingBlocks.Logging;
@ -39,7 +40,7 @@ builder.Services.AddCustomProblemDetails();
builder.Services.AddCustomMapster(typeof(PassengerRoot).Assembly);
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<IEventMapper, EventMapper>();
builder.Services.AddCustomHealthCheck();
builder.Services.AddCustomMassTransit(typeof(PassengerRoot).Assembly, env);
builder.Services.AddCustomOpenTelemetry();
builder.Services.AddGrpc(options =>
@ -67,6 +68,7 @@ app.UseProblemDetails();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseCustomHealthCheck();
app.UseEndpoints(endpoints =>
{