Skip to content

Commit

Permalink
Fixed copy/paste misses
Browse files Browse the repository at this point in the history
  • Loading branch information
smbecker committed Dec 2, 2024
1 parent cb89eac commit 07c7f07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/HealthChecks.ClickHouse/ClickHouseHealthCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class ClickHouseHealthCheckOptions
internal ClickHouseHealthCheckOptions()
{
// This ctor is internal on purpose: those who want to use ClickHouseHealthCheckOptions
// need to specify either ConnectionString or DataSource when creating it.
// Making the ConnectionString and DataSource setters internal
// need to specify either ConnectionString or a Func<ClickHouseConnection> when creating it.
// Making the ConnectionString and Func<ClickHouseConnection> setters internal
// allows us to ensure that the customers don't try to mix both concepts.
// By encapsulating all of that, we ensure that all instances of this type are valid.
}
Expand Down
34 changes: 13 additions & 21 deletions src/HealthChecks.ClickHouse/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
## PostgreSQL Health Check
## ClickHouse Health Check

This health check verifies the ability to communicate with [PostgreSQL](https://www.postgresql.org/). It uses the [Npgsql](https://www.npgsql.org/) library.

## NpgsqlDataSource

Starting with Npgsql 7.0 (and .NET 7), the starting point for any database operation is [NpgsqlDataSource](https://www.npgsql.org/doc/api/Npgsql.NpgsqlDataSource.html). The data source represents your PostgreSQL database, and can hand out connections to it, or support direct execution of SQL against it. The data source encapsulates the various Npgsql configuration needed to connect to PostgreSQL, as well as the **connection pooling which makes Npgsql efficient**.

Npgsql's **data source supports additional configuration beyond the connection string**, such as logging, advanced authentication options, type mapping management, and more.
This health check verifies the ability to communicate with [ClickHouse](https://www.clickhouse.com/). It uses the [ClickHouse.Client](https://www.nuget.org/packages/ClickHouse.Client) library.

## Recommended approach

To take advantage of the performance `NpgsqlDataSource` has to offer, it should be used as a **singleton**. Otherwise, the app might end up with having multiple data source instances, all of which would have their own connection pools. This can lead to resources exhaustion and major performance issues (Example: [#1993](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/1993)).

We encourage you to use [Npgsql.DependencyInjection](https://www.nuget.org/packages/Npgsql.DependencyInjection) package for registering a singleton factory for `NpgsqlDataSource`. It allows easy configuration of your Npgsql connections and registers the appropriate services in your DI container.
When registering the ClickHouse health check, it is recommended to use a shared `HttpClient` instance so that you can take advantage of the built-in [pooling](https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines#pooled-connections).

To make the shift to `NpgsqlDataSource` as easy as possible, the `Npgsql.DependencyInjection` package registers not just a factory for the data source, but also factory for `NpgsqlConnection` (and even `DbConnection`). So, your app does not need to suddenly start using `NpgsqlDataSource` everywhere.
We encourage you to use [ClickHouse.Extensions.DependencyInjection](https://www.nuget.org/packages/ClickHouse.Extensions.DependencyInjection) package. It allows easy configuration of your ClickHouse connections and registers the appropriate services in your DI container.

```csharp
void Configure(IServiceCollection services)
{
services.AddNpgsqlDataSource("Host=pg_server;Username=test;Password=test;Database=test");
services.AddHealthChecks().AddNpgSql();
services.AddClickHouseDataSource("Host=ch;Username=default;Password=test;Database=default");
services.AddHealthChecks().AddClickHouse(static sp => sp.GetRequiredService<ClickHouseDataSource>().CreateConnection());
}
```

By default, the `NpgsqlDataSource` instance is resolved from service provider. If you need to access more than one PostgreSQL database, you can use keyed DI services to achieve that:
If you need to access more than one ClickHouse databases, you can use keyed DI services to achieve that:

```csharp
void Configure(IServiceCollection services)
{
services.AddNpgsqlDataSource("Host=pg_server;Username=test;Password=test;Database=first", serviceKey: "first");
services.AddHealthChecks().AddNpgSql(sp => sp.GetRequiredKeyedService<NpgsqlDataSource>("first"));
services.AddClickHouseDataSource("Host=ch;Username=default;Password=test;Database=first", serviceKey: "first");
services.AddHealthChecks().AddClickHouse(static sp => sp.GetRequiredKeyedService<ClickHouseDataSource>("first").CreateConnection());

services.AddNpgsqlDataSource("Host=pg_server;Username=test;Password=test;Database=second", serviceKey: "second");
services.AddHealthChecks().AddNpgSql(sp => sp.GetRequiredKeyedService<NpgsqlDataSource>("second"));
services.AddClickHouseDataSource("Host=ch;Username=default;Password=test;Database=second", serviceKey: "second");
services.AddHealthChecks().AddClickHouse(static sp => sp.GetRequiredKeyedService<ClickHouseDataSource>("second").CreateConnection());
}
```


## Connection String

Raw connection string is of course still supported:
A raw connection string can also be used. This approach is **not** recommended given that it will not take advantage of any pooling within the underlying `HttpClient`.

```csharp
services.AddHealthChecks().AddNpgSql("Host=pg_server;Username=test;Password=test;Database=test");
services.AddHealthChecks().AddClickHouse("Host=ch;Username=default;Password=test;Database=default");
```

0 comments on commit 07c7f07

Please sign in to comment.