aspnet / Diagnostics

[Archived] Diagnostics middleware for reporting info and handling exceptions and errors in ASP.NET Core, and diagnosing Entity Framework Core migrations errors. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
213 stars 111 forks source link

In Preview3 IHealthCheck doesn't seem to be able to return granular HealthStatus #507

Closed alecor191 closed 5 years ago

alecor191 commented 5 years ago

I noticed that in Preview3 IHealthCheck can only return whether it passed or not (through boolean HealthCheckResult.Result property). When registering the health check, I can specify what false means, i.e. whether HealthStatus.Unhealthy or HealthStatus.Degraded should be returned. Essentially I have to pick one of the two states for a given health check implementation and can't have the health check decide what to return.

In Preview2 a IHealthCheck implementation was able to provide more granular state. I.e. it was able to return any of the HealthStatus values.

This limitation in Preview3 is now an issue in a couple of my scenarios. Here an example: I have a health check that looks at the expiration date of the HTTPS certificate used in my ASP.NET Core services.

Is there still a way to achieve this in Preview3?

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @alecor191. @rynowak, can you please handle this? Thanks!

rynowak commented 5 years ago

Hi @alecor191 - this is an intentional change - we want the application developer to be able to configure the status reported by a health check. This is essential if the health check is supplied by a library that you didn't write.

For your use case, I a few options:

You can include these details in the data returns by your health check https://github.com/aspnet/Diagnostics/blob/release/2.2/samples/HealthChecksSample/GCInfoHealthCheck.cs#L60 and then configure a custom writer to include information in the response https://github.com/aspnet/Diagnostics/blob/release/2.2/samples/HealthChecksSample/CustomWriterStartup.cs

Or you could just register two health checks. Write a check that accepts 'days remaining' as a parameter, and register the same code twice, once with 30 days remaining and once with 0 days. Then, configure one of them to report degraded when it fails.

I hope this helps.

alecor191 commented 5 years ago

Thanks @rynowak!

Those two approaches should work fine for me. I just found the previous approach a bit more convenient/simple in my setup. However, I understand that there are other scenarios where a more generic approach is required.