AspNetCore.Diagnostics.HealthChecks icon indicating copy to clipboard operation
AspNetCore.Diagnostics.HealthChecks copied to clipboard

IHealthCheckPublisher and SetEvaluationTimeInSeconds

Open aleksvujic opened this issue 1 year ago • 0 comments

We are using .NET 7, AspNetCore.HealthChecks.UI 7.0.2 and AspNetCore.HealthChecks.UI.Client 7.1.0. We have the following code in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddHealthChecks()
        .AddCheck<NotificationsServiceHealthCheck>("Notifications service health check", tags: new[] { "external-service" });

    var evaluationTimeInSeconds = 60;
    services.Configure<HealthCheckPublisherOptions>(options =>
    {
        options.Period = TimeSpan.FromSeconds(evaluationTimeInSeconds);
    });

    services.AddSingleton<IHealthCheckPublisher, CustomHealthCheckPublisher>();

    services
        .AddHealthChecksUI(setup =>
        {
            setup.AddHealthCheckEndpoint("External services health checks", "https://localhost:44366/health-external-services");
            setup.SetEvaluationTimeInSeconds(evaluationTimeInSeconds);
        });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHealthChecks("/health-external-services", new HealthCheckOptions()
        {
            Predicate = r => r.Tags.Contains("external-service"),
            ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
        });
        
        endpoints.MapHealthChecksUI(setup =>
        {
            setup.UIPath = "/HealthChecks";
            setup.AsideMenuOpened = false;
        });
    });
}

We use a custom implementation of IHealthCheckPublisher - CustomHealthCheckPublisher. We noticed that before IHealthCheckPublisher.PublishAsync method is triggered, all health checks are re-executed.

If both HealthCheckPublisherOptions.Period and SetEvaluationTimeInSeconds re-run health checks, why have both? If each invocation of IHealthCheckPublisher.PublishAsync re-runs all health checks by specified time interval (HealthCheckPublisherOptions.Period), we don't need to do the "same" with SetEvaluationTimeInSeconds. Can we disable SetEvaluationTimeInSeconds somehow and leave health check execution just to IHealthCheckPublisher? Moreover, if health checks are invoked in the context of IHealthCheckPublisher, their results aren't persisted. Is this expected?

aleksvujic avatar Nov 28 '23 12:11 aleksvujic