Add Health Checks to a DOTVVM/ASP.NET Core Application and Build a Dashboard to Monitor Services

Vincent Nwonah
DotVVM
Published in
4 min readMay 25, 2020

Introduction

With the shift from Monoliths to Microservices for Web Services Architecture, It has become even more necessary than before to have proper monitoring.

Monitoring amongst many things, tells us; is this application, or service or endpoint healthy enough to receive requests? Developers need to be able to access this information from time to time to ensure application uptime, but perhaps this information is even more useful to orchestrators like Kubernetes, Service Fabric and several others.

Since these orchestrators typically destroy nonresponsive instances/containers/pods and replace them with healthy instances, they should be able to tell when a service becomes unhealthy.

It is the responsibility of services within the cluster to tell the orchestrator what “healthy” means for them. HTTP services like web applications typically provide an endpoint that returns a 200 OK response as long as they’re healthy and any other response tells the orchestrator to replace them.

In this article, we will add health checks to Existing DOTVVM containerized applications and build a DOTVVM web app to display the health of services in the deployment.

Prerequisites

To run the sample given here you must have docker and docker-compose correctly setup on your system.

The starter project for this tutorial is the containerized version of the DOTVVM blazing pizza application. It contains a MySql Database, an ASP.NET Core API and A Dotvvm Web App.

Clone the project from https://github.com/vnwonah/dotvvm-docker-compose and switch to the branch named “finish”. To get an idea of how the application works, navigate to the src folder using your favourite command line and run the command docker-compose up -d. Navigate to localhost:8080 to confirm the application is accessible.

What we will be Building

We will add a second DOTVVM Web Application to the deployment. It simply displays the status of other services in the system.

Adding Health Checks to Individual Services

As mentioned earlier, applications need to have a way of informing other services that they are healthy, or not. For our example, all applications will provide an endpoint /hc which returns a message in the form { “status”: “active” }.

Configuring this endpoint is surprisingly easy with asp.net core’s endpoint middleware.

First Add the nuget Package AspNetCore.HealthChecks.UI.Client to both the Server and App Projects.

dotnet add package AspNetCore.HealthChecks.UI.Client — version 3.0.0

Open Startup.cs for the BlazingPizza.App project and add the registration services.AddHealthChecks().AddCheck(“self”, () => HealthCheckResult.Healthy()); to the Configure Services method.

Add the code snippet

health check configuration

to the Configure Method. This provides an endpoint at http://localhost:8080/hc that provides the health status of the application.

Build the project with docker-compose build and run with docker-compose up -d, navigate to http://localhost:8080/hc and confirm you get the response {“status”:”Healthy”,”totalDuration”:”00:00:00.0054861",”entries”:{“self”:{“data”:{},”duration”:”00:00:00.0004111",”status”:”Healthy”}}}

Repeat the process above for the BlazingPizza.Server application. Because this app cannot be accessed outside the docker network however, we cannot directly test the health endpoint from the browser.

For this, we will add a third application BlazingPizza.Status, to show the status of all services in the deployment.

Adding a Web Application to Display Service Status

Add the nuget packages AspNetCore.HealthChecks.UI.Client and AspNetCore.HealthChecks.UI.

Add the code snippets below to Configure Services in Startup.cs

register health check UI

Also, add the one below to the Configure method

config health check UI

Note: Remove the app.UseStaticFiles registration in the configure method.

The app.UseHealthChecksUI adds a UI for us to see services we are currently monitoring. Run the BlazingPizza.Status App now and navigate to localhost:<port_number>/hc-ui and you should see the dashboard shown above.

To make this the default page, open DefaultPageModel and add the method below

This redirects to the health check page when you navigate to the application root.

Dockerizing the Status App and Adding Other Services to Monitor

For the BlazingPizza.Status App to access the other services it has to be within the same docker network. Add a Dockerfile to src folder named status.dockerfile and add the code below to it

Next, add the status service to the docker-compose.yml file

We need to configure kestrel to serve our app on port 9000 and 9001. For this, open Program.cs and modify the Build WebHost method to include the kestrel configuration as shown below

We can now return to the command line and run the commands

docker-compose build and docker-compose up -d. Navigate to localhost:900 and we can see our dashboard.

Adding Services to be Monitored

So far we have been able to access the dashboard but there are no metrics. Open up the docker-file.yml and modify the status service to look like below

Note the newly added environment part, we configure a name for the service to monitor, and the path to ping.
Deploy the solution with docker-compose up -d and navigate to localhost:9000 and you should see the services listed there.

Conclusion

In this article, we have seen how to configure asp.net core web app for Health Checks and how to build a dashboard to monitor services in Dotvvm.

--

--