diff --git a/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs b/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs index 498e53dee5..fbcecaf63d 100644 --- a/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.NpgSql/DependencyInjection/NpgSqlHealthCheckBuilderExtensions.cs @@ -65,7 +65,35 @@ public static IHealthChecksBuilder AddNpgSql( IEnumerable? tags = default, TimeSpan? timeout = default) { - Guard.ThrowIfNull(connectionStringFactory); + return builder.AddNpgSql(sp => new NpgsqlDataSourceBuilder(connectionStringFactory(sp)).Build(), healthQuery, configure, name, failureStatus, tags, timeout); + } + + /// + /// Add a health check for Postgres databases. + /// + /// The . + /// A factory to build the NpgsqlDataSource to use. + /// The query to be used in check. + /// An optional action to allow additional Npgsql specific configuration. + /// The health check name. Optional. If null the type name 'npgsql' 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 AddNpgSql( + this IHealthChecksBuilder builder, + Func dbDataSourceFactory, + string healthQuery = HEALTH_QUERY, + Action? configure = null, + string? name = default, + HealthStatus? failureStatus = default, + IEnumerable? tags = default, + TimeSpan? timeout = default) + { + Guard.ThrowIfNull(dbDataSourceFactory); return builder.Add(new HealthCheckRegistration( name ?? NAME, @@ -73,7 +101,7 @@ public static IHealthChecksBuilder AddNpgSql( { var options = new NpgSqlHealthCheckOptions { - ConnectionString = connectionStringFactory(sp), + DataSource = dbDataSourceFactory(sp), CommandText = healthQuery, Configure = configure, }; diff --git a/src/HealthChecks.NpgSql/NpgSqlHealthCheck.cs b/src/HealthChecks.NpgSql/NpgSqlHealthCheck.cs index e33cc96977..37a72c681e 100644 --- a/src/HealthChecks.NpgSql/NpgSqlHealthCheck.cs +++ b/src/HealthChecks.NpgSql/NpgSqlHealthCheck.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Diagnostics.HealthChecks; -using Npgsql; namespace HealthChecks.NpgSql; @@ -12,7 +11,7 @@ public class NpgSqlHealthCheck : IHealthCheck public NpgSqlHealthCheck(NpgSqlHealthCheckOptions options) { - Guard.ThrowIfNull(options.ConnectionString, true); + Guard.ThrowIfNull(options.DataSource); Guard.ThrowIfNull(options.CommandText, true); _options = options; } @@ -22,7 +21,7 @@ public async Task CheckHealthAsync(HealthCheckContext context { try { - using var connection = new NpgsqlConnection(_options.ConnectionString); + await using var connection = _options.DataSource!.CreateConnection(); _options.Configure?.Invoke(connection); await connection.OpenAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs b/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs index 910d8053f7..0138c30ea8 100644 --- a/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs +++ b/src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs @@ -11,8 +11,18 @@ public class NpgSqlHealthCheckOptions { /// /// The Postgres connection string to be used. + /// Use property for advanced configuration. /// - public string ConnectionString { get; set; } = null!; + public string? ConnectionString + { + get => DataSource?.ConnectionString; + set => DataSource = value is not null ? NpgsqlDataSource.Create(value) : null; + } + + /// + /// The Postgres data source to be used. + /// + public NpgsqlDataSource? DataSource { get; set; } /// /// The query to be executed. diff --git a/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs b/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs index e45c86b00e..b897f56df3 100644 --- a/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs +++ b/test/HealthChecks.Npgsql.Tests/DependencyInjection/RegistrationTests.cs @@ -9,7 +9,7 @@ public void add_health_check_when_properly_configured() { var services = new ServiceCollection(); services.AddHealthChecks() - .AddNpgSql("connectionstring"); + .AddNpgSql("Server=localhost"); using var serviceProvider = services.BuildServiceProvider(); var options = serviceProvider.GetRequiredService>(); @@ -27,7 +27,7 @@ public void add_named_health_check_when_properly_configured() { var services = new ServiceCollection(); services.AddHealthChecks() - .AddNpgSql("connectionstring", name: "my-npg-1"); + .AddNpgSql("Server=localhost", name: "my-npg-1"); using var serviceProvider = services.BuildServiceProvider(); var options = serviceProvider.GetRequiredService>(); @@ -48,7 +48,7 @@ public void add_health_check_with_connection_string_factory_when_properly_config .AddNpgSql(_ => { factoryCalled = true; - return "connectionstring"; + return "Server=localhost"; }, name: "my-npg-1"); using var serviceProvider = services.BuildServiceProvider(); diff --git a/test/HealthChecks.Npgsql.Tests/HealthChecks.NpgSql.approved.txt b/test/HealthChecks.Npgsql.Tests/HealthChecks.NpgSql.approved.txt index 3724c042be..d84990c4dd 100644 --- a/test/HealthChecks.Npgsql.Tests/HealthChecks.NpgSql.approved.txt +++ b/test/HealthChecks.Npgsql.Tests/HealthChecks.NpgSql.approved.txt @@ -10,7 +10,8 @@ namespace HealthChecks.NpgSql public NpgSqlHealthCheckOptions() { } public string CommandText { get; set; } public System.Action? Configure { get; set; } - public string ConnectionString { get; set; } + public string? ConnectionString { get; set; } + public Npgsql.NpgsqlDataSource? DataSource { get; set; } public System.Func? HealthCheckResultBuilder { get; set; } } } @@ -19,6 +20,7 @@ namespace Microsoft.Extensions.DependencyInjection public static class NpgSqlHealthCheckBuilderExtensions { public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddNpgSql(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, HealthChecks.NpgSql.NpgSqlHealthCheckOptions options, 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 AddNpgSql(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func dbDataSourceFactory, string healthQuery = "SELECT 1;", System.Action? configure = null, 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 AddNpgSql(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func connectionStringFactory, string healthQuery = "SELECT 1;", System.Action? configure = null, 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 AddNpgSql(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string connectionString, string healthQuery = "SELECT 1;", System.Action? configure = null, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } }