-
Notifications
You must be signed in to change notification settings - Fork 803
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
393 additions
and
0 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,45 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
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 | ||
{ | ||
var clientOptions = new SurrealDbOptionsBuilder() | ||
.FromConnectionString(_options.ConnectionString) | ||
.Build(); | ||
|
||
using var client = new SurrealDbClient(clientOptions); | ||
|
||
_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; } | ||
} |
89 changes: 89 additions & 0 deletions
89
test/HealthChecks.SurrealDb.Tests/DependencyInjection/RegistrationTests.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,89 @@ | ||
using SurrealDb.Net; | ||
|
||
namespace HealthChecks.SurrealDb.Tests.DependencyInjection; | ||
|
||
public class surrealdb_registration_should | ||
{ | ||
[Fact] | ||
public void add_health_check_when_properly_configured() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddHealthChecks() | ||
.AddSurreal("connectionstring") | ||
.Services; | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
registration.Name.ShouldBe("surrealdb"); | ||
check.ShouldBeOfType<SurrealDbHealthCheck>(); | ||
} | ||
|
||
[Fact] | ||
public void invoke_beforeOpen_when_defined() | ||
{ | ||
var services = new ServiceCollection(); | ||
bool invoked = false; | ||
const string connectionstring = "Server=http://localhost:8000;Namespace=test;Database=test;Username=root;Password=root"; | ||
void beforeOpen(ISurrealDbClient client) | ||
{ | ||
invoked = true; | ||
client.Uri.AbsoluteUri.ShouldBe("http://localhost:8000"); | ||
} | ||
services.AddHealthChecks() | ||
.AddSurreal(connectionstring, configure: beforeOpen); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
Record.ExceptionAsync(() => check.CheckHealthAsync(new HealthCheckContext())).GetAwaiter().GetResult(); | ||
invoked.ShouldBeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void add_named_health_check_when_properly_configured() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddHealthChecks() | ||
.AddSurreal("connectionstring", name: "my-surrealdb-1") | ||
.Services; | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
registration.Name.ShouldBe("my-surrealdb-1"); | ||
check.ShouldBeOfType<SurrealDbHealthCheck>(); | ||
} | ||
|
||
[Fact] | ||
public void add_health_check_with_connection_string_factory_when_properly_configured() | ||
{ | ||
var services = new ServiceCollection(); | ||
bool factoryCalled = false; | ||
services.AddHealthChecks() | ||
.AddSurreal(_ => | ||
{ | ||
factoryCalled = true; | ||
return "connectionstring"; | ||
}); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
registration.Name.ShouldBe("surrealdb"); | ||
check.ShouldBeOfType<SurrealDbHealthCheck>(); | ||
factoryCalled.ShouldBeTrue(); | ||
} | ||
} |
Oops, something went wrong.