Xabaril / AspNetCore.Diagnostics.HealthChecks

Enterprise HealthChecks for ASP.NET Core Diagnostics Package
Apache License 2.0
4.04k stars 789 forks source link

Could not retrieve health checks data but network tab shows successful retrieval #494

Closed remy90 closed 4 years ago

remy90 commented 4 years ago

Please, fill the following sections to help us fix the issue

What happened: HealthCheckUI page displays: "Could not retrieve health checks data"

What you expected to happen: Indication in healthchecksui of my apis and db in the UI. "/hc" does return the expected JSON, "/hc-ui#/healthchecks" also retrieves "/hc" JSON data successfully, as determined by monitoring the network tab.

How to reproduce it (as minimally and precisely as possible):

I added the following to my Startup.cs:

ConfigureServices(...) { services.AddHealthChecksUI(); }
Configure(...) 
{ 
    app.UseHealthChecks("/hc", new HealthCheckOptions()
    {
        Predicate = _ => true,
        ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
    });
    app.UseHealthChecksUI(config =>
    {
        config.ApiPath = "/hc";
        config.UIPath = "/hc-ui";
    });
}

appSettings:

"HealthChecks-UI": {
      "HealthChecks": [
        {
          "Name": "Ordering HTTP Check",
          "Uri": "https://localhost:44328/hc"
        }
      ]
    }

Following this tutorial: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/monitor-app-health

Anything else we need to know?: Screenshot 2020-04-21 12 16 51

Environment:

CarlosLanderas commented 4 years ago

Hello @remy90 , you are setting the same path for the healthchecks endpoint and the ui-api path. Just remove this line, or set a different path

  config.ApiPath = "/hc";

If you see that tutorial, they establish :

// Startup.cs from WebStatus(Watch Dog) service
//
public void ConfigureServices(IServiceCollection services)
{
    //…
    // Registers required services for health checks
    services.AddHealthChecksUI();
}
//…
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
```csharp
{
    //…
    app.UseHealthChecksUI(config=> config.UIPath = "/hc-ui");
    //…
}

That is the path where the ui will be server by the browser

remy90 commented 4 years ago

My first attempts were without ApiPath assignment. I don't think it's hitting /hc at all. It's hitting "/healthchecks-api" but it's empty: "[]"

CarlosLanderas commented 4 years ago

@remy90 can you publish the whole sample so we can help you? Hitting healthchecks-api is fine!. The UI does not call the healthchecks directly, it has a background collector performing this tasks. I see you are using an https url in the appsettings. Is your app running in https port?

You can also take a look to the samples:

https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/tree/master/samples

remy90 commented 4 years ago
public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry();
    var migrationsAssembly = typeof(MyDbContextFactory).GetTypeInfo().Assembly.GetName().Name;
    var config = new Config();

    Configuration.GetSection("MyConfig").Bind(config);

    var connectionString = myConfig.ConnectionString;

    services.AddDbContext<MyDbContext>(ConfigureDbContext());

    services.AddAuthentication().AddAzureAd().AddCookie();
    services.AddAuthorization();
    services.AddRazorPages();
    services
        .AddHealthChecks()
        .AddCheck(
                "myDB-check",
                new SqlConnectionHealthCheck(connectionString),
                HealthStatus.Unhealthy,
                new[] { "mydb" })
        .AddSqlServer(connectionString);
    services.AddHealthChecksUI();
    services.AddControllers()
    services.ConfigureServices(Configuration);
}

public void Configure(IApplicationBuilder app)
{
    app.UseBrowserLink();
    app.UseDeveloperExceptionPage();
    IdentityModelEventSource.ShowPII = true;

    app.UseStaticFiles();

    app.UseRouting();

    app.UseCors();

    app.UseAuthentication();

    app.UseAuthorization();

    app.UseHealthChecks("/hc", new HealthCheckOptions()
    {
        Predicate = _ => true,
        ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
    });

    app.UseHealthChecksUI(config =>
    {
        config.UIPath = "/hc-ui";
    });
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints
                .MapRazorPages()
                .RequireAuthorization();
        endpoints.MapHealthChecks("/hc");
        endpoints.MapHealthChecksUI();
    });
}
CarlosLanderas commented 4 years ago

You are registering the healthchecks middleware twice, one with the old version and one with the new version.

You dont have to use:

app.UseHealthChecksUI(config =>
    {
        config.UIPath = "/hc-ui";
    });

and

  endpoints.MapHealthChecksUI();

If you are in 3.0 you must only use the endpoints middleware registration

Please, read the docs and check the samples

Thanks

remy90 commented 4 years ago

Only made the upgrade to 3.1 in the past few days. My apologies, however that still doesn't seem to have done the trick. I've removed:

app.UseHealthChecksUI(config =>
    {
        config.UIPath = "/hc-ui";
    });

and applied:

endpoints.MapHealthChecksUI(options => 
{
    options.UIPath = "/hc-ui";
});

still receiving the same results as before

CarlosLanderas commented 4 years ago

You have a problem here:

  endpoints.MapHealthChecks("/hc");

You are not configuring the UIResponseWriter that is necessary to export healthchecks to a UI readable format, check it here:

https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/master/samples/HealthChecks.UI.Branding/Startup.cs#L80

I recommend you to read the samples as you have different flavours.

remy90 commented 4 years ago

Thanks for your assistance. I clearly was using a mixture of flavours. The original article I referenced suggests it's using 3.1 based on the package references. For posterity, the following resolved my issue:

services.AddHealthChecksUI("healthchecksdb", setup =>
    setup.AddHealthCheckEndpoint("endpoint2", "health-process");