Skip to content
Closed
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
1 change: 1 addition & 0 deletions HealthChecks.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Project Path="src/NetEvolve.HealthChecks.MySql.Connector/NetEvolve.HealthChecks.MySql.Connector.csproj" />
<Project Path="src/NetEvolve.HealthChecks.MySql/NetEvolve.HealthChecks.MySql.csproj" />
<Project Path="src/NetEvolve.HealthChecks.Npgsql/NetEvolve.HealthChecks.Npgsql.csproj" />
<Project Path="src/NetEvolve.HealthChecks.Npgsql.Devart/NetEvolve.HealthChecks.Npgsql.Devart.csproj" />
<Project Path="src/NetEvolve.HealthChecks.Odbc/NetEvolve.HealthChecks.Odbc.csproj" />
<Project Path="src/NetEvolve.HealthChecks.Oracle/NetEvolve.HealthChecks.Oracle.csproj" />
<Project Path="src/NetEvolve.HealthChecks.Qdrant/NetEvolve.HealthChecks.Qdrant.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace NetEvolve.HealthChecks.Npgsql.Devart;

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using global::Devart.Data.PostgreSql;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using NetEvolve.Arguments;
using NetEvolve.HealthChecks.Abstractions;

/// <summary>
/// Extensions methods for <see cref="IHealthChecksBuilder"/> with custom Health Checks.
/// </summary>
public static class DependencyInjectionExtensions
{
private static readonly string[] _defaultTags = ["postgresql", "database", "devart"];

/// <summary>
/// Add a health check for the PostgreSQL database, based on Devart's <see cref="PgSqlConnection"/>.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="name">The name of the <see cref="NpgsqlDevartHealthCheck"/>.</param>
/// <param name="options">An optional action to configure.</param>
/// <param name="tags">A list of additional tags that can be used to filter sets of health checks. Optional.</param>
/// <exception cref="ArgumentNullException">The <paramref name="builder"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="name"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">The <paramref name="name"/> is <see langword="null" /> or <c>whitespace</c>.</exception>
/// <exception cref="ArgumentException">The <paramref name="name"/> is already in use.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="tags"/> is <see langword="null" />.</exception>
public static IHealthChecksBuilder AddPostgreSqlDevart(
[NotNull] this IHealthChecksBuilder builder,
[NotNull] string name,
Action<NpgsqlDevartOptions>? options = null,
params string[] tags
)
{
ArgumentNullException.ThrowIfNull(builder);
Argument.ThrowIfNullOrEmpty(name);
ArgumentNullException.ThrowIfNull(tags);

if (!builder.IsServiceTypeRegistered<NpgsqlDevartCheckMarker>())
{
_ = builder
.Services.AddSingleton<NpgsqlDevartCheckMarker>()
.AddSingleton<NpgsqlDevartHealthCheck>()
.ConfigureOptions<NpgsqlDevartConfigure>();
}

builder.ThrowIfNameIsAlreadyUsed<NpgsqlDevartHealthCheck>(name);

if (options is not null)
{
_ = builder.Services.Configure(name, options);
}

return builder.AddCheck<NpgsqlDevartHealthCheck>(
name,
HealthStatus.Unhealthy,
_defaultTags.Union(tags, StringComparer.OrdinalIgnoreCase)
);
}

private sealed partial class NpgsqlDevartCheckMarker;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(_ProjectTargetFrameworks)</TargetFrameworks>
<Description>Contains HealthChecks for PostgreSQL, based on the nuget package `Devart.Data.PostgreSql`.</Description>
<PackageTags>$(PackageTags);postgresql;npgsql;devart</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Devart.Data.PostgreSql" />
<PackageReference Include="NetEvolve.Extensions.Tasks" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NetEvolve.HealthChecks.Abstractions\NetEvolve.HealthChecks.Abstractions.csproj" />
<ProjectReference
Include="..\SourceGenerator.SqlHealthCheck\SourceGenerator.SqlHealthCheck.csproj"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer"
/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace NetEvolve.HealthChecks.Npgsql.Devart;

using System.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using NetEvolve.Arguments;
using static Microsoft.Extensions.Options.ValidateOptionsResult;

internal sealed class NpgsqlDevartConfigure
: IConfigureNamedOptions<NpgsqlDevartOptions>,
IPostConfigureOptions<NpgsqlDevartOptions>,
IValidateOptions<NpgsqlDevartOptions>
{
private readonly IConfiguration _configuration;

public NpgsqlDevartConfigure(IConfiguration configuration) => _configuration = configuration;

public void Configure(string? name, NpgsqlDevartOptions options)
{
Argument.ThrowIfNullOrWhiteSpace(name);
_configuration.Bind($"HealthChecks:PostgreSql:{name}", options);
}

public void Configure(NpgsqlDevartOptions options) => Configure(Options.DefaultName, options);

public void PostConfigure(string? name, NpgsqlDevartOptions options)
{
if (string.IsNullOrWhiteSpace(name))
{
return;
}

if (string.IsNullOrWhiteSpace(options.Command))
{
options.Command = NpgsqlDevartHealthCheck.DefaultCommand;
}
}

public ValidateOptionsResult Validate(string? name, NpgsqlDevartOptions options)
{
if (string.IsNullOrWhiteSpace(name))
{
return Fail("The name cannot be null or whitespace.");
}

if (options is null)
{
return Fail("The option cannot be null.");
}

if (string.IsNullOrWhiteSpace(options.ConnectionString))
{
return Fail("The connection string cannot be null or whitespace.");
}

if (options.Timeout < Timeout.Infinite)
{
return Fail("The timeout cannot be less than infinite (-1).");
}

return Success;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace NetEvolve.HealthChecks.Npgsql.Devart;

using global::Devart.Data.PostgreSql;
using Microsoft.Extensions.Options;
using NetEvolve.HealthChecks.Abstractions;
using SourceGenerator.SqlHealthCheck;

[GenerateSqlHealthCheck(typeof(PgSqlConnection), typeof(NpgsqlDevartOptions), false)]
internal sealed partial class NpgsqlDevartHealthCheck(IOptionsMonitor<NpgsqlDevartOptions> optionsMonitor)
: ConfigurableHealthCheckBase<NpgsqlDevartOptions>(optionsMonitor)
{
/// <summary>
/// The default sql command.
/// </summary>
public const string DefaultCommand = "SELECT 1;";
}
34 changes: 34 additions & 0 deletions src/NetEvolve.HealthChecks.Npgsql.Devart/NpgsqlDevartOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace NetEvolve.HealthChecks.Npgsql.Devart;

/// <summary>
/// Options for <see cref="NpgsqlDevartHealthCheck"/>
/// </summary>
public sealed record NpgsqlDevartOptions
{
/// <summary>
/// Gets or sets the connection string for the PostgreSQL database to check.
/// </summary>
/// <value>
/// The connection string used to establish a connection to the PostgreSQL database.
/// </value>
public string ConnectionString { get; set; } = default!;

/// <summary>
/// Gets or sets the timeout in milliseconds to use when connecting and executing tasks against the PostgreSQL database.
/// </summary>
/// <value>
/// The timeout in milliseconds. Default value is 100 milliseconds.
/// </value>
public int Timeout { get; set; } = 100;

/// <summary>
/// Gets the SQL command to execute against the PostgreSQL database.
/// </summary>
/// <value>
/// The SQL command to execute. Default value is defined by <see cref="NpgsqlDevartHealthCheck.DefaultCommand"/>.
/// </value>
/// <remarks>
/// This property is for internal use only.
/// </remarks>
public string Command { get; internal set; } = NpgsqlDevartHealthCheck.DefaultCommand;
}
80 changes: 80 additions & 0 deletions src/NetEvolve.HealthChecks.Npgsql.Devart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# NetEvolve.HealthChecks.Npgsql.Devart

[![NuGet](https://img.shields.io/nuget/v/NetEvolve.HealthChecks.Npgsql.Devart?logo=nuget)](https://www.nuget.org/packages/NetEvolve.HealthChecks.Npgsql.Devart/)
[![NuGet](https://img.shields.io/nuget/dt/NetEvolve.HealthChecks.Npgsql.Devart?logo=nuget)](https://www.nuget.org/packages/NetEvolve.HealthChecks.Npgsql.Devart/)

This package provides a health check for PostgreSQL, based on the [Devart.Data.PostgreSql](https://www.nuget.org/packages/Devart.Data.PostgreSql/) package.
The main purpose is to check if the PostgreSQL database is available and if the database is online.

:bulb: This package is available for .NET 8.0 and later.

## Installation
To use this package, you need to add the package to your project. You can do this by using the NuGet package manager or by using the dotnet CLI.
```powershell
dotnet add package NetEvolve.HealthChecks.Npgsql.Devart
```

## Health Check - NpgsqlDevart Liveness
The health check is a liveness check. It checks if the PostgreSQL database is available and if the database is online.
If the query needs longer than the configured timeout, the health check will return `Degraded`.
If the query fails, for whatever reason, the health check will return `Unhealthy`.

### Usage
After adding the package, you need to import the namespace and add the health check to the health check builder.
```csharp
using NetEvolve.HealthChecks.Npgsql.Devart;
```
Therefore, you can use two different approaches. In both approaches you have to provide a name for the health check.

:heavy_exclamation_mark: The configuration of this package is compatible with the [NetEvolve.HealthChecks.Npgsql](https://www.nuget.org/packages/NetEvolve.HealthChecks.Npgsql/) package. If you want to use the new package, you can simply replace the package and the configuration will be compatible.

### Parameters
- `name`: The name of the health check.
- `options`: The configuration options for the health check. Optional.
- `tags`: The tags for the health check. Optional.
- `postgresql`: This is always used as tag, so that you can filter for all PostgreSQL health checks.
- `database`: This is always used as tag, so that you can filter for all database health checks.
- `devart`: This is always used as tag, so that you can filter for all Devart health checks.

### Variant 1: Configuration based
The first one is to use the configuration based approach. This approach is recommended if you have multiple PostgreSQL instances to check.
```csharp
var builder = services.AddHealthChecks();

builder.AddPostgreSqlDevart("<name>");
```

The configuration looks like this:
```json
{
..., // other configuration
"HealthChecks": {
"PostgreSql": {
"<name>": {
"ConnectionString": "<connection string>",
"Timeout": "<timeout>" // optional, default is 100 milliseconds
}
}
}
}
```

### Variant 2: Builder based
The second approach is to use the builder based approach. This approach is recommended if you only have one PostgreSQL instance to check or dynamic programmatic values.
```csharp
var builder = services.AddHealthChecks();

builder.AddPostgreSqlDevart("<name>", options =>
{
options.ConnectionString = "<connection string>";
options.Timeout = "<timeout>"; // optional, default is 100 milliseconds
});
```

### :bulb: You can always provide tags to all health checks, for grouping or filtering.

```csharp
var builder = services.AddHealthChecks();

builder.AddPostgreSqlDevart("<name>", options => ..., "postgresql", "database");
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="ClickHouse.Client" />
<PackageReference Include="Confluent.Kafka" />
<PackageReference Include="Dapr.Client" />
<PackageReference Include="Devart.Data.PostgreSql" />
<PackageReference Include="Devart.Data.SqlServer" />
<PackageReference Include="DuckDB.NET.Data" />
<PackageReference Include="Elastic.Clients.Elasticsearch" />
Expand Down Expand Up @@ -118,6 +119,7 @@
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.MySql.Connector\NetEvolve.HealthChecks.MySql.Connector.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.MySql\NetEvolve.HealthChecks.MySql.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.Npgsql\NetEvolve.HealthChecks.Npgsql.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.Npgsql.Devart\NetEvolve.HealthChecks.Npgsql.Devart.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.Odbc\NetEvolve.HealthChecks.Odbc.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.Oracle\NetEvolve.HealthChecks.Oracle.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.HealthChecks.Qdrant\NetEvolve.HealthChecks.Qdrant.csproj" />
Expand Down
Loading
Loading