Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions src/Common/src/Common/HealthChecks/HealthAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,18 @@ await Parallel.ForEachAsync(contributors, cancellationToken, async (contributor,
private static async Task<IDictionary<string, SteeltoeHealthCheckResult>> AggregateMicrosoftHealthChecksAsync(ICollection<IHealthContributor> contributors,
ICollection<HealthCheckRegistration> healthCheckRegistrations, IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
if (healthCheckRegistrations.Count == 0)
HealthCheckRegistration[] activeHealthCheckRegistrations =
healthCheckRegistrations.Where(registration => !registration.Tags.Contains("SkipFromHealthActuator")).ToArray();

if (activeHealthCheckRegistrations.Length == 0)
{
return new Dictionary<string, SteeltoeHealthCheckResult>();
}

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

// run all HealthCheckRegistration checks in parallel
await Parallel.ForEachAsync(healthCheckRegistrations, cancellationToken, async (registration, _) =>
await Parallel.ForEachAsync(activeHealthCheckRegistrations, cancellationToken, async (registration, _) =>
{
string contributorName = GetKey(keys, registration.Name);
SteeltoeHealthCheckResult healthCheckResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,53 @@ public async Task Converts_AspNet_health_check_results()
""");
}

[Fact]
public async Task Can_skip_AspNet_health_check()
{
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(AppSettings);
builder.Services.AddHealthActuator();

IHealthChecksBuilder checksBuilder = builder.Services.AddHealthChecks();
checksBuilder.AddCheck<AspNetUnhealthyCheck>("aspnet-unhealthy-check", tags: ["SkipFromHealthActuator"]);
checksBuilder.AddCheck<AspNetHealthyCheck>("aspnet-healthy-check");

await using WebApplication host = builder.Build();

host.MapHealthChecks("/health");
await host.StartAsync(TestContext.Current.CancellationToken);
using HttpClient httpClient = host.GetTestClient();

HttpResponseMessage actuatorResponse = await httpClient.GetAsync(new Uri("http://localhost/actuator/health"), TestContext.Current.CancellationToken);

actuatorResponse.StatusCode.Should().Be(HttpStatusCode.OK);

string actuatorResponseBody = await actuatorResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);

actuatorResponseBody.Should().BeJson("""
{
"status": "UP",
"components": {
"aspnet-healthy-check": {
"status": "UP",
"description": "healthy-description",
"details": {
"healthy-data-key": "healthy-data-value"
}
}
}
}
""");

HttpResponseMessage aspNetResponse = await httpClient.GetAsync(new Uri("http://localhost/health"), TestContext.Current.CancellationToken);

aspNetResponse.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);

string aspNetResponseBody = await aspNetResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);

aspNetResponseBody.Should().Be("Unhealthy");
}

[Fact]
public async Task Can_use_scoped_AspNet_health_check()
{
Expand Down