Skip to content

Introduce per-HealthCheck Delay and Period #42645

@francedot

Description

@francedot

In the current implementation, delay, periodicity of the healthchecks are set at the HealthCheckPublisherOptions configuration level and shared among all the HealthCheckRegistration:

builder.Services.Configure<HealthCheckPublisherOptions>(options =>
{
    options.Delay = TimeSpan.FromSeconds(10);
    options.Period = TimeSpan.FromSeconds(2);
});

API proposal is to introduce per-HealthCheck parameters to the HealthCheckRegistration used by IHealthChecksBuilder.Add(HealthCheckRegistration registration). Here is an example of how the new flavor of HealthChecks would be used:

builder.Services.AddHealthChecks()
   .Add(new HealthCheckRegistration(
               name: "SampleHealthCheck1",
               instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))),
               failureStatus: null,
               tags: null,
               timeout: default,
               delay: TimeSpan.FromSeconds(40),
               period: TimeSpan.FromSeconds(30)));

Proposed API

HealthCheckRegistration.cs

namespace Microsoft.Extensions.Diagnostics.HealthChecks;

public sealed class HealthCheckRegistration
{
     public HealthCheckRegistration(string name, IHealthCheck instance, HealthStatus? failureStatus, IEnumerable<string>? tags, TimeSpan? timeout);
+    public HealthCheckRegistration(string name, IHealthCheck instance, HealthStatus? failureStatus, IEnumerable<string>? tags, TimeSpan? timeout, TimeSpan? delay);
+    public HealthCheckRegistration(string name, IHealthCheck instance, HealthStatus? failureStatus, IEnumerable<string>? tags, TimeSpan? timeout, TimeSpan? delay, TimeSpan? period);
     public HealthCheckRegistration(string name, Func<IServiceProvider, IHealthCheck> factory, HealthStatus? failureStatus, IEnumerable<string>? tags, TimeSpan? timeout);
+    public HealthCheckRegistration(string name, Func<IServiceProvider, IHealthCheck> factory, HealthStatus? failureStatus, IEnumerable<string>? tags, TimeSpan? timeout, TimeSpan? delay);
+    public HealthCheckRegistration(string name, Func<IServiceProvider, IHealthCheck> factory, HealthStatus? failureStatus, IEnumerable<string>? tags, TimeSpan? timeout, TimeSpan? delay, TimeSpan? period);

+    public TimeSpan? Delay { get; }
+    public TimeSpan? Period { get; }
}

Usage Examples

builder.Services.AddHealthChecks()
   .Add(new HealthCheckRegistration(
               name: "SampleHealthCheck1",
               instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))),
               failureStatus: null,
               tags: null,
               timeout: default,
               delay: TimeSpan.FromSeconds(40),
               period: TimeSpan.FromSeconds(30)));

Alternative Designs

It might have been possible to implement HC periodicity by reusing the HC Registration Predicate delegate to condition the execution of the check instance i.e. <Timer_Run> * MULTIPLIER < HC_Periodicity. Although this would only to create HCs with periodicity set to a multiple of the default one.

Risks

Performance regressions due to multiple timers introduction in HealthCheckPublisherHostedService.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-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