Skip to content

Commit

Permalink
Add ability to execute surql query
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Nov 25, 2023
1 parent b969ed0 commit 458e4dd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class SurrealDbHealthCheckBuilderExtensions
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="connectionString">The SurrealDB connection string to be used.</param>
/// <param name="healthQuery">The query to be executed.</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">
Expand All @@ -28,20 +29,22 @@ public static class SurrealDbHealthCheckBuilderExtensions
public static IHealthChecksBuilder AddSurreal(
this IHealthChecksBuilder builder,
string connectionString,
string? healthQuery = null,
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);
return builder.AddSurreal(_ => connectionString, healthQuery, 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="healthQuery">The query to be executed.</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">
Expand All @@ -54,6 +57,7 @@ public static IHealthChecksBuilder AddSurreal(
public static IHealthChecksBuilder AddSurreal(
this IHealthChecksBuilder builder,
Func<IServiceProvider, string> connectionStringFactory,
string? healthQuery = null,
Action<ISurrealDbClient>? configure = null,
string? name = default,
HealthStatus? failureStatus = default,
Expand All @@ -69,6 +73,7 @@ public static IHealthChecksBuilder AddSurreal(
var options = new SurrealDbHealthCheckOptions
{
ConnectionString = connectionStringFactory(sp),
Query = healthQuery,
Configure = configure,
};
return new SurrealDbHealthCheck(options);
Expand Down
5 changes: 5 additions & 0 deletions src/HealthChecks.SurrealDb/SurrealDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context

bool result = await client.Health(cancellationToken).ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(_options.Query))
{
await client.Query(_options.Query).ConfigureAwait(false);
}

return _options.HealthCheckResultBuilder == null
? HealthCheckResult.Healthy()
: _options.HealthCheckResultBuilder(result);
Expand Down
5 changes: 5 additions & 0 deletions src/HealthChecks.SurrealDb/SurrealDbHealthCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class SurrealDbHealthCheckOptions
/// </summary>
public string ConnectionString { get; set; } = null!;

/// <summary>
/// The query to be executed.
/// </summary>
public string? Query { get; set; }

/// <summary>
/// An optional action executed before the connection is opened in the health check.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,39 @@ public async Task be_healthy_if_surrealdb_is_available()
[Fact]
public async Task be_unhealthy_if_surrealdb_is_not_available()
{
var connectionString = "Server=http://200.0.0.100:1234;Namespace=test;Database=test;Username=root;Password=root";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddSurreal(connectionString, tags: new string[] { "surrealdb" });
})
.Configure(app =>
{
app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("surrealdb")
});
});

using var server = new TestServer(webHostBuilder);

using var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}

[Fact]
public async Task be_unhealthy_if_surql_query_throw_error()
{
var connectionString = "Server=tcp:localhost,5433;Initial Catalog=master;User Id=sa;Password=Password12!;Encrypt=false";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddSurreal("Server=http://200.0.0.100:1234;Namespace=test;Database=test;Username=root;Password=root", tags: new string[] { "surrealdb" });
.AddSurreal(connectionString, healthQuery: "THROW \"Error\";", tags: new string[] { "surrealdb" });
})
.Configure(app =>
{
Expand Down

0 comments on commit 458e4dd

Please sign in to comment.