Skip to content

Commit

Permalink
Set up basic and detailed health checks at /api/v3/health/ready and /…
Browse files Browse the repository at this point in the history
…api/v3/health/live (#715)

* Set up basic and detailed health checks at /api/v3/health/ready and /api/v3/health/live
Enable anonymous access to these endpoints to allow health monitoring by external systems without authentication
Utilize default ASP.NET Core Health Checks behavior to return HTTP 200 for Healthy and HTTP 503 for Unhealthy or Degraded states, facilitating straightforward integration with load balancers and monitoring tools

* Linting
  • Loading branch information
adrianwium authored Apr 12, 2024
1 parent 6be5316 commit cf53ef6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions helm/yoma-api/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ spec:
{{- if .Values.livenessProbe.enabled }}
livenessProbe:
httpGet:
path: /index.html
path: /api/v3/health/live
port: {{ .Values.service.portName | default "http" }}
{{- end }}
{{- if .Values.readinessProbe.enabled }}
readinessProbe:
httpGet:
path: /index.html
path: /api/v3/health/ready
port: {{ .Values.service.portName | default "http" }}
{{- end }}
resources:
Expand Down
28 changes: 19 additions & 9 deletions src/api/src/application/Yoma.Core.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using StackExchange.Redis;
Expand Down Expand Up @@ -81,6 +83,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddHttpContextAccessor();
services.AddMemoryCache();
services.AddHealthChecks().AddCheck("API Ready Check", () => HealthCheckResult.Healthy("API is up"), tags: ["ready"]);
#endregion

#region 3rd Party
Expand Down Expand Up @@ -128,24 +131,31 @@ public void Configure(IApplicationBuilder app)
if (_appSettings.HttpsRedirectionEnabledEnvironmentsAsEnum.HasFlag(_environment)) app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
// basic check to ensure the API is up
endpoints.MapHealthChecks("/api/v3/health/ready", new HealthCheckOptions
{
Predicate = (check) => check.Tags.Contains("ready")
}).AllowAnonymous();

// more detailed check to ensure the API can connect to the database
endpoints.MapHealthChecks("/api/v3/health/live", new HealthCheckOptions
{
Predicate = (check) => check.Tags.Contains("live")
}).AllowAnonymous();
});

//enabling sentry tracing causes endless information logs about 'Sentry trace header is null'
//if (_environment != Domain.Core.Environment.Local) app.UseSentryTracing();

app.UseAuthentication();
app.UseAuthorization();
#endregion

#region 3rd Party
app.UseHangfireDashboard(options: new DashboardOptions
{
DarkModeEnabled = true,
/*
TODO: Resolve shared Data Protection Keys (NFS or S3 or SSM or Postgres) and set this back to `false`
* https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers
* https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview
* https://github.com/aws/aws-ssm-data-protection-provider-for-aspnet
*/
IgnoreAntiforgeryToken = true, //replicas >=2 will cause antiforgery token issues
Authorization = [new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ public static void ConfigureServices_InfrastructureDatabase(this IServiceCollect
maxRetryDelay: TimeSpan.FromSeconds(appSettings.DatabaseRetryPolicy.MaxRetryDelayInSeconds),
errorCodesToAdd: null);
})
.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.MultipleCollectionIncludeWarning)); //didable warning related to not using AsSplitQuery() as per MS SQL implementation
//.UseLazyLoadingProxies(): without arguments is used to enable lazy loading. Simply not calling UseLazyLoadingProxies() ensure lazy loading is not enabled
//disable warning related to not using AsSplitQuery() as per MS SQL implementation
//.UseLazyLoadingProxies(): without arguments is used to enable lazy loading. Simply not calling UseLazyLoadingProxies() ensure lazy loading is not enabled
.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.MultipleCollectionIncludeWarning));

}, ServiceLifetime.Scoped, ServiceLifetime.Scoped);

services.AddHealthChecks().AddNpgSql(
connectionString: configuration.Configuration_ConnectionString(),
name: "Database Connectivity Check",
tags: ["live"]);

//<PackageReference Include="EntityFrameworkProfiler.Appender" Version="6.0.6040" />
//if (environment == Domain.Core.Environment.Local)
// HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit cf53ef6

Please sign in to comment.