Implement a watchdog for your microservices using off-the-shelf ASP.Net Healthcheck UI.

Posted by Ghassan Karwchan on Thu, Feb 2, 2023

What is a watchdog

A watchdog is a service that monitor other services and take actions when it detect a service is failing.
ASP.NET starting from Core 5 has a built in UI module to monitor other services, and in few line of code you can build a watchdog for your services.

How to hook it up with your services:

Your microservices should implement health check endpoints to be monitored by the orchestration service (Kubernetes, docker storm, … etc).
I explained in previous post on how to use built-in ASP.NET health check features to achieve build these endpoints.
I recommend reading that article because there I talked about the Microsoft package AspNetCore.Diagnostics.HealthChecks. This package contains all the middleware for build-in health checks, and as well in the same repository has a UI module that with few line of code build a UI to monitor all integrated services and monitor their health check endpoints.

How to use the UI module to build the monitoring service.

  1. Create a MVC application using

    1    dotnet new mvc -n watchdog
  2. Add required packages:

    1   dotnet add package AspNetCore.HealthChecks.UI
  3. define in appsettings.json all the services that you want to monitor. the format of the config section is as follows:

     1    // Configuration
     2   {
     3     "HealthChecksUI": {
     4        "HealthChecks": [
     5           {
     6              "Name": "Ordering HTTP Check",
     7              "Uri": "http://host.docker.internal:5102/hc"
     8           },
     9          {
    10            "Name": "Ordering HTTP Background Check",
    11            "Uri": "http://host.docker.internal:5111/hc"
    12          },
    13         //...
    14       ]}
    15      }

    In reality when we deploy the application to AKS or any service-mesh service, we need to replace the url address. But now we are just describing the idea of the UI module.

  4. add HealthCheckUI service

2            .AddInMemoryStorage();
4// ....
5var app = builder.Build();
6// ....
8// the URL /hc-ui will display the user interface
9app.UseHealthChecksUI(config => config.UIPath = "/hc-ui");

When you run the application you will see the user interface that looks like

Image description

I am going to add more on monitoring services using other built-in features, so keep following me.