-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Refactor probes and add more health checks (#1159)
- Adds health check for Redis, PosgreSQL and the wellknown-endpoints. - Ensures that we have different endpoints for readiness/liveness/startup/health Related to #292 <img width="542" alt="image" src="https://github.com/user-attachments/assets/5b71bfbc-1e83-427c-8042-e363ffbf8faa"> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Added health check capabilities for Redis, PostgreSQL, and well-known endpoints. - Introduced multiple health check endpoints: `/startup`, `/liveness`, `/readiness`, and `/health`. - Integrated health checks into the service collection for better monitoring. - Added a new project for utility functions related to health checks. - **Enhancements** - Improved health monitoring with a new HTTP client and health check configurations, including a self-check feature. - Added support for dynamic configuration of health check probes in deployment templates. - Updated API specifications to reflect new health check schemas and structures. - **Bug Fixes** - Enhanced error handling for health checks to provide clearer feedback on endpoint status. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Are Almaas <arealmaas@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Dialogporten Automation Bot <164321870+dialogporten-bot@users.noreply.github.com> Co-authored-by: Magnus Sandgren <5285192+MagnusSandgren@users.noreply.github.com>
- Loading branch information
1 parent
4c43e2f
commit 6889a96
Showing
14 changed files
with
280 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6474,4 +6474,4 @@ | |
"url": "https://altinn-dev-api.azure-api.net/dialogporten" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/RedisHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using StackExchange.Redis; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Digdir.Domain.Dialogporten.Infrastructure.HealthChecks; | ||
|
||
internal sealed class RedisHealthCheck : IHealthCheck | ||
{ | ||
private readonly InfrastructureSettings _settings; | ||
|
||
public RedisHealthCheck(IOptions<InfrastructureSettings> options) | ||
{ | ||
_settings = options?.Value ?? throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using var redis = await ConnectionMultiplexer.ConnectAsync(_settings.Redis.ConnectionString); | ||
var db = redis.GetDatabase(); | ||
await db.PingAsync(); | ||
return HealthCheckResult.Healthy("Redis connection is healthy."); | ||
} | ||
catch (RedisConnectionException ex) | ||
{ | ||
return HealthCheckResult.Unhealthy("Unable to connect to Redis.", exception: ex); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy("An unexpected error occurred while checking Redis health.", exception: ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Digdir.Library.Utils.AspNet.HealthChecks; | ||
using HealthChecks.UI.Client; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
|
||
namespace Digdir.Library.Utils.AspNet; | ||
|
||
public static class AspNetUtilitiesExtensions | ||
{ | ||
public static IServiceCollection AddAspNetHealthChecks( | ||
this IServiceCollection services, | ||
Action<AspNetUtilitiesSettings>? configure = null) | ||
=> services.AddAspNetHealthChecks((x, _) => configure?.Invoke(x)); | ||
|
||
public static IServiceCollection AddAspNetHealthChecks(this IServiceCollection services, Action<AspNetUtilitiesSettings, IServiceProvider>? configure = null) | ||
{ | ||
var optionsBuilder = services.AddOptions<AspNetUtilitiesSettings>(); | ||
|
||
if (configure is not null) | ||
{ | ||
optionsBuilder.Configure(configure); | ||
} | ||
|
||
return services | ||
.AddHealthChecks() | ||
.AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["self"]) | ||
.AddCheck<EndpointsHealthCheck>( | ||
"Endpoints", | ||
failureStatus: HealthStatus.Unhealthy, | ||
tags: ["external"]) | ||
.Services; | ||
} | ||
|
||
public static WebApplication MapAspNetHealthChecks(this WebApplication app) => | ||
app.MapHealthCheckEndpoint("/health/startup", check => check.Tags.Contains("dependencies")) | ||
.MapHealthCheckEndpoint("/health/liveness", check => check.Tags.Contains("self")) | ||
.MapHealthCheckEndpoint("/health/readiness", check => check.Tags.Contains("critical")) | ||
.MapHealthCheckEndpoint("/health", check => check.Tags.Contains("dependencies")) | ||
.MapHealthCheckEndpoint("/health/deep", check => check.Tags.Contains("dependencies") || check.Tags.Contains("external")); | ||
|
||
private static WebApplication MapHealthCheckEndpoint(this WebApplication app, string path, Func<HealthCheckRegistration, bool> predicate) | ||
{ | ||
app.MapHealthChecks(path, new HealthCheckOptions { Predicate = predicate, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); | ||
return app; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Digdir.Library.Utils.AspNet/AspNetUtilitiesSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Digdir.Library.Utils.AspNet; | ||
|
||
public sealed class AspNetUtilitiesSettings | ||
{ | ||
public HealthCheckSettings HealthCheckSettings { get; set; } = new(); | ||
} | ||
|
||
public sealed class HealthCheckSettings | ||
{ | ||
public List<string> HttpGetEndpointsToCheck { get; set; } = []; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NoWarn>1591</NoWarn> <!-- Disable warnings for missing XML comments --> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> | ||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.