forked from Xabaril/AspNetCore.Diagnostics.HealthChecks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added healtcheck package for Hazelcast Xabaril#810
- Loading branch information
Showing
10 changed files
with
298 additions
and
15 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,6 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="NuGet.org" value="https://api.nuget.org/v3/index.json" /> | ||
<add key="NugetorgProxy" value="https://nexus.cariprpc.it/repository/nuget.org-proxy/index.json" /> | ||
</packageSources> | ||
</configuration> |
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
73 changes: 73 additions & 0 deletions
73
src/HealthChecks.Hazelcast/DependencyInjection/HazelcastHealthCheckBuilderExtensions.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,73 @@ | ||
using HealthChecks.Hazelcast; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to configure <see cref="HazelcastHealthCheck"/>. | ||
/// </summary> | ||
public static class HazelcastHealthCheckBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Add a health check for Hazelcast. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="setup">Action to configure <see cref="HazelcastHealthCheckOptions"/></param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'hazelcast' 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></returns> | ||
public static IHealthChecksBuilder AddHazelcast( | ||
this IHealthChecksBuilder builder, | ||
Action<HazelcastHealthCheckOptions> setup, | ||
string? name = default, | ||
HealthStatus failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
var options = new HazelcastHealthCheckOptions(); | ||
setup?.Invoke(options); | ||
|
||
return builder.Add(new HealthCheckRegistration( | ||
name ?? "hazelcast", | ||
_ => new HazelcastHealthCheck(options), | ||
failureStatus, | ||
tags, | ||
timeout)); | ||
} | ||
|
||
/// <summary> | ||
/// Add a health check for Hazelcast. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="options">Options for health check.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'hazelcast' 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></returns> | ||
public static IHealthChecksBuilder AddHazelcast( | ||
this IHealthChecksBuilder builder, | ||
HazelcastHealthCheckOptions options, | ||
string? name = default, | ||
HealthStatus failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
Guard.ThrowIfNull(options); | ||
|
||
return builder.Add(new HealthCheckRegistration( | ||
name ?? "hazelcast", | ||
_ => new HazelcastHealthCheck(options), | ||
failureStatus, | ||
tags, | ||
timeout)); | ||
} | ||
} |
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,50 @@ | ||
using Hazelcast; | ||
using Hazelcast.Networking; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.Hazelcast; | ||
|
||
/// <summary> | ||
/// A health check for Hazelcast services. | ||
/// </summary> | ||
public class HazelcastHealthCheck : IHealthCheck | ||
{ | ||
private readonly HazelcastHealthCheckOptions _options; | ||
|
||
public HazelcastHealthCheck(HazelcastHealthCheckOptions options) | ||
{ | ||
_ = Guard.ThrowIfNull(options); | ||
_ = Guard.ThrowIfNull(options.ConnectionHost, true); | ||
|
||
_options = options; | ||
if (string.IsNullOrEmpty(_options.ClientName)) | ||
{ | ||
_options.ClientName = $"HazelcastHealthcheck_{Environment.MachineName}"; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await using var client = await HazelcastClientFactory.StartNewClientAsync(options => | ||
{ | ||
options.Networking.Addresses.Add(_options.ConnectionHost); | ||
options.Networking.ReconnectMode = ReconnectMode.DoNotReconnect; | ||
options.Networking.ConnectionTimeoutMilliseconds = (int)_options.ConnectionTimeout.TotalMilliseconds; | ||
|
||
options.ClusterName = _options.ClusterName; | ||
options.ClientName = _options.ClientName; | ||
}, cancellationToken).ConfigureAwait(false); | ||
|
||
return client.IsActive && client.IsConnected | ||
? HealthCheckResult.Healthy() | ||
: new HealthCheckResult(context.Registration.FailureStatus, description: $"Hazelcast client connection failed: {client.State}"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
} |
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,27 @@ | ||
namespace HealthChecks.Hazelcast; | ||
|
||
/// <summary> | ||
/// Options for <see cref="HazelcastHealthCheck"/>. | ||
/// </summary> | ||
public class HazelcastHealthCheckOptions | ||
{ | ||
/// <summary> | ||
/// Hazelcast connection host ({ip/DNS}:{port}) | ||
/// </summary> | ||
public string ConnectionHost { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Hazelcast cluster name to connect | ||
/// </summary> | ||
public string ClusterName { get; set; } = "dev"; | ||
|
||
/// <summary> | ||
/// Hazelcast connection client name (use to track the healthcheck connection to your Hazelcast) | ||
/// </summary> | ||
public string ClientName { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Hazelcast client connection timeout | ||
/// </summary> | ||
public TimeSpan ConnectionTimeout { get; set; } = TimeSpan.FromMilliseconds(100); | ||
} |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<PackageTags>$(PackageTags);Hazelcast</PackageTags> | ||
<Description>HealthChecks.Hazelcast is the health check package for Hazelcast.</Description> | ||
<VersionPrefix>$(HealthCheckHazelcast)</VersionPrefix> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Hazelcast.Net" Version="5.3.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.9" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.