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

Add ClickHouse HealthCheck support #2315

Merged
merged 7 commits into from
Dec 11, 2024
Merged
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
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ FTP_PORT=21
FTP_USER=bob
FTP_PASS=12345
RAVENDB_PORT=9030
SOLR_PORT=8983
SOLR_PORT=8983
CLICKHOUSE_USER=default
CLICKHOUSE_PASSWORD=Password12!
CLICKHOUSE_PORT=8123
2 changes: 2 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ flags:
carryforward: true
AzureStorage:
carryforward: true
ClickHouse:
carryforward: true
Consul:
carryforward: true
CosmosDb:
Expand Down
3 changes: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ azure:
- changed-files:
- any-glob-to-any-file: [src/HealthChecks.Azure*/**/*]

clickhouse:
- src/HealthChecks.ClickHouse/**/*

consul:
- changed-files:
- any-glob-to-any-file: [src/HealthChecks.Consul/**/*]
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/healthchecks_clickhouse_cd.yml
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
17 changes: 17 additions & 0 deletions .github/workflows/healthchecks_clickhouse_cd_preview.yml
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
77 changes: 77 additions & 0 deletions .github/workflows/healthchecks_clickhouse_ci.yml
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
14 changes: 14 additions & 0 deletions AspNetCore.Diagnostics.HealthChecks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.Rabbitmq.v6",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.RabbitMQ.v6.Tests", "test\HealthChecks.RabbitMQ.v6.Tests\HealthChecks.RabbitMQ.v6.Tests.csproj", "{2787F63E-ABEA-9461-CDF3-97FE7C5C3DCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.ClickHouse", "src\HealthChecks.ClickHouse\HealthChecks.ClickHouse.csproj", "{96E2B0A3-02BD-456B-8888-4D96DABA99EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.ClickHouse.Tests", "test\HealthChecks.ClickHouse.Tests\HealthChecks.ClickHouse.Tests.csproj", "{2FB5CB9F-F870-48DE-BD1D-306AE86A67CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -881,6 +885,14 @@ Global
{2787F63E-ABEA-9461-CDF3-97FE7C5C3DCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2787F63E-ABEA-9461-CDF3-97FE7C5C3DCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2787F63E-ABEA-9461-CDF3-97FE7C5C3DCC}.Release|Any CPU.Build.0 = Release|Any CPU
{96E2B0A3-02BD-456B-8888-4D96DABA99EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96E2B0A3-02BD-456B-8888-4D96DABA99EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96E2B0A3-02BD-456B-8888-4D96DABA99EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96E2B0A3-02BD-456B-8888-4D96DABA99EB}.Release|Any CPU.Build.0 = Release|Any CPU
{2FB5CB9F-F870-48DE-BD1D-306AE86A67CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FB5CB9F-F870-48DE-BD1D-306AE86A67CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FB5CB9F-F870-48DE-BD1D-306AE86A67CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FB5CB9F-F870-48DE-BD1D-306AE86A67CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1026,6 +1038,8 @@ Global
{D49CF52C-9D21-4D98-8A15-A2B259E9C003} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
{C76D7349-A3D2-7277-93C6-EE92E8E447A5} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{2787F63E-ABEA-9461-CDF3-97FE7C5C3DCC} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
{96E2B0A3-02BD-456B-8888-4D96DABA99EB} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{2FB5CB9F-F870-48DE-BD1D-306AE86A67CA} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B8C62A1-11B6-469F-874C-A02443256568}
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageVersion Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageVersion Include="Azure.Storage.Files.Shares" Version="12.17.1" />
<PackageVersion Include="Azure.Storage.Queues" Version="12.17.1" />
<PackageVersion Include="ClickHouse.Client" Version="7.8.2" />
<PackageVersion Include="Confluent.Kafka" Version="2.3.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="Dapr.Client" Version="1.12.0" />
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ HealthChecks packages include health checks for:
| Azure Key Vault | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.AzureKeyVault)](https://www.nuget.org/packages/AspNetCore.HealthChecks.AzureKeyVault) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.AzureKeyVault)](https://www.nuget.org/packages/AspNetCore.HealthChecks.AzureKeyVault) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/azure)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/azure)
| Azure Search | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.AzureSearch)](https://www.nuget.org/packages/AspNetCore.HealthChecks.AzureSearch) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.AzureSearch)](https://www.nuget.org/packages/AspNetCore.HealthChecks.AzureSearch) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/azure)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/azure)
| Azure Service Bus | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.AzureServiceBus)](https://www.nuget.org/packages/AspNetCore.HealthChecks.AzureServiceBus) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.AzureServiceBus)](https://www.nuget.org/packages/AspNetCore.HealthChecks.AzureServiceBus) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/azure)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/azure) | Queue and Topics |
| ClickHouse | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.ClickHouse)](https://www.nuget.org/packages/AspNetCore.HealthChecks.ClickHouse) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.ClickHouse)](https://www.nuget.org/packages/AspNetCore.HealthChecks.ClickHouse) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/clickhouse)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/clickhouse)
| Consul | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.Consul)](https://www.nuget.org/packages/AspNetCore.HealthChecks.Consul) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.Consul)](https://www.nuget.org/packages/AspNetCore.HealthChecks.Consul) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/consul)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/consul)
| CosmosDb | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.CosmosDb)](https://www.nuget.org/packages/AspNetCore.HealthChecks.CosmosDb) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.CosmosDb)](https://www.nuget.org/packages/AspNetCore.HealthChecks.CosmosDb) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/cosmosdb)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/cosmosdb) | CosmosDb and Azure Table
| Dapr | [![Nuget](https://img.shields.io/nuget/dt/AspNetCore.HealthChecks.Dapr)](https://www.nuget.org/packages/AspNetCore.HealthChecks.Dapr) | [![Nuget](https://img.shields.io/nuget/v/AspNetCore.HealthChecks.Dapr)](https://www.nuget.org/packages/AspNetCore.HealthChecks.Dapr) | [![view](https://img.shields.io/github/issues/Xabaril/AspNetCore.Diagnostics.HealthChecks/dapr)](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/dapr)
Expand Down Expand Up @@ -143,6 +144,7 @@ Install-Package AspNetCore.HealthChecks.AzureKeyVault
Install-Package AspNetCore.HealthChecks.AzureSearch
Install-Package AspNetCore.HealthChecks.AzureServiceBus
Install-Package AspNetCore.HealthChecks.AzureStorage
Install-Package AspNetCore.HealthChecks.ClickHouse
Install-Package AspNetCore.HealthChecks.Consul
Install-Package AspNetCore.HealthChecks.CosmosDb
Install-Package AspNetCore.HealthChecks.Dapr
Expand Down Expand Up @@ -703,4 +705,4 @@ answering [questions](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthCh
2. Follow the code guidelines and conventions.
3. New features are not only code, tests and documentation are also mandatory.
4. PRs with [`Ups for grabs`](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/Ups%20for%20grabs)
and [help wanted](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/help%20wanted) tags are good candidates to contribute.
and [help wanted](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/labels/help%20wanted) tags are good candidates to contribute.
1 change: 1 addition & 0 deletions build/versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<HealthCheckAzureStorageBlobs>9.0.0</HealthCheckAzureStorageBlobs>
<HealthCheckAzureStorageFilesShares>9.0.0</HealthCheckAzureStorageFilesShares>
<HealthCheckAzureStorageQueues>9.0.0</HealthCheckAzureStorageQueues>
<HealthCheckClickHouse>9.0.0</HealthCheckClickHouse>
<HealthCheckCloudFirestore>9.0.0</HealthCheckCloudFirestore>
<HealthCheckConsul>9.0.0</HealthCheckConsul>
<HealthCheckCosmosDb>9.0.0</HealthCheckCosmosDb>
Expand Down
13 changes: 11 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ services:
volumes:
- ./build/docker-services/solrcore:/var/solr/data/solrcore
- ./build/docker-services/solrcoredown:/var/solr/data/solrcoredown
clickhouse:
image: clickhouse/clickhouse-server:24-alpine
environment:
- CLICKHOUSE_DB=${CLICKHOUSE_USER}
- CLICKHOUSE_USER=${CLICKHOUSE_USER}
- CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD}
- CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT="1"
ports:
- ${CLICKHOUSE_PORT}:8123
postgres:
image: postgres
environment:
Expand Down Expand Up @@ -165,7 +174,7 @@ services:
image: postgres
ports:
- "8010:5432"
environment:
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=Password12!
nats:
Expand All @@ -180,7 +189,7 @@ services:
ports:
- "8086:8086"
environment:
DOCKER_INFLUXDB_INIT_MODE: setup
DOCKER_INFLUXDB_INIT_MODE: setup
DOCKER_INFLUXDB_INIT_USERNAME: ci_user
DOCKER_INFLUXDB_INIT_PASSWORD: password
DOCKER_INFLUXDB_INIT_ORG: influxdata
Expand Down
41 changes: 41 additions & 0 deletions src/HealthChecks.ClickHouse/ClickHouseHealthCheck.cs
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);
}
}
}
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 src/HealthChecks.ClickHouse/HealthChecks.ClickHouse.csproj
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>
18 changes: 18 additions & 0 deletions src/HealthChecks.ClickHouse/README.md
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");
});
}
```
Loading
Loading