Skip to content

Commit 4ff460d

Browse files
committed
Enable skipping AspNet health checks at actuator endpoint
1 parent c3aec3d commit 4ff460d

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/Common/src/Common/HealthChecks/HealthAggregator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,18 @@ await Parallel.ForEachAsync(contributors, cancellationToken, async (contributor,
6868
private static async Task<IDictionary<string, SteeltoeHealthCheckResult>> AggregateMicrosoftHealthChecksAsync(ICollection<IHealthContributor> contributors,
6969
ICollection<HealthCheckRegistration> healthCheckRegistrations, IServiceProvider serviceProvider, CancellationToken cancellationToken)
7070
{
71-
if (healthCheckRegistrations.Count == 0)
71+
HealthCheckRegistration[] activeHealthCheckRegistrations =
72+
healthCheckRegistrations.Where(registration => !registration.Tags.Contains("SkipFromHealthActuator")).ToArray();
73+
74+
if (activeHealthCheckRegistrations.Length == 0)
7275
{
7376
return new Dictionary<string, SteeltoeHealthCheckResult>();
7477
}
7578

7679
var healthChecks = new ConcurrentDictionary<string, SteeltoeHealthCheckResult>();
7780
var keys = new ConcurrentBag<string>(contributors.Select(contributor => contributor.Id));
7881

79-
// run all HealthCheckRegistration checks in parallel
80-
await Parallel.ForEachAsync(healthCheckRegistrations, cancellationToken, async (registration, _) =>
82+
await Parallel.ForEachAsync(activeHealthCheckRegistrations, cancellationToken, async (registration, _) =>
8183
{
8284
string contributorName = GetKey(keys, registration.Name);
8385
SteeltoeHealthCheckResult healthCheckResult;

src/Management/test/Endpoint.Test/Actuators/Health/HealthAggregationTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,53 @@ public async Task Converts_AspNet_health_check_results()
517517
""");
518518
}
519519

520+
[Fact]
521+
public async Task Can_skip_AspNet_health_check()
522+
{
523+
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
524+
builder.Configuration.AddInMemoryCollection(AppSettings);
525+
builder.Services.AddHealthActuator();
526+
527+
IHealthChecksBuilder checksBuilder = builder.Services.AddHealthChecks();
528+
checksBuilder.AddCheck<AspNetUnhealthyCheck>("aspnet-unhealthy-check", tags: ["SkipFromHealthActuator"]);
529+
checksBuilder.AddCheck<AspNetHealthyCheck>("aspnet-healthy-check");
530+
531+
await using WebApplication host = builder.Build();
532+
533+
host.MapHealthChecks("/health");
534+
await host.StartAsync(TestContext.Current.CancellationToken);
535+
using HttpClient httpClient = host.GetTestClient();
536+
537+
HttpResponseMessage actuatorResponse = await httpClient.GetAsync(new Uri("http://localhost/actuator/health"), TestContext.Current.CancellationToken);
538+
539+
actuatorResponse.StatusCode.Should().Be(HttpStatusCode.OK);
540+
541+
string actuatorResponseBody = await actuatorResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
542+
543+
actuatorResponseBody.Should().BeJson("""
544+
{
545+
"status": "UP",
546+
"components": {
547+
"aspnet-healthy-check": {
548+
"status": "UP",
549+
"description": "healthy-description",
550+
"details": {
551+
"healthy-data-key": "healthy-data-value"
552+
}
553+
}
554+
}
555+
}
556+
""");
557+
558+
HttpResponseMessage aspNetResponse = await httpClient.GetAsync(new Uri("http://localhost/health"), TestContext.Current.CancellationToken);
559+
560+
aspNetResponse.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);
561+
562+
string aspNetResponseBody = await aspNetResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
563+
564+
aspNetResponseBody.Should().Be("Unhealthy");
565+
}
566+
520567
[Fact]
521568
public async Task Can_use_scoped_AspNet_health_check()
522569
{

0 commit comments

Comments
 (0)