Skip to content

Commit

Permalink
[FEATURE] Add Health Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmanuelthiel committed Nov 27, 2023
1 parent de474d2 commit 133cf0b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public Startup(IConfiguration configuration)

_options = new StartupOptions();

// Middleware
_options
.AddMiddleware<ApiExceptionFilter>();

// Add Swagger
_options
.AddOpenApi("v1")
Expand All @@ -29,7 +33,8 @@ public Startup(IConfiguration configuration)

// Add Monitoring
_options
.AddMonitoring(Configuration["ServiceName"], Configuration["ServiceVersion"])
.AddMonitoring()
.WithMeter(Observability.Meter.Name)
.WithApplicationInsights(Configuration["AzureApplicationInsightsConnectionString"])
.WithPrometheus();
}
Expand Down Expand Up @@ -102,7 +107,7 @@ Make sure to register the Meter at Startup.
```csharp
var options = new StartupOptions();
options
.AddMonitoring(Configuration["ServiceName"], Configuration["ServiceVersion"])
.AddMonitoring()
.WithMeter(Observability.Meter.Name)
// ...
```
Expand All @@ -114,3 +119,31 @@ Observability.Pings.Add(1);

Observability.PingDelay.Record(new Random().Next(50, 100));
```

## Health Checks

The library automatically includes a health check endpoint at `/healthz`, which checks the basic health of the service.

You can add additional health checks to the default setup.

- Inline Health Checks
- Custom Health Checks, that implement the IHealthCheck interface
- [Database Health Checks](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-6.0#database-probe)
- [Entity Framework Core DbContext probes](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-6.0#entity-framework-core-dbcontext-probe)

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddDefaultSetup(_options);

// Health Checks
services
.AddHealthChecks()
.AddCheck("MyHealthCheck", () => HealthCheckResult.Healthy("Everything is fine.")
.AddCheck("MyOtherHealthCheck", MyHealthChecker) // Implement IHealthCheck
.AddSqlServer("<MY_CONNECTION_STRING>")
.AddDbContextCheck<SampleDbContext>());

// ...
}
```
6 changes: 4 additions & 2 deletions src/Wemogy.AspNet/Startup/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public static void AddDefaultSetup(this IServiceCollection serviceCollection, St

serviceCollection.AddDefaultControllers(options.DaprEnvironment != null, options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes);

serviceCollection.AddHealthChecks();

serviceCollection.AddDefaultRouting();
}

Expand Down Expand Up @@ -120,9 +122,9 @@ public static void UseDefaultSetup(this IApplicationBuilder applicationBuilder,
{
applicationBuilder.UseEndpoints(endpoints =>
{
// Register endpoints for Dapr PubSub subscription information
endpoints.MapSubscribeHandler();
endpoints.MapSubscribeHandler(); // Register endpoints for Dapr PubSub
endpoints.MapControllers();
endpoints.MapHealthChecks("/healthz");
});
}
else
Expand Down
1 change: 1 addition & 0 deletions src/Wemogy.AspNet/Startup/StartupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class StartupOptions
internal MonitoringEnvironment? MonitoringEnvironment { get; private set; }
internal DaprEnvironment? DaprEnvironment { get; private set; }
internal HashSet<Type> Middlewares { get; private set; }
internal HashSet<Type> HealthChecks { get; private set; }

/// <summary>
/// Sets the <see cref="Microsoft.AspNetCore.Mvc.MvcOptions.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes"/> property for all contollers.
Expand Down

0 comments on commit 133cf0b

Please sign in to comment.