Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Refactor probes and add more health checks #1159

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1317e97
sup
oskogstad Sep 18, 2024
51c3515
low cse
oskogstad Sep 18, 2024
b25034e
chore: mo health checks pls
arealmaas Sep 20, 2024
42326d1
chore: even mo health checks pls
arealmaas Sep 20, 2024
a275c12
Merge branch 'main' into chore/add-redis-postgres-health-checks
arealmaas Sep 20, 2024
ddf0ce7
cleanup
arealmaas Sep 20, 2024
d58b8e4
Update src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/Wel…
arealmaas Sep 20, 2024
1cafcdf
Update src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/Wel…
arealmaas Sep 20, 2024
3996060
cleanup
arealmaas Sep 20, 2024
8889b77
cleanup
arealmaas Sep 20, 2024
02c13b0
cleanup
arealmaas Sep 23, 2024
edb9f93
Merge branch 'main' into chore/add-redis-postgres-health-checks
oskogstad Oct 1, 2024
e9322f1
Update src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/Wel…
oskogstad Oct 1, 2024
383f56a
chore: adjust health checks
arealmaas Oct 1, 2024
62bd903
chore: adjust health checks
arealmaas Oct 1, 2024
1129ce2
cleanup
arealmaas Oct 1, 2024
7ffc934
Merge branch 'main' into chore/add-redis-postgres-health-checks
arealmaas Oct 1, 2024
33e7c4e
chore(deps): update dependency htmlagilitypack to 1.11.66 (#1212)
renovate[bot] Oct 2, 2024
08e94cb
ci: ensure unique revisions for deployments (#1211)
arealmaas Oct 2, 2024
f8fe468
chore(main): release 1.20.1 (#1207)
dialogporten-bot Oct 2, 2024
ea2400b
ci: send message on release created and successfully validated (#1185)
arealmaas Oct 2, 2024
b755f0c
beginning
arealmaas Oct 2, 2024
84f0941
refactor
arealmaas Oct 3, 2024
4b8fdb6
refactor
arealmaas Oct 3, 2024
59ef954
ok
arealmaas Oct 3, 2024
2ca9fae
ok
arealmaas Oct 3, 2024
d7e3b3f
ok
arealmaas Oct 3, 2024
e41bd02
swaggah
arealmaas Oct 3, 2024
fcf6727
cleanup
arealmaas Oct 3, 2024
34ce8a8
cleanup
arealmaas Oct 3, 2024
62ced3e
cleanup
arealmaas Oct 3, 2024
3abf115
Update src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/Red…
arealmaas Oct 3, 2024
3173da9
cleanup
arealmaas Oct 3, 2024
2f267c8
cleanup
arealmaas Oct 3, 2024
48bab84
cleanup
arealmaas Oct 3, 2024
1a4dddb
cleanup
arealmaas Oct 3, 2024
296c01b
Update src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/Red…
arealmaas Oct 3, 2024
4038e3b
cleanup
arealmaas Oct 3, 2024
f5eea3a
cleanup
arealmaas Oct 3, 2024
1210864
cleanup
arealmaas Oct 3, 2024
4ba55db
Update src/Digdir.Library.Utils.AspNet/HealthChecks/EndpointsHealthCh…
arealmaas Oct 3, 2024
f1b0186
Update src/Digdir.Domain.Dialogporten.Infrastructure/HealthChecks/Red…
arealmaas Oct 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
<PackageReference Include="Altinn.ApiClients.Maskinporten" Version="9.1.0"/>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="HotChocolate.Subscriptions.Redis" Version="13.9.12" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.8" />
<PackageReference Include="Altinn.Authorization.ABAC" Version="0.0.8"/>
<PackageReference Include="Bogus" Version="35.6.1"/>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.8" />
<PackageReference Include="Npgsql" Version="8.0.4"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Net.Http;
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Digdir.Domain.Dialogporten.Infrastructure.HealthChecks;

public class WellKnownEndpointsHealthCheck : IHealthCheck
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<WellKnownEndpointsHealthCheck> _logger;
private readonly IConfiguration _configuration;

public WellKnownEndpointsHealthCheck(
IHttpClientFactory httpClientFactory,
ILogger<WellKnownEndpointsHealthCheck> logger,
IConfiguration configuration)
{
_httpClientFactory = httpClientFactory;
_logger = logger;
_configuration = configuration;
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
}

public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
var wellKnownEndpoints = _configuration.GetSection("WebApi:Authentication:JwtBearerTokenSchemas")
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
.GetChildren()
.Select(section => section.GetValue<string>("WellKnown"))
.ToList();
arealmaas marked this conversation as resolved.
Show resolved Hide resolved

var client = _httpClientFactory.CreateClient("HealthCheckClient");
arealmaas marked this conversation as resolved.
Show resolved Hide resolved

foreach (var url in wellKnownEndpoints)
{
try
{
var response = await client.GetAsync(url, cancellationToken);
if (!response.IsSuccessStatusCode)
{
_logger.LogWarning("Health check failed for Well-Known endpoint: {Url}", url);
return HealthCheckResult.Unhealthy($"Well-Known endpoint {url} is unhealthy.");
}
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception occurred while checking Well-Known endpoint: {Url}", url);
return HealthCheckResult.Unhealthy($"Exception occurred while checking Well-Known endpoint {url}.");
}
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
}
arealmaas marked this conversation as resolved.
Show resolved Hide resolved

return HealthCheckResult.Healthy("All Well-Known endpoints are healthy.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
using Digdir.Domain.Dialogporten.Infrastructure.Persistence.Configurations.Actors;
using Digdir.Domain.Dialogporten.Infrastructure.Persistence.Repositories;
using HotChocolate.Subscriptions;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using StackExchange.Redis;
using ZiggyCreatures.Caching.Fusion;
using ZiggyCreatures.Caching.Fusion.NullObjects;
using Digdir.Domain.Dialogporten.Infrastructure.HealthChecks;

namespace Digdir.Domain.Dialogporten.Infrastructure;

Expand Down Expand Up @@ -182,6 +184,27 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
})
.AddPolicyHandlerFromRegistry(PollyPolicy.DefaultHttpRetryPolicy);

services.AddHttpClient("HealthCheckClient")
.SetHandlerLifetime(TimeSpan.FromSeconds(10));

services.AddHealthChecks()
.AddRedis(
redisConnectionString: infrastructureSettings.Redis.ConnectionString,
name: "redis",
failureStatus: HealthStatus.Unhealthy,
tags: ["dependencies"])
.AddNpgSql(
connectionString: infrastructureSettings.DialogDbConnectionString,
name: "postgres",
healthQuery: "SELECT 1",
failureStatus: HealthStatus.Unhealthy,
tags: ["dependencies"])
.AddCheck<WellKnownEndpointsHealthCheck>(
"Well-Known Endpoints",
failureStatus: HealthStatus.Unhealthy,
tags: ["dependencies", "auth"]);


if (environment.IsDevelopment())
{
var localDeveloperSettings = configuration.GetLocalDevelopmentSettings();
Expand Down
33 changes: 28 additions & 5 deletions src/Digdir.Domain.Dialogporten.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
using FastEndpoints;
using FastEndpoints.Swagger;
using FluentValidation;
using HealthChecks.UI.Client;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using NSwag;
using Serilog;

Expand Down Expand Up @@ -123,14 +126,14 @@ static void BuildAndRun(string[] args)
};
})
.AddControllers(options => options.InputFormatters.Insert(0, JsonPatchInputFormatter.Get()))
.AddNewtonsoftJson()
.Services

.AddNewtonsoftJson()
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
.Services
arealmaas marked this conversation as resolved.
Show resolved Hide resolved

// Auth
.AddDialogportenAuthentication(builder.Configuration)
.AddAuthorization()
.AddHealthChecks();
.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["self"]);

if (builder.Environment.IsDevelopment())
{
Expand Down Expand Up @@ -200,7 +203,27 @@ static void BuildAndRun(string[] args)
uiConfig.DocumentPath = dialogPrefix + "/swagger/{documentName}/swagger.json";
});
app.MapControllers();
app.MapHealthChecks("/healthz");
app.MapHealthChecks("/startup", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("dependencies"),
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
app.MapHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("self"), // Retains the self check for liveness
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.MapHealthChecks("/readiness", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("dependencies"),
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.MapHealthChecks("/health", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("dependencies"),
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

app.Run();
}

Expand Down
Loading