Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ app.MapSimpleHealthPage("/health-ui", options =>
});
```

## Filtering which checks are executed

By default the page executes all registered health checks. Use `Predicate` to select a subset, for example by tag. Checks that are filtered out are not executed at all:

```csharp
app.MapSimpleHealthPage("/health-ui", options =>
{
options.Predicate = registration => registration.Tags.Contains("ui");
});
```

This is the same `Func<HealthCheckRegistration, bool>` shape as `HealthCheckOptions.Predicate` used by `MapHealthChecks`.

## Example for dynamic response

If you want a single entry URL, you can dispatch based on the Accept header:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static IEndpointConventionBuilder MapSimpleHealthPage(
var endpoint = endpoints.MapGet(pattern, async context =>
{
var healthCheckService = context.RequestServices.GetRequiredService<HealthCheckService>();
var report = await healthCheckService.CheckHealthAsync(context.RequestAborted);
var report = await healthCheckService.CheckHealthAsync(options.Predicate, context.RequestAborted);

var renderer = new SimpleHealthPageHtmlRenderer
{
Expand Down
8 changes: 8 additions & 0 deletions src/HealthChecks.MiniUI/SimpleHealthPageOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthChecks.MiniUI;

/// <summary>
/// Options for rendering and securing the simple health page endpoint.
/// </summary>
public sealed class SimpleHealthPageOptions
{
/// <summary>
/// Gets or sets a predicate selecting which health check registrations are executed, for example by tag.
/// When <see langword="null"/>, all registered health checks are executed.
/// </summary>
public Func<HealthCheckRegistration, bool>? Predicate { get; set; }

/// <summary>
/// Gets or sets the page title displayed at the top of the health page.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="10.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
Expand Down
89 changes: 89 additions & 0 deletions tests/HealthChecks.MiniUI.Tests/PredicateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;

namespace HealthChecks.MiniUI.Tests;

public sealed class PredicateTests
{
[Fact]
public void Predicate_DefaultsToNull()
{
Assert.Null(new SimpleHealthPageOptions().Predicate);
}

[Fact]
public async Task Predicate_Null_RunsAllHealthChecks()
{
using var host = await CreateHostAsync(configure: null);

var html = await host.GetTestClient().GetStringAsync("/health");

Assert.Contains("ready-check", html, StringComparison.Ordinal);
Assert.Contains("live-check", html, StringComparison.Ordinal);
}

[Fact]
public async Task Predicate_FiltersByTag()
{
using var host = await CreateHostAsync(o => o.Predicate = r => r.Tags.Contains("ready"));

var html = await host.GetTestClient().GetStringAsync("/health");

Assert.Contains("ready-check", html, StringComparison.Ordinal);
Assert.DoesNotContain("live-check", html, StringComparison.Ordinal);
}

[Fact]
public async Task Predicate_ExcludingAllChecks_ReportsHealthy()
{
using var host = await CreateHostAsync(o => o.Predicate = _ => false);

var response = await host.GetTestClient().GetAsync("/health");
var html = await response.Content.ReadAsStringAsync();

Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain("ready-check", html, StringComparison.Ordinal);
Assert.DoesNotContain("live-check", html, StringComparison.Ordinal);
}

[Fact]
public async Task Predicate_FilteringOutUnhealthyCheck_ChangesStatusCode()
{
using var host = await CreateHostAsync(o =>
{
o.Predicate = r => r.Tags.Contains("live");
o.StatusCodes.Unhealthy = StatusCodes.Status503ServiceUnavailable;
});

var response = await host.GetTestClient().GetAsync("/health");

Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}

private static Task<IHost> CreateHostAsync(Action<SimpleHealthPageOptions>? configure)
{
return new HostBuilder()
.ConfigureWebHost(webHost =>
{
webHost.UseTestServer();
webHost.ConfigureServices(services =>
{
services.AddRouting();
services.AddHealthChecks()
.AddCheck("ready-check", () => HealthCheckResult.Unhealthy("not ready"), tags: ["ready"])
.AddCheck("live-check", () => HealthCheckResult.Healthy("alive"), tags: ["live"]);
});
webHost.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapSimpleHealthPage("/health", configure));
});
})
.StartAsync();
}
}