Asp.Net Core provides Health Check libraries and middleware to developers with which they are able to report the health of app infrastructure components.
Health checks are revealed by an app as HTTP endpoints. Developers can configure health check endpoints for distinct real-time monitoring scenarios-
- Health probes can be used by load balances and container orchestrators to check the status of an app.
- Use of memory, disc, and other hardware resources of server can be monitored for healthy status.
- App dependencies can be tested by health checks, such as databases, external service endpoints to ensure normal functioning and availability.
Developers generally use Health Checks with an external monitoring service or container orchestrator to detect the status of an app. Prior adding health checks to an app, developers decide the type of monitoring system for use. The system function is to tell the type of health checks needed and how to configure their endpoints.
The sample app offers startup code to demo health checks for different scenarios. The database probe scenario checks the database connection health with the help of aspnetcore.diagnostics.healthchecks. The DbContext probe scenario checks the desired database with an EF Core DBcontext. The sample app creates a database and offers its connection string that developers can find in the appsettings.json file. It also has the following package references inside the project file-
Second health check scenario guides developers about filtering health checks to a management port. The sample app used in the instance needs developers to create a Properties/launchSettings.Json file that has the management URL and port.
Basic Health Probe
For discovering the status of the app, a basic health probe configuration that reports the availability of the app to process requests is enough.
The basic configuration records health check services and drives the Health Check Middleware to reply at a URL endpoint in a healthy way. The app can be considered as healthy when it is responding at the health endpoint URL.
What are health checks?
Health Checks are a way of checking to see if the app is healthy. These are become critical as more and more apps are shifting to a Microservice-style architecture. While Microservice architectures offer numerous advantages, they still have a con sides, such as- there is a higher operations overhead to check all of these services are running. Developers have to monitor the status of different services instead of monitoring the health of single majestic monolith. Different services are usually having single responsibility.
Heath Checks are applied to app along with a service discovery tool like Consul, which monitor the Microservice and keep their health in check. Consul is designed to automatically route traffic away from the unhealthy Microservice and only serve traffic to the healthy Microservice. This makes everything interesting for developers!
How to implement a health check?
Developers may find a few different ways to do health checks, but the commonly used method is exposing an HTTP endpoint to their app designed to doing health checks. Developers will return a status code of 200 if everything is in place, and any non-2xx code that means something went wrong. For instance, developers might return a 500 if something went wrong along with a JSON payload explaining what went wrong.
What does Microsoft.AspnetCore.HealthChecks package offering?
Microsoft is planning to provide a set of Health Check Packages to assist developers in solving this issue in a consistent way. If developers look inside the HitHub repo, they will see a package for asp.net development 4.x under Microsoft.Aspnet.healthchecks namespace. They can find a samples folder on the GitHub repo that includes ways to wire that up as per their interest in asp.net 4.x.