From 73abc7a094f9ef625b83455898a21d22d2fa6a82 Mon Sep 17 00:00:00 2001 From: Ailton Pinto Date: Wed, 24 Jan 2024 16:11:32 -0300 Subject: [PATCH] feat: add mongodb configuration overloads (#2121) --- .../MongoDbHealthCheckBuilderExtensions.cs | 60 +++++++++++++++++++ .../MongoDbHealthCheck.cs | 8 ++- .../DependencyInjection/RegistrationTests.cs | 44 +++++++++++++- .../HealthChecks.MongoDb.approved.txt | 3 + 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/src/HealthChecks.MongoDb/DependencyInjection/MongoDbHealthCheckBuilderExtensions.cs b/src/HealthChecks.MongoDb/DependencyInjection/MongoDbHealthCheckBuilderExtensions.cs index c1681a210f..39a39b44bb 100644 --- a/src/HealthChecks.MongoDb/DependencyInjection/MongoDbHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.MongoDb/DependencyInjection/MongoDbHealthCheckBuilderExtensions.cs @@ -190,4 +190,64 @@ public static IHealthChecksBuilder AddMongoDb( tags, timeout)); } + + /// + /// Add a health check for MongoDb that list all databases from specified . + /// + /// The . + /// A factory to build MongoClient to be used. + /// The health check name. Optional. If null the type name 'mongodb' will be used for the name. + /// + /// The that should be reported when the health check fails. Optional. If null then + /// the default status of will be reported. + /// + /// A list of tags that can be used to filter sets of health checks. Optional. + /// An optional representing the timeout of the check. + /// The specified . + public static IHealthChecksBuilder AddMongoDb( + this IHealthChecksBuilder builder, + Func mongoClientFactory, + string? name = default, + HealthStatus? failureStatus = default, + IEnumerable? tags = default, + TimeSpan? timeout = default) + { + return builder.Add(new HealthCheckRegistration( + name ?? NAME, + sp => new MongoDbHealthCheck(mongoClientFactory(sp)), + failureStatus, + tags, + timeout)); + } + + /// + /// Add a health check for MongoDb database that list all collections from specified database on . + /// + /// The . + /// A factory to build MongoClient to be used. + /// The name of the database to check. + /// The health check name. Optional. If null the type name 'mongodb' will be used for the name. + /// + /// The that should be reported when the health check fails. Optional. If null then + /// the default status of will be reported. + /// + /// A list of tags that can be used to filter sets of health checks. Optional. + /// An optional representing the timeout of the check. + /// The specified . + public static IHealthChecksBuilder AddMongoDb( + this IHealthChecksBuilder builder, + Func mongoClientFactory, + string mongoDatabaseName, + string? name = default, + HealthStatus? failureStatus = default, + IEnumerable? tags = default, + TimeSpan? timeout = default) + { + return builder.Add(new HealthCheckRegistration( + name ?? NAME, + sp => new MongoDbHealthCheck(mongoClientFactory(sp), mongoDatabaseName), + failureStatus, + tags, + timeout)); + } } diff --git a/src/HealthChecks.MongoDb/MongoDbHealthCheck.cs b/src/HealthChecks.MongoDb/MongoDbHealthCheck.cs index 5d1d75a41d..a0558bad7a 100644 --- a/src/HealthChecks.MongoDb/MongoDbHealthCheck.cs +++ b/src/HealthChecks.MongoDb/MongoDbHealthCheck.cs @@ -8,7 +8,7 @@ namespace HealthChecks.MongoDb; public class MongoDbHealthCheck : IHealthCheck { private static readonly BsonDocumentCommand _command = new(BsonDocument.Parse("{ping:1}")); - private static readonly ConcurrentDictionary _mongoClient = new(); + private static readonly ConcurrentDictionary _mongoClient = new(); private readonly MongoClientSettings _mongoClientSettings; private readonly string? _specifiedDatabase; @@ -21,6 +21,12 @@ public MongoDbHealthCheck(string connectionString, string? databaseName = defaul } } + public MongoDbHealthCheck(IMongoClient client, string? databaseName = default) + : this(client.Settings, databaseName) + { + _mongoClient[_mongoClientSettings.ToString()] = client; + } + public MongoDbHealthCheck(MongoClientSettings clientSettings, string? databaseName = default) { _specifiedDatabase = databaseName; diff --git a/test/HealthChecks.MongoDb.Tests/DependencyInjection/RegistrationTests.cs b/test/HealthChecks.MongoDb.Tests/DependencyInjection/RegistrationTests.cs index 99dab6119b..05b6b34c0b 100644 --- a/test/HealthChecks.MongoDb.Tests/DependencyInjection/RegistrationTests.cs +++ b/test/HealthChecks.MongoDb.Tests/DependencyInjection/RegistrationTests.cs @@ -37,6 +37,26 @@ public void add_health_check_when_properly_configured_mongoClientSettings() check.ShouldBeOfType(); } [Fact] + public void add_health_check_when_properly_configured_mongoClientFactory() + { + var services = new ServiceCollection(); + + services + .AddSingleton(MongoClientSettings.FromUrl(MongoUrl.Create("mongodb://connectionstring"))) + .AddSingleton(sp => new MongoClient(sp.GetRequiredService())) + .AddHealthChecks() + .AddMongoDb(sp => sp.GetRequiredService()); + + using var serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>(); + + var registration = options.Value.Registrations.First(); + var check = registration.Factory(serviceProvider); + + registration.Name.ShouldBe("mongodb"); + check.ShouldBeOfType(); + } + [Fact] public void add_named_health_check_when_properly_configured_connectionString() { var services = new ServiceCollection(); @@ -56,7 +76,9 @@ public void add_named_health_check_when_properly_configured_connectionString() public void add_named_health_check_when_properly_configured_mongoClientSettings() { var services = new ServiceCollection(); - services.AddHealthChecks() + + services + .AddHealthChecks() .AddMongoDb(MongoClientSettings.FromUrl(MongoUrl.Create("mongodb://connectionstring")), name: "my-mongodb-group"); using var serviceProvider = services.BuildServiceProvider(); @@ -65,6 +87,26 @@ public void add_named_health_check_when_properly_configured_mongoClientSettings( var registration = options.Value.Registrations.First(); var check = registration.Factory(serviceProvider); + registration.Name.ShouldBe("my-mongodb-group"); + check.ShouldBeOfType(); + } + [Fact] + public void add_named_health_check_when_properly_configured_mongoClientFactory() + { + var services = new ServiceCollection(); + + services + .AddSingleton(MongoClientSettings.FromUrl(MongoUrl.Create("mongodb://connectionstring"))) + .AddSingleton(sp => new MongoClient(sp.GetRequiredService())) + .AddHealthChecks() + .AddMongoDb(sp => sp.GetRequiredService(), name: "my-mongodb-group"); + + using var serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>(); + + var registration = options.Value.Registrations.First(); + var check = registration.Factory(serviceProvider); + registration.Name.ShouldBe("my-mongodb-group"); check.ShouldBeOfType(); } diff --git a/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.approved.txt b/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.approved.txt index ef4c3f47fd..88613ac36a 100644 --- a/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.approved.txt +++ b/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.approved.txt @@ -2,6 +2,7 @@ namespace HealthChecks.MongoDb { public class MongoDbHealthCheck : Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck { + public MongoDbHealthCheck(MongoDB.Driver.IMongoClient client, string? databaseName = null) { } public MongoDbHealthCheck(MongoDB.Driver.MongoClientSettings clientSettings, string? databaseName = null) { } public MongoDbHealthCheck(string connectionString, string? databaseName = null) { } public System.Threading.Tasks.Task CheckHealthAsync(Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckContext context, System.Threading.CancellationToken cancellationToken = default) { } @@ -12,9 +13,11 @@ namespace Microsoft.Extensions.DependencyInjection public static class MongoDbHealthCheckBuilderExtensions { public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, MongoDB.Driver.MongoClientSettings mongoClientSettings, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } + public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func mongoClientFactory, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func mongodbConnectionStringFactory, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string mongodbConnectionString, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, MongoDB.Driver.MongoClientSettings mongoClientSettings, string mongoDatabaseName, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } + public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func mongoClientFactory, string mongoDatabaseName, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func mongodbConnectionStringFactory, string mongoDatabaseName, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddMongoDb(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string mongodbConnectionString, string mongoDatabaseName, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } }