-
Notifications
You must be signed in to change notification settings - Fork 804
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 CassandraDb HealthCheck support #2174
Open
djesusnet
wants to merge
37
commits into
Xabaril:master
Choose a base branch
from
djesusnet:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
37c579e
Add CassandraDb HealthCheck support
djesusnet 9565dc4
Update healthchecks_cassandradb_ci.yaml
djesusnet 5661506
Update ymal
djesusnet 0dc5f14
Update CassandraDbHealthCheckTests
djesusnet 17ae1a8
update CassandraDbHealthCheckTests
djesusnet 2d161ff
updates yml and CassandraDbHealthCheckTests
djesusnet 02797c9
Update unit tests
djesusnet 9a2abdc
Update yml
djesusnet 1e7b60a
Update HealthChecks CassandraDb CIi.yml
djesusnet 50a4995
Update Ci.yml
djesusnet d92a380
Updates unit tests
djesusnet 2dcd466
update dockercompose
djesusnet 49261bb
Update DockerCompose
djesusnet 4ee4e47
update docker-compose
djesusnet 5f23cb0
Update CI
djesusnet 96d5f4a
Update CI
djesusnet 68a11d9
update CI
djesusnet cefe913
update ci
djesusnet 9c627ce
update CI
djesusnet 88be2d8
Update ci
djesusnet eba5c54
Ajuste CI
djesusnet db136c5
update Ci
djesusnet 1917355
Ajuste no CI
djesusnet 5f595a3
Ajuste Ci
djesusnet 67fac24
update CI - CassandraDb
djesusnet 6f776ef
Update unit test
djesusnet 488cf5e
update CI
djesusnet 281575a
update Ci
djesusnet 025ad27
Ajuste no CI
djesusnet 3a665da
Update CassandraDbHealthCheck
djesusnet 338713e
Update tests
djesusnet 2365d8a
Insert txt
djesusnet b7cdd1d
Insert txt
djesusnet 57a8bec
update HealthChecks SQL Server DB CI
djesusnet cd8d8b2
Update and rollback
djesusnet e7bff20
Update Readme.md
djesusnet 1be70b8
Merge branch 'master' into master
djesusnet 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 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 CassandraDb CD | ||
|
||
on: | ||
push: | ||
tags: | ||
- release-cassandradb-* | ||
- release-all-* | ||
|
||
jobs: | ||
build: | ||
uses: ./.github/workflows/reusable_cd_workflow.yml | ||
secrets: inherit | ||
with: | ||
BUILD_CONFIG: Release | ||
PROJECT_PATH: ./src/HealthChecks.CassandraDb/HealthChecks.CassandraDb.csproj | ||
PACKAGE_NAME: AspNetCore.HealthChecks.CassandraDb |
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 CassandraDb Preview CD | ||
|
||
on: | ||
push: | ||
tags: | ||
- preview-cassandradb-* | ||
- 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.CassandraDb/HealthChecks.CassandraDb.csproj | ||
PACKAGE_NAME: AspNetCore.HealthChecks.CassandraDb |
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,93 @@ | ||
name: HealthChecks CassandraDb CI | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [ master ] | ||
paths: | ||
- src/HealthChecks.CassandraDb/** | ||
- test/HealthChecks.CassandraDb.Tests/** | ||
- test/_SHARED/** | ||
- .github/workflows/healthchecks_cassandradb_ci.yml | ||
- Directory.Build.props | ||
- Directory.Build.targets | ||
tags-ignore: | ||
- release-* | ||
- preview-* | ||
|
||
pull_request: | ||
branches: [ master ] | ||
paths: | ||
- src/HealthChecks.CassandraDb/** | ||
- test/HealthChecks.CassandraDb.Tests/** | ||
- test/_SHARED/** | ||
- .github/workflows/healthchecks_cassandradb_ci.yml | ||
- Directory.Build.props | ||
- Directory.Build.targets | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
services: | ||
CassandraDb: | ||
image: cassandra:latest | ||
ports: | ||
- 9042:9042 | ||
options: --health-cmd="cqlsh -e 'DESCRIBE KEYSPACES'" --health-interval=10s --health-timeout=5s --health-retries=6 | ||
|
||
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: Cache NuGet Packages | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.nuget/packages | ||
key: ${{ runner.os }}-nuget-${{ hashFiles('**/.*csproj') }} | ||
restore-keys: ${{ runner.os }}-nuget- | ||
|
||
- name: Restore | ||
run: | | ||
dotnet restore ./src/HealthChecks.CassandraDb/HealthChecks.CassandraDb.csproj && | ||
dotnet restore ./test/HealthChecks.CassandraDb.Tests/HealthChecks.CassandraDb.Tests.csproj | ||
|
||
- name: Check formatting | ||
run: | | ||
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.CassandraDb/HealthChecks.CassandraDb.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && | ||
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.CassandraDb.Tests/HealthChecks.CassandraDb.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) | ||
|
||
- name: Build | ||
run: | | ||
dotnet build --no-restore ./src/HealthChecks.CassandraDb/HealthChecks.CassandraDb.csproj && | ||
dotnet build --no-restore ./test/HealthChecks.CassandraDb.Tests/HealthChecks.CassandraDb.Tests.csproj | ||
|
||
- name: Wait for Cassandra to be ready | ||
run: | | ||
until docker run --network host -e CQLSH_HOST=localhost -e CQLSH_PORT=9042 cassandra:latest cqlsh -e "SELECT now() FROM system.local;"; do | ||
echo "Waiting for Cassandra..." | ||
sleep 10 | ||
done | ||
|
||
- name: Test | ||
run: > | ||
dotnet test | ||
./test/HealthChecks.CassandraDb.Tests/HealthChecks.CassandraDb.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: CassandraDb | ||
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 Cassandra; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.CassandraDb; | ||
|
||
public class CassandraDbHealthCheck : IHealthCheck | ||
{ | ||
private readonly CassandraDbOptions _options; | ||
|
||
public CassandraDbHealthCheck(CassandraDbOptions options) | ||
{ | ||
_options = Guard.ThrowIfNull(options); | ||
|
||
if (_options.ContactPoint is null && _options.Keyspace is null) | ||
{ | ||
throw new ArgumentException("A connection or connection string must be set!", nameof(options)); | ||
} | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var builder = Cluster.Builder().AddContactPoint(_options.ContactPoint); | ||
_options.ConfigureClusterBuilder?.Invoke(builder); | ||
|
||
var cluster = builder.Build(); | ||
|
||
ISession session = await cluster.ConnectAsync(_options.Keyspace).ConfigureAwait(false); | ||
|
||
|
||
RowSet rows = await session.ExecuteAsync(new SimpleStatement(_options.Query)).ConfigureAwait(false); | ||
var result = rows.FirstOrDefault(); | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
return HealthCheckResult.Unhealthy(ex.Message); | ||
} | ||
} | ||
} |
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,14 @@ | ||
using Cassandra; | ||
|
||
namespace HealthChecks.CassandraDb; | ||
|
||
/// <summary> | ||
/// Options for CassandraHealthCheck. | ||
/// </summary> | ||
public class CassandraDbOptions | ||
{ | ||
public string ContactPoint { get; set; } = null!; | ||
public string Keyspace { get; set; } = null!; | ||
public string Query { get; set; } = "SELECT now() FROM system.local;"; | ||
public Action<Builder> ConfigureClusterBuilder { get; set; } = null!; | ||
} |
91 changes: 91 additions & 0 deletions
91
src/HealthChecks.CassandraDb/DependencyInjection/CassandraDbHealthCheckBuilderExtensions.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,91 @@ | ||
using Cassandra; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.CassandraDb.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to configure <see cref="CassandraDbHealthCheck"/>. | ||
/// </summary> | ||
public static class CassandraDbHealthCheckBuilderExtensions | ||
{ | ||
private const string NAME = "cassandra"; | ||
|
||
/// <summary> | ||
/// Add a health check for Cassandra databases. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="contactPoint">The Cassandra contact point to be used.</param> | ||
/// <param name="keyspace">The Cassandra keyspace to be used.</param> | ||
/// <param name="query">The query to be executed. Default is 'SELECT now() FROM system.local;'.</param> | ||
/// <param name="configureClusterBuilder">An action to allow additional Cassandra specific configuration. It cannot be null.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'cassandra' 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 AddCassandra( | ||
this IHealthChecksBuilder builder, | ||
string contactPoint, | ||
string keyspace, | ||
string query = "SELECT now() FROM system.local;", | ||
Action<Builder>? configureClusterBuilder = null, // Note: The caller must ensure this is not null | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
// Define a default configuration action if none is provided | ||
var defaultConfigurationAction = new Action<Builder>(b => | ||
// Default minimal configuration | ||
b.AddContactPoint(contactPoint).Build()); | ||
|
||
var options = new CassandraDbOptions | ||
{ | ||
ContactPoint = contactPoint, | ||
Keyspace = keyspace, | ||
Query = query, | ||
ConfigureClusterBuilder = configureClusterBuilder ?? defaultConfigurationAction // Use provided configuration or default | ||
}; | ||
|
||
return builder.AddCassandra(options, name, failureStatus, tags, timeout); | ||
} | ||
|
||
/// <summary> | ||
/// Add a health check for Cassandra databases using <see cref="CassandraDbOptions"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="options">Options for the Cassandra health check.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'cassandra' 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 AddCassandra( | ||
this IHealthChecksBuilder builder, | ||
CassandraDbOptions options, | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
// Ensure options.ConfigureClusterBuilder is never null | ||
if (options.ConfigureClusterBuilder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options.ConfigureClusterBuilder), "ConfigureClusterBuilder action cannot be null."); | ||
} | ||
|
||
return builder.Add(new HealthCheckRegistration( | ||
name ?? NAME, | ||
sp => new CassandraDbHealthCheck(options), | ||
failureStatus, | ||
tags, | ||
timeout)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/HealthChecks.CassandraDb/HealthChecks.CassandraDb.csproj
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CassandraCSharpDriver" Version="3.20.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Large diffs are not rendered by default.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the current approach, we would build a new instance every time the health check would be invoked. Which I expect would be bad for performance and could even lead to some resource leaks (nothing is getting disposed here).
We have changed many of our health checks similar to what I described in #2040: for all the client instances that are thread-safe and should be used as singleton, we don't create our own instances, but expect it to be resolved from the DI (so the user registers a singleton and we just resolve).
I've done some quick web search, but have failed to find dependency injection best practices for working with the C# client of CassandarDb.
Should we just expect the users to inject the cluster into the DI and solve it?
For example this is what we do for MongoDB:
We allow the users to specify a
Func<IServiceProvider, IMongoClient>
factory:AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.MongoDb/DependencyInjection/MongoDbHealthCheckBuilderExtensions.cs
Line 33 in 36bf171
when it's not specified, we just try to resolve it from the DI container:
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.MongoDb/DependencyInjection/MongoDbHealthCheckBuilderExtensions.cs
Line 51 in 36bf171