-
Notifications
You must be signed in to change notification settings - Fork 807
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
19 changed files
with
727 additions
and
4 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
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 |
---|---|---|
@@ -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 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 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,76 @@ | ||
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 | ||
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 | ||
|
||
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: | | ||
6.0.x | ||
7.0.x | ||
8.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 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
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Diagnostics; | ||
using ClickHouse.Client.ADO; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.ClickHouse; | ||
|
||
/// <summary> | ||
/// A health check for ClickHouse databases. | ||
/// </summary> | ||
public class ClickHouseHealthCheck : IHealthCheck | ||
{ | ||
private readonly ClickHouseHealthCheckOptions _options; | ||
|
||
public ClickHouseHealthCheck(ClickHouseHealthCheckOptions options) | ||
{ | ||
Debug.Assert(options.ConnectionString is not null); | ||
Guard.ThrowIfNull(options.CommandText, true); | ||
_options = options; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await using var connection = new ClickHouseConnection(_options.ConnectionString); | ||
_options.Configure?.Invoke(connection); | ||
await connection.OpenAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
using var command = connection.CreateCommand(); | ||
command.CommandText = _options.CommandText; | ||
var result = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
return _options.HealthCheckResultBuilder == null | ||
? HealthCheckResult.Healthy() | ||
: _options.HealthCheckResultBuilder(result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, description: ex.Message, exception: ex); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/HealthChecks.ClickHouse/ClickHouseHealthCheckOptions.cs
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using ClickHouse.Client.ADO; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.ClickHouse; | ||
|
||
/// <summary> | ||
/// Options for <see cref="ClickHouseHealthCheck"/>. | ||
/// </summary> | ||
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 | ||
// 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. | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="ClickHouseHealthCheckOptions"/>. | ||
/// </summary> | ||
/// <param name="connectionString">The ClickHouse connection string to be used.</param> | ||
public ClickHouseHealthCheckOptions(string connectionString) | ||
{ | ||
ConnectionString = Guard.ThrowIfNull(connectionString, throwOnEmptyString: true); | ||
} | ||
|
||
/// <summary> | ||
/// The ClickHouse connection string to be used. | ||
/// </summary> | ||
public string? ConnectionString { get; internal set; } | ||
|
||
/// <summary> | ||
/// The query to be executed. | ||
/// </summary> | ||
public string CommandText { get; set; } = ClickHouseHealthCheckBuilderExtensions.HEALTH_QUERY; | ||
|
||
/// <summary> | ||
/// An optional action executed before the connection is opened in the health check. | ||
/// </summary> | ||
public Action<ClickHouseConnection>? Configure { get; set; } | ||
|
||
/// <summary> | ||
/// An optional delegate to build health check result. | ||
/// </summary> | ||
public Func<object?, HealthCheckResult>? HealthCheckResultBuilder { get; set; } | ||
} |
Oops, something went wrong.