-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
15 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
``` |