简体   繁体   中英

.NET Core health check of external services

I am using .NET Core 3.1 to develop a web application. It depends on several other external services (APIs written in Node.js). I would like to monitor the health of external services and use HealthChecks.UI to show the health on a separate page. I am not interested in health of my own application, I am interested about the health of the dependent external systems. Can this be achieved by health checks package ?

This is the code that I have currently:

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddHealthChecks()
            .AddCheck<ExternalServiceHealthCheck>("External service health check");
        
        services
            .AddHealthChecksUI()
            .AddInMemoryStorage();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecksUI(setup =>
            {
                setup.UIPath = "/HealthChecks";
            });
        });
    }
}

ExternalServiceHealthCheck.cs

public class ExternalServiceHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(HealthCheckResult.Healthy("External service is healthy"));
    }
}

When I go to /HealthChecks , I get an empty UI even though I registered ExternalServiceHealthCheck (see image). Why is it not showing on /HealthChecks page?

在此处输入图片说明

It looks like the UI cannot see the healthchecks. By default, unless you have configured it otherwise, the UI looks for /healthchecks-api to get the data for what healthchecks to render.

Also the /healthchecks-api needs to install the AspNetCore.HealthChecks.UI.Client package and set that packages UIResponseWriter as the ResponseWriter property of the options:

app.MapHealthChecks("/healthchecks-api", new HealthCheckOptions
{
    Predicate = _ => true,
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

You should be able to view /healthchecks-api and see some JSON coming back, rather than the basic "healthy"/"unhealthy" message. This is all from the readme for the HealthChecks UI package.

I fully reproduced your problem locally, when I added in the configuration file

"HealthChecks-UI": {
     "HealthChecks": [
       {
         "Name": "Ordering HTTP Check",
         "Uri": "http://localhost:5102/hc"
       },
       {
         "Name": "Ordering HTTP Background Check",
         "Uri": "http://localhost:5111/hc"
       }
     ]
   }

It can display correctly.

If you want to use healthchecks-ui , you must configure the Endpoint through the appsettings.json or in the Startup , otherwise it cannot be displayed on the UI. In your case, as long as the API Endpoint returns http status code 200, it is the health status. So you should provide APIs specifically for Health Check in other projects. If everything is normal, they only need to return http status code 200.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM