Skip to content

Allow modifying HealthCheckRegistration using IHealthChecksBuilder #26570

@weichch

Description

@weichch

Is your feature request related to a problem? Please describe.

In our project, I'm providing a set of default health checks in a base library. The health checks come with a set of default values set to their HealthCheckRegistration objects that I want the consumer applications/libraries need to be able to override.

HealthCheckRegistration is mutable which fits my purpose, however there is no way out of the box for me to access the HealthCheckRegistrations from health check APIs such as IHealthChecksBuilder.

After checking the source code, I found the registrations are added to HealthCheckServiceOptions which is only accessible via services.Configure<HealthCheckServiceOptions> method. So to alter the registrations I will need to do:

// In my base library
services.AddHealthChecks()
    .AddCheck<TestHealthCheck1>("test1", tags: new[] {"tag"});
    .AddCheck<TestHealthCheck2>("test2", tags: new[] {"tag"});

// In consumer application
services.Configure<HealthCheckServiceOptions>(options =>
{
    var registrations = options.Registrations.FindTargetHealthChecksByTag("tag");
   // Configure all registrations with specified tag
});

This works but not looks ideal as this workaround isn't documented, as well as it defeats the purpose of using IHealthChecksBuilder to manage registrations.

Describe the solution you'd like

I'd like to add some extension methods to IHealthChecksBuilder to allow modifying HealthCheckRegistrations like:

/// <summary>
/// Configures existing health check with the specified name.
/// </summary>
public static IHealthChecksBuilder ConfigureCheck(
    this IHealthChecksBuilder builder,
    string name,
    Action<HealthCheckRegistration> configure)
{
}

 /// <summary>
/// Configures all existing health checks that match the the specified criteria.
/// </summary>
public static IHealthChecksBuilder ConfigureCheck(
    this IHealthChecksBuilder builder,
    Predicate<HealthCheckRegistration> predicate,
    Action<HealthCheckRegistration> configure)
{
}

And the usage looks like:

services.AddHealthChecks().ConfigureCheck(
    registration => registration.Tags.Contains("tag"),
    registration => registration.Timeout = timeout);

Additional context

I already have those extension methods in our code, and extracted them to my own fork weichch#1.

Metadata

Metadata

Assignees

Labels

area-healthchecksIncludes: Healthchecks (some bugs also in Extensions repo)

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions