Add health check
Some checks failed
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is failing

This commit is contained in:
Kasper Juul Hermansen 2021-11-14 22:32:49 +01:00
parent 232ac18bf7
commit 224a5e5ff5
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
4 changed files with 52 additions and 9 deletions

View File

@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -37,7 +39,10 @@ namespace Todo.Api
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo.Api", Version = "v1" }); c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo.Api", Version = "v1" });
}); });
services.AddPersistence(Configuration); services.AddPersistence(Configuration, out var mongoDbOptions);
services
.AddHealthChecks()
.AddMongoDb(MongoDbConnectionHandler.FormatConnectionString(mongoDbOptions));
services.AddSignalR(); services.AddSignalR();
} }
@ -62,7 +67,39 @@ namespace Todo.Api
{ {
endpoints.MapControllers(); endpoints.MapControllers();
endpoints.MapHub<TodoHub>("/hubs/todo"); endpoints.MapHub<TodoHub>("/hubs/todo");
endpoints.MapHealthChecks("/health/live", new HealthCheckOptions()
{
ResponseWriter = async (context, report) =>
{
var response = new HealthCheckResponse()
{
Status = report.Status.ToString(),
HealthChecks = report.Entries.Select(x => new IndividualHealthCheckResponse
{
Component = x.Key,
Status = x.Value.Status.ToString(),
Description = x.Value.Description
}),
HealthCheckDuration = report.TotalDuration
};
await context.Response.WriteAsJsonAsync(response);
}
});
}); });
} }
private class HealthCheckResponse
{
public string Status { get; set; }
public IEnumerable<IndividualHealthCheckResponse> HealthChecks { get; set; }
public TimeSpan HealthCheckDuration { get; set; }
}
private class IndividualHealthCheckResponse
{
public string Status { get; set; }
public string Component { get; set; }
public string Description { get; set; }
}
} }
} }

View File

@ -4,6 +4,7 @@ global using System.Linq;
global using System.Collections.Generic; global using System.Collections.Generic;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Todo.Core.Interfaces.Persistence; using Todo.Core.Interfaces.Persistence;
using Todo.Persistence.Mongo; using Todo.Persistence.Mongo;
using Todo.Persistence.Mongo.Repositories; using Todo.Persistence.Mongo.Repositories;
@ -12,11 +13,13 @@ namespace Todo.Persistence
{ {
public static class DependencyInjection public static class DependencyInjection
{ {
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration,
out MongoDbOptions mongoDbOptions)
{ {
var exportableMongoDbOptions = new MongoDbOptions();
var options = configuration.GetRequiredSection("MONGODB"); var options = configuration.GetRequiredSection("MONGODB");
Console.WriteLine(options.Value); options.Bind(exportableMongoDbOptions);
mongoDbOptions = exportableMongoDbOptions;
services services
.AddOptions<MongoDbOptions>() .AddOptions<MongoDbOptions>()

View File

@ -21,10 +21,12 @@ public class MongoDbConnectionHandler
private static IMongoDatabase CreateConnectionFromOptions(MongoDbOptions options) private static IMongoDatabase CreateConnectionFromOptions(MongoDbOptions options)
{ {
var conn = new MongoClient( var conn = new MongoClient(FormatConnectionString(options));
$"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}");
var database = conn.GetDatabase(options.Database); var database = conn.GetDatabase(options.Database);
return database; return database;
} }
public static string FormatConnectionString(MongoDbOptions options) =>
$"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}";
} }

View File

@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.13.2" /> <PackageReference Include="MongoDB.Driver" Version="2.13.2" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.13.2" /> <PackageReference Include="MongoDB.Driver.Core" Version="2.13.2" />
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="5.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>