Health Checks
Quick guide to get started with using a health checks default or customized.
Installing Prerequisites
To use the default health checks is necessary a nuget pack MediaiBox.Core.Middleware. In your pipeline add the AddHealthChecks() and UseMibOnAfterAll() to add the defaults health checks. The map of health checks in the mib app are:
MibEditHistoryMicroService
-> Healthcheck in MongoDB
MibConcurrencyMicroService
-> Healthcheck in MibAuthorization
MibPermissionMicroService
-> Healthcheck in SqlServer
-> Healthcheck in MibServer3 (Configurable Permissions)
MibAuthorization
-> Healthcheck in SqlServer
-> Healthcheck in MibPermissionMicroService
MibServerApi
-> Healthcheck in SqlServer
-> Healthcheck in MibPermissionMicroService
-> Healthcheck in MibEditHistoryMicroService
-> Healthcheck in MibAuthorization
MibServer3
-> Healthcheck in SqlServer
-> Healthcheck in MibConcurrencyMicroService (only if service url is configured)
-> Healthcheck in MibPermissionMicroService
-> Healthcheck in MibEditHistoryMicroService
-> Healthcheck in MibServerApi
-> Healthcheck in MibAuthorization
For a simplified call (without checking the services of dependencies) use the config UseSimpleHealthCheck=true.
How to add new health check?
For add new health checks follow the steps below:
- Add in your pipeline application the call
AddHealthChecks(). - After this call add your customize health check. In the pack MediaiBox.Core.Middleware the following health checks have been made available:
AddSqlServerAddMongoDbAddS3AddAzureBlobStorageAddAzureFileShareAddBlobServiceClientAddDiskStorageHealthCheckAddDnsResolveHealthCheckAddDnsResolveHostCountHealthCheckAddFolderAddFtpHealthCheckAddImapHealthCheckAddMongoDbAddMySqlAddNpgSqlAddPingHealthCheckAddPrivateMemoryHealthCheckAddProcessAllocatedMemoryHealthCheckAddProcessHealthCheckAddS3AddSftpHealthCheckAddSmtpHealthCheckAddSqliteAddSslHealthCheckAddTcpHealthCheckAddUrlGroupAddVirtualMemorySizeHealthCheckAddWindowsServiceHealthCheckAddWorkingSetHealthCheck
Documentation on how it works is here.
Example:
// Add services to the container.
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHealthChecks()
.AddSqlServer(SqlServerHelper.GetConnectionString())
.AddMongoDb(MongoClientSettingsHelper.ClientSettings())
.AddS3(S3Helper.Options)
.AddUrlGroup(UriHelper.MibServerApi(), "MibServerApi");
How to add a customize health check?
For add a customize health checks follow the steps below:
- Create a class that implements the
IHealthCheckinterface with the desired logic. - Add in your pipeline application the call
AddHealthChecks(). - After this call add the call
AddCheck<YourCreatedClass>().
Documentation on how it works is here and here.
Example:
public class SampleHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context, CancellationToken cancellationToken = default)
{
var isHealthy = true;
// ...
if (isHealthy)
{
return Task.FromResult(
HealthCheckResult.Healthy("A healthy result."));
}
return Task.FromResult(
new HealthCheckResult(
context.Registration.FailureStatus, "An unhealthy result."));
}
}
builder.Services.AddHealthChecks()
.AddCheck<SampleHealthCheck>("Sample");
How works the call of health check?
When the UseMibOnAfterAll() is add in your app pipeline, one route /health is automatically added. This route can be called in three ways:
- Without parameters. Example:
http://localhost:9999/health. - With the parameter excludedHealthCheck. Example:
http://localhost:9999/health/MibServerApi. This parameter indicates the name of a health check that should be ignored by the called service. For the call to work it is necessary that all health checks are named when registered. - With the option return500onerror. By default, each call returns status code 200 (even with Unhealthy services). If you wanted to return the code 500 when the service is Unhealthy, send on call the option return500onerror=true. Example:
http://localhost:9999/health?return500onerror=trueorhttp://localhost:9999/health/MibServerApi?return500onerror=true
The name of services used by mib by default are:
- MongoDB
- SqlServer
- MibAuthorization
- MibServerApi
- MibServer3
- MibConcurrencyMicroService
- MibPermissionMicroService
- MibEditHistoryMicroService
Example for a basic called to the /health
My app has these settings for calling health checks:
// Add services to the container.
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHealthChecks()
.AddSqlServer(SqlServerHelper.GetConnectionString())
.AddMongoDb(MongoClientSettingsHelper.ClientSettings())
.AddS3(S3Helper.Options)
.AddUrlGroup(UriHelper.MibServerApi(), "MibServerApi");
When i make the call http://localhost:9999/health i get the return:
{
"Status": "Healthy",
"HealthChecks": [
{
"Status": "Healthy",
"Description": "sqlserver"
},
{
"Status": "Healthy",
"Description": "mongodb"
},
{
"Status": "Healthy",
"Description": "aws s3"
},
{
"Status": "Healthy",
"Description": "MibServerApi"
}
],
"HealthCheckDuration": "00:00:09.8620000"
}
Let's analyze the information returned:
Status: Indicates service status based on all calls made. Are they: Healthy, Degraded or Unhealthy.
HealthChecks: Indicates all services your app called for the health checks. The Description is the registered name for the health check.
HealthCheckDuration: Duration in milliseconds of the call execution.
If one or more called are not Healthy the state of the service will also not be. Look:
{
"Status": "Unhealthy",
"HealthChecks": [
{
"Status": "Unhealthy",
"Description": "sqlserver"
},
{
"Status": "Healthy",
"Description": "mongodb"
},
{
"Status": "Healthy",
"Description": "aws s3"
},
{
"Status": "Healthy",
"Description": "MibServerApi"
}
],
"HealthCheckDuration": "00:00:22.0040000"
}
Because the service SqlServer it is Unhealthy, the service status also stay Unhealthy.
Example for a called to the /health with the parameter excludedHealthCheck
My app has these settings for calling health checks:
// Add services to the container.
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHealthChecks()
.AddSqlServer(SqlServerHelper.GetConnectionString())
.AddMongoDb(MongoClientSettingsHelper.ClientSettings())
.AddS3(S3Helper.Options)
.AddUrlGroup(UriHelper.MibServerApi(), "MibServerApi");
Another service called a /health check from my app, but on your call he added the parameter excludedHealthCheck to not perform the health of the SqlServer service. Your call looks like this: http://localhost:9999/health/SqlServer.
Look how the return was:
{
"Status": "Healthy",
"HealthChecks": [
{
"Status": "Healthy",
"Description": "mongodb"
},
{
"Status": "Healthy",
"Description": "aws s3"
},
{
"Status": "Healthy",
"Description": "MibServerApi"
}
],
"HealthCheckDuration": "00:00:02.0780000"
}
The SqlServer health service was not called by the app.
Log for health calls
All calls to /health are logged in Verbose. Look at an example log:
2022-11-29 12:17:02 - [health] - Initialize health check
2022-11-29 12:17:02 - [health] - Exclude health check parameter: sqlSERVER
2022-11-29 12:17:02 - [health] - Return500OnError: false
2022-11-29 12:17:02 - [health] - List of services to check : mongodb, aws s3, MibServerApi
2022-11-29 12:17:02 - [health] - Service: mongodb sucess
2022-11-29 12:17:04 - [health] - Service: aws s3 sucess
2022-11-29 12:17:04 - [health] - Service: MibServerApi sucess
2022-11-29 12:17:04 - [health] - Completed health check