-
Notifications
You must be signed in to change notification settings - Fork 849
Add ClickHouse HealthCheck support #2315
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f9ff57
Add ClickHouse HealthCheck support
smbecker f7bec00
Fixed copy/paste misses
smbecker 55eda45
Code review feedback
smbecker 782c94b
Fix merge
smbecker 8293e03
Apply suggestions from code review
adamsitnik dd663fd
simplify the public API surface
adamsitnik 8b98bd6
fix the tests
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| name: HealthChecks ClickHouse DB CD | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - release-clickhouse-* | ||
| - release-all-* | ||
|
|
||
| jobs: | ||
| build: | ||
| uses: ./.github/workflows/reusable_cd_workflow.yml | ||
| secrets: inherit | ||
| with: | ||
| BUILD_CONFIG: Release | ||
| PROJECT_PATH: ./src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj | ||
| PACKAGE_NAME: AspNetCore.HealthChecks.ClickHouse |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: HealthChecks ClickHouse DB Preview CD | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - preview-clickhouse-* | ||
| - preview-all-* | ||
|
|
||
| jobs: | ||
| build: | ||
| uses: ./.github/workflows/reusable_cd_preview_workflow.yml | ||
| secrets: inherit | ||
| with: | ||
| BUILD_CONFIG: Release | ||
| VERSION_SUFFIX_PREFIX: rc1 | ||
| PROJECT_PATH: ./src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj | ||
| PACKAGE_NAME: AspNetCore.HealthChecks.ClickHouse |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| name: HealthChecks ClickHouse DB CI | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| push: | ||
| branches: [ master ] | ||
| paths: | ||
| - src/HealthChecks.ClickHouse/** | ||
| - test/HealthChecks.ClickHouse.Tests/** | ||
| - test/_SHARED/** | ||
| - .github/workflows/healthchecks_clickhouse_ci.yml | ||
| - Directory.Build.props | ||
| - Directory.Build.targets | ||
| - Directory.Packages.props | ||
| tags-ignore: | ||
| - release-* | ||
| - preview-* | ||
|
|
||
| pull_request: | ||
| branches: [ master ] | ||
| paths: | ||
| - src/HealthChecks.ClickHouse/** | ||
| - test/HealthChecks.ClickHouse.Tests/** | ||
| - test/_SHARED/** | ||
| - .github/workflows/healthchecks_clickhouse_ci.yml | ||
| - Directory.Build.props | ||
| - Directory.Build.targets | ||
| - Directory.Packages.props | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| services: | ||
| clickhouse: | ||
| image: clickhouse/clickhouse-server:24-alpine | ||
| ports: | ||
| - 8123:8123 | ||
| env: | ||
| CLICKHOUSE_DB: default | ||
| CLICKHOUSE_USER: default | ||
| CLICKHOUSE_PASSWORD: "Password12!" | ||
| CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: "1" | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - name: Setup .NET | ||
| uses: actions/setup-dotnet@v4 | ||
| with: | ||
| dotnet-version: | | ||
| 8.0.x | ||
| 9.0.x | ||
| - name: Restore | ||
| run: | | ||
| dotnet restore ./src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj && | ||
| dotnet restore ./test/HealthChecks.ClickHouse.Tests/HealthChecks.ClickHouse.Tests.csproj | ||
| - name: Check formatting | ||
| run: | | ||
| dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && | ||
| dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.ClickHouse.Tests/HealthChecks.ClickHouse.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) | ||
| - name: Build | ||
| run: | | ||
| dotnet build --no-restore ./src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj && | ||
| dotnet build --no-restore ./test/HealthChecks.ClickHouse.Tests/HealthChecks.ClickHouse.Tests.csproj | ||
| - name: Test | ||
| run: > | ||
| dotnet test | ||
| ./test/HealthChecks.ClickHouse.Tests/HealthChecks.ClickHouse.Tests.csproj | ||
| --no-restore | ||
| --no-build | ||
| --collect "XPlat Code Coverage" | ||
| --results-directory .coverage | ||
| -- | ||
| DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover | ||
| - name: Upload Coverage | ||
| uses: codecov/codecov-action@v3 | ||
| with: | ||
| flags: ClickHouse | ||
| directory: .coverage |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using ClickHouse.Client.ADO; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| namespace HealthChecks.ClickHouse; | ||
|
|
||
| /// <summary> | ||
| /// A health check for ClickHouse databases. | ||
| /// </summary> | ||
| public class ClickHouseHealthCheck : IHealthCheck | ||
| { | ||
| internal const string HEALTH_QUERY = "SELECT 1;"; | ||
|
|
||
| private readonly ClickHouseConnection _connection; | ||
| private readonly string _command; | ||
|
|
||
| public ClickHouseHealthCheck(ClickHouseConnection connection, string command) | ||
| { | ||
| _connection = connection; | ||
| _command = command ?? HEALTH_QUERY; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| try | ||
| { | ||
| await _connection.OpenAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| using var command = _connection.CreateCommand(); | ||
| command.CommandText = _command; | ||
|
|
||
| await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| return HealthCheckResult.Healthy(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return new HealthCheckResult(context.Registration.FailureStatus, description: ex.Message, exception: ex); | ||
| } | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
src/HealthChecks.ClickHouse/DependencyInjection/ClickHouseHealthCheckBuilderExtensions.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using ClickHouse.Client.ADO; | ||
| using HealthChecks.ClickHouse; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods to configure <see cref="ClickHouseHealthCheck"/>. | ||
| /// </summary> | ||
| public static class ClickHouseHealthCheckBuilderExtensions | ||
| { | ||
| private const string NAME = "ClickHouse"; | ||
|
|
||
| /// <summary> | ||
| /// Add a health check for ClickHouse databases. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
| /// <param name="connectionFactory">A factory to build the ClickHouse connection to use.</param> | ||
| /// <param name="healthQuery">The query to be used in check.</param> | ||
| /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'ClickHouse' will be used for the name.</param> | ||
| /// <param name="failureStatus"> | ||
| /// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then | ||
| /// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported. | ||
| /// </param> | ||
| /// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param> | ||
| /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param> | ||
| /// <returns>The specified <paramref name="builder"/>.</returns> | ||
| public static IHealthChecksBuilder AddClickHouse( | ||
| this IHealthChecksBuilder builder, | ||
| Func<IServiceProvider, ClickHouseConnection> connectionFactory, | ||
| string healthQuery = ClickHouseHealthCheck.HEALTH_QUERY, | ||
| string? name = default, | ||
| HealthStatus? failureStatus = default, | ||
| IEnumerable<string>? tags = default, | ||
| TimeSpan? timeout = default) | ||
| { | ||
| Guard.ThrowIfNull(connectionFactory); | ||
| Guard.ThrowIfNull(healthQuery); | ||
|
|
||
| return builder.Add(new HealthCheckRegistration( | ||
| name ?? NAME, | ||
| sp => new ClickHouseHealthCheck(connectionFactory(sp), healthQuery), | ||
| failureStatus, | ||
| tags, | ||
| timeout)); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>netstandard2.1;$(DefaultNetCoreApp)</TargetFrameworks> | ||
| <PackageTags>$(PackageTags);Beat;ClickHouse</PackageTags> | ||
| <Description>HealthChecks.ClickHouse is a health check for ClickHouse.</Description> | ||
| <VersionPrefix>$(HealthCheckClickHouse)</VersionPrefix> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="ClickHouse.Client" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ## ClickHouse Health Check | ||
|
|
||
| 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 | ||
|
|
||
| When registering the ClickHouse health check, it is [recommended](https://github.com/DarkWanderer/ClickHouse.Client/wiki/Connection-lifetime-&-pooling#recommendations) to use `IHttpClientFactory` or a static instance of `HttpClient` to manage connections. | ||
|
|
||
| ```csharp | ||
| void Configure(IServiceCollection services) | ||
| { | ||
| services.AddHttpClient("ClickHouseClient"); | ||
| services.AddHealthChecks().AddClickHouse(static sp => { | ||
| var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>(); | ||
| return new ClickHouseConnection("Host=ch;Username=default;Password=test;Database=default", httpClientFactory, "ClickHouseClient"); | ||
| }); | ||
| } | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.