Skip to content

Add Health Check tags to client integrations #8877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ private static void AddRedisClient(
// That is why we don't invoke it here, but capture the state (in a closure)
// and let the health check invoke it and handle the exception (if any).
connectionMultiplexerFactory: sp => serviceKey is null ? sp.GetRequiredService<IConnectionMultiplexer>() : sp.GetRequiredKeyedService<IConnectionMultiplexer>(serviceKey),
healthCheckName));
healthCheckName,
tags: settings.HealthCheckTags));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder if we want/need a more general way to affect the HealthCheckRegistration. It has other properties on it that a user may want to set:

https://github.com/dotnet/aspnetcore/blob/7ced4d6df649ecdd9b298d1b30491f837c815165/src/HealthChecks/Abstractions/src/HealthCheckRegistration.cs#L142-L171

  • Timeout
  • Delay
  • Period

Any thoughts @sebastienros on if we need to be able to configure these things? Or would we tell someone to just turn ours off, and add their own?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Timeout property has been configured for some clients:

if (!settings.DisableHealthChecks)
{
var healthCheckName = serviceKey is null ? "Qdrant.Client" : $"Qdrant.Client_{connectionName}";
builder.TryAddHealthCheck(new HealthCheckRegistration(
healthCheckName,
sp => new QdrantHealthCheck(serviceKey is null ?
sp.GetRequiredService<QdrantClient>() :
sp.GetRequiredKeyedService<QdrantClient>(serviceKey)),
failureStatus: null,
tags: null,
timeout: settings.HealthCheckTimeout
));
}

if (!settings.DisableHealthChecks)
{
var healthCheckName = serviceKey is null ? "Elastic.Clients.Elasticsearch" : $"Elastic.Clients.Elasticsearch_{connectionName}";
builder.TryAddHealthCheck(new HealthCheckRegistration(
healthCheckName,
sp => new ElasticsearchHealthCheck(serviceKey is null ?
sp.GetRequiredService<ElasticsearchClient>() :
sp.GetRequiredKeyedService<ElasticsearchClient>(serviceKey)),
failureStatus: null,
tags: null,
timeout: settings.HealthCheckTimeout > 0 ? TimeSpan.FromMilliseconds(settings.HealthCheckTimeout.Value) : null
));
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public sealed class StackExchangeRedisSettings
/// The default value is <see langword="false"/>.
/// </value>
public bool DisableTracing { get; set; }

/// <summary>
/// Gets or sets a list of tags that can be used to filter sets of health checks.
/// </summary>
public IList<string> HealthCheckTags { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void KeyedServiceRedisInstrumentation()
builder.AddKeyedRedisClient("redis", settings =>
{
settings.ConnectionString = "localhost";
settings.DisableTracing = ! true;
settings.DisableTracing = !true;
});
using var host = builder.Build();

Expand Down Expand Up @@ -383,6 +383,40 @@ public async Task CanAddMultipleKeyedCachingServices()
Assert.Single(connection3.GetServers().Single().Keys());
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void AddRedisClientShouldSetHealthCheckTags(bool usedKeyedServices)
{
var builder = Host.CreateEmptyApplicationBuilder(null);
PopulateConfiguration(builder.Configuration);

IList<string> tags = ["aspire-redis", "stack-exchange-redis"];

if (usedKeyedServices)
{
builder.AddKeyedRedisClient("redis", configureSettings: settings =>
{
settings.HealthCheckTags = tags;
});
}
else
{
builder.AddRedisClient("redis", configureSettings: settings =>
{
settings.HealthCheckTags = tags;
});
}

using var host = builder.Build();

var healthcheckName = usedKeyedServices ? "StackExchange.Redis_redis" : "StackExchange.Redis";

var options = host.Services.GetRequiredService<IOptions<HealthCheckServiceOptions>>();
Assert.NotNull(options);
Assert.Contains(options.Value.Registrations, health => health.Name == healthcheckName && health.Tags.Contains("aspire-redis") && health.Tags.Contains("stack-exchange-redis"));
}

private void PopulateConfiguration(ConfigurationManager configuration, string? key = null) =>
configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>(ConformanceTests.CreateConfigKey("Aspire:StackExchange:Redis", key, "ConnectionString"), ConnectionString)
Expand Down
Loading