-
Notifications
You must be signed in to change notification settings - Fork 797
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
11 changed files
with
401 additions
and
13 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
111 changes: 111 additions & 0 deletions
111
src/HealthChecks.SurrealDb/DependencyInjection/SurrealDbHealthCheckBuilderExtensions.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,111 @@ | ||
using HealthChecks.SurrealDb; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using SurrealDb.Net; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to configure <see cref="SurrealDbHealthCheck"/>. | ||
/// </summary> | ||
public static class SurrealDbHealthCheckBuilderExtensions | ||
{ | ||
private const string NAME = "surrealdb"; | ||
|
||
/// <summary> | ||
/// Add a health check for SurrealDB. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="connectionString">The SurrealDB connection string to be used.</param> | ||
/// <param name="configure">An optional action to allow additional SQL Server specific configuration.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'surrealdb' 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 AddSurreal( | ||
this IHealthChecksBuilder builder, | ||
string connectionString, | ||
Action<ISurrealDbClient>? configure = null, | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
return builder.AddSurreal(_ => connectionString, configure, name, failureStatus, tags, timeout); | ||
} | ||
|
||
/// <summary> | ||
/// Add a health check for SurrealDB. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="connectionStringFactory">A factory to build the SurrealDB connection string to use.</param> | ||
/// <param name="configure">An optional action to allow additional SQL Server specific configuration.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'surrealdb' 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 AddSurreal( | ||
this IHealthChecksBuilder builder, | ||
Func<IServiceProvider, string> connectionStringFactory, | ||
Action<ISurrealDbClient>? configure = null, | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
Guard.ThrowIfNull(connectionStringFactory); | ||
|
||
return builder.Add(new HealthCheckRegistration( | ||
name ?? NAME, | ||
sp => | ||
{ | ||
var options = new SurrealDbHealthCheckOptions | ||
{ | ||
ConnectionString = connectionStringFactory(sp), | ||
Configure = configure, | ||
}; | ||
return new SurrealDbHealthCheck(options); | ||
}, | ||
failureStatus, | ||
tags, | ||
timeout)); | ||
} | ||
|
||
/// <summary> | ||
/// Add a health check for SurrealDB. | ||
/// </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 'surrealdb' 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 AddSurreal( | ||
this IHealthChecksBuilder builder, | ||
SurrealDbHealthCheckOptions options, | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
Guard.ThrowIfNull(options); | ||
|
||
return builder.Add(new HealthCheckRegistration( | ||
name ?? NAME, | ||
_ => new SurrealDbHealthCheck(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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<PackageTags>$(PackageTags);SurrealDb</PackageTags> | ||
<Description>HealthChecks.SurrealDb is the health check package for SurrealDB.</Description> | ||
<VersionPrefix>$(HealthCheckSurrealDb)</VersionPrefix> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.9" /> | ||
<PackageReference Include="SurrealDb.Net" Version="0.1.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,40 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using SurrealDb.Net; | ||
|
||
namespace HealthChecks.SurrealDb; | ||
|
||
/// <summary> | ||
/// A health check for SurrealDb services. | ||
/// </summary> | ||
public class SurrealDbHealthCheck : IHealthCheck | ||
{ | ||
private readonly SurrealDbHealthCheckOptions _options; | ||
|
||
public SurrealDbHealthCheck(SurrealDbHealthCheckOptions options) | ||
{ | ||
Guard.ThrowIfNull(options.ConnectionString, true); | ||
_options = options; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using var client = new SurrealDbClient(_options.ConnectionString); | ||
|
||
_options.Configure?.Invoke(client); | ||
await client.Connect(cancellationToken).ConfigureAwait(false); | ||
|
||
bool result = await client.Health(cancellationToken).ConfigureAwait(false); | ||
|
||
return _options.HealthCheckResultBuilder == null | ||
? HealthCheckResult.Healthy() | ||
: _options.HealthCheckResultBuilder(result); | ||
} | ||
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,25 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using SurrealDb.Net; | ||
|
||
namespace HealthChecks.SurrealDb; | ||
|
||
/// <summary> | ||
/// Options for <see cref="SurrealDbHealthCheck"/>. | ||
/// </summary> | ||
public class SurrealDbHealthCheckOptions | ||
{ | ||
/// <summary> | ||
/// The Sql Server connection string to be used. | ||
/// </summary> | ||
public string ConnectionString { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// An optional action executed before the connection is opened in the health check. | ||
/// </summary> | ||
public Action<ISurrealDbClient>? Configure { get; set; } | ||
|
||
/// <summary> | ||
/// An optional delegate to build health check result. | ||
/// </summary> | ||
public Func<object?, HealthCheckResult>? HealthCheckResultBuilder { get; set; } | ||
} |
Oops, something went wrong.