-
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.
- Loading branch information
1 parent
49a3b89
commit 89accc8
Showing
6 changed files
with
87 additions
and
100 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
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; } = []; | ||
} |
This file was deleted.
Oops, something went wrong.
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