diff --git a/src/Components/Aspire.StackExchange.Redis/AspireRedisExtensions.cs b/src/Components/Aspire.StackExchange.Redis/AspireRedisExtensions.cs index b818b72a883..5969cf4b75c 100644 --- a/src/Components/Aspire.StackExchange.Redis/AspireRedisExtensions.cs +++ b/src/Components/Aspire.StackExchange.Redis/AspireRedisExtensions.cs @@ -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() : sp.GetRequiredKeyedService(serviceKey), - healthCheckName)); + healthCheckName, + tags: settings.HealthCheckTags)); } } diff --git a/src/Components/Aspire.StackExchange.Redis/StackExchangeRedisSettings.cs b/src/Components/Aspire.StackExchange.Redis/StackExchangeRedisSettings.cs index 38e59ee5b25..1389f47466d 100644 --- a/src/Components/Aspire.StackExchange.Redis/StackExchangeRedisSettings.cs +++ b/src/Components/Aspire.StackExchange.Redis/StackExchangeRedisSettings.cs @@ -28,4 +28,9 @@ public sealed class StackExchangeRedisSettings /// The default value is . /// public bool DisableTracing { get; set; } + + /// + /// Gets or sets a list of tags that can be used to filter sets of health checks. + /// + public IList HealthCheckTags { get; set; } = []; } diff --git a/tests/Aspire.StackExchange.Redis.Tests/AspireRedisExtensionsTests.cs b/tests/Aspire.StackExchange.Redis.Tests/AspireRedisExtensionsTests.cs index 96ff3463148..c5ecad42656 100644 --- a/tests/Aspire.StackExchange.Redis.Tests/AspireRedisExtensionsTests.cs +++ b/tests/Aspire.StackExchange.Redis.Tests/AspireRedisExtensionsTests.cs @@ -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(); @@ -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 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>(); + 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(ConformanceTests.CreateConfigKey("Aspire:StackExchange:Redis", key, "ConnectionString"), ConnectionString)