Skip to content

Provide AddChecks method in IHealthCheckBuilder #32317

Open
@DurgaPrasadReddyV

Description

@DurgaPrasadReddyV

Background and Motivation

Currently IHealthCheckBuilder provides a "AddCheck" method to register a custom health check in my application.

services.AddHealthChecks().AddCheck<CustomHealthCheck>("CustomHealthCheck");

And inline is implementation for CheckHealthAsync method in my CustomHealthCheck class.

public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
        //hardcoding directly the result for explanation purpose
        return Task.FromResult(new HealthCheckResult(HealthStatus.Healthy));
}

We can observe that I am able to return only one HealthCheckResult object from my CustomHealthCheck class.
I am looking for an api from which I can return multiple health check results from a single custom health check class.
Into this custom health check class I would like to add and remove health checks from an external application while the application is running so that there would be no need to modify code and redeploy the application.

Proposed API

services.AddHealthChecks().AddChecks<CustomHealthChecks>("CustomHealthChecks");

And inline is implementation for CheckHealthAsync method in my CustomHealthChecks class.
The interface would be IHealthChecks instead of IHealthCheck which returns a list of HealthCheckResults.

public Task<List<HealthCheckResult>> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            //hardcoding result for explanation  
            return Task.FromResult(new List<HealthCheckResult>(){new HealthCheckResult(
                HealthStatus.Healthy)});
        }

Usage Examples

services.AddHealthChecks().AddChecks<CustomHealthChecks>("CustomHealthChecks");

With this registration in my ConfigureServices method I can add 0 to n healthchecks in my CustomHealthChecks class during runtime and return their status by populating the list.

Alternative Designs

Currently I am populating the IReadOnlyDictionary<string, object> Data of HealthCheckResult class to meet the requirement of different health checks status added dynamically during runtime.

Risks

No risk is involved because this is new api.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-healthchecksIncludes: Healthchecks (some bugs also in Extensions repo)enhancementThis issue represents an ask for new feature or an enhancement to an existing one

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions