Skip to content
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

PR To support DbDataSource for Postgresql #1832

Merged
merged 10 commits into from
Jun 30, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,28 @@ public static IHealthChecksBuilder AddNpgSql(
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
Guard.ThrowIfNull(connectionStringFactory);
return builder.AddNpgSql(e => new NpgsqlDataSourceBuilder(connectionStringFactory(e)).Build(), healthQuery, configure, name, failureStatus, tags, timeout);
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
}

public static IHealthChecksBuilder AddNpgSql(
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
this IHealthChecksBuilder builder,
Func<IServiceProvider, NpgsqlDataSource> dbDataSourceFactory,
string healthQuery = HEALTH_QUERY,
Action<NpgsqlConnection>? configure = null,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
Guard.ThrowIfNull(dbDataSourceFactory);

return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp =>
{
var options = new NpgSqlHealthCheckOptions
{
ConnectionString = connectionStringFactory(sp),
DataSource = dbDataSourceFactory(sp),
CommandText = healthQuery,
Configure = configure,
};
Expand Down
5 changes: 2 additions & 3 deletions src/HealthChecks.NpgSql/NpgSqlHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Npgsql;

namespace HealthChecks.NpgSql;

Expand All @@ -12,7 +11,7 @@ public class NpgSqlHealthCheck : IHealthCheck

public NpgSqlHealthCheck(NpgSqlHealthCheckOptions options)
{
Guard.ThrowIfNull(options.ConnectionString, true);
Guard.ThrowIfNull(options.DataSource, true);
Guard.ThrowIfNull(options.CommandText, true);
_options = options;
}
Expand All @@ -22,7 +21,7 @@ public async Task<HealthCheckResult> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/HealthChecks.NpgSql/NpgSqlHealthCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace HealthChecks.NpgSql;
public class NpgSqlHealthCheckOptions
{
/// <summary>
/// The Postgres connection string to be used.
/// The Postgres data source to be used.
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public string ConnectionString { get; set; } = null!;
michaelmairegger marked this conversation as resolved.
Show resolved Hide resolved
public NpgsqlDataSource DataSource { get; set; } = null!;

/// <summary>
/// The query to be executed.
Expand Down