Skip to content
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

DocumentDb - Add option to check database and collection #1985

Merged
merged 9 commits into from
Aug 7, 2023
2 changes: 1 addition & 1 deletion build/versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<HealthCheckConsul>7.0.0</HealthCheckConsul>
<HealthCheckCosmosDb>7.0.0</HealthCheckCosmosDb>
<HealthCheckDapr>7.0.0</HealthCheckDapr>
<HealthCheckDocumentDb>7.0.0</HealthCheckDocumentDb>
<HealthCheckDocumentDb>7.1.0</HealthCheckDocumentDb>
<HealthCheckDynamoDb>7.0.0</HealthCheckDynamoDb>
<HealthCheckElasticsearch>7.0.0</HealthCheckElasticsearch>
<HealthCheckEventStoregRPC>7.0.0</HealthCheckEventStoregRPC>
Expand Down
24 changes: 23 additions & 1 deletion src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public DocumentDbHealthCheck(DocumentDbOptions documentDbOptions)
{
_documentDbOptions.UriEndpoint = Guard.ThrowIfNull(documentDbOptions.UriEndpoint);
_documentDbOptions.PrimaryKey = Guard.ThrowIfNull(documentDbOptions.PrimaryKey);
_documentDbOptions.DatabaseName = documentDbOptions.DatabaseName;
cieciurm marked this conversation as resolved.
Show resolved Hide resolved
_documentDbOptions.CollectionName = documentDbOptions.CollectionName;
}

/// <inheritdoc />
Expand All @@ -30,7 +32,15 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
documentDbClient = _connections[_documentDbOptions.UriEndpoint];
}
}
await documentDbClient.OpenAsync(cancellationToken).ConfigureAwait(false);

if (CanCheckCollectionInDatabase())
cieciurm marked this conversation as resolved.
Show resolved Hide resolved
{
await documentDbClient.ReadDocumentCollectionAsync(GetCollectionUri()).ConfigureAwait(false);
cieciurm marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
await documentDbClient.OpenAsync(cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
}
Expand All @@ -39,4 +49,16 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}

private bool CanCheckCollectionInDatabase()
{
if (string.IsNullOrWhiteSpace(_documentDbOptions.DatabaseName) || string.IsNullOrWhiteSpace(_documentDbOptions.CollectionName))
{
return false;
}

return true;
}

private Uri GetCollectionUri() => UriFactory.CreateDocumentCollectionUri(_documentDbOptions.DatabaseName, _documentDbOptions.CollectionName);
cieciurm marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions src/HealthChecks.DocumentDb/DocumentDbOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class DocumentDbOptions
public string UriEndpoint { get; set; } = null!;

public string PrimaryKey { get; set; } = null!;

public string DatabaseName { get; set; } = null!;

public string CollectionName { get; set; } = null!;
cieciurm marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void add_health_check_when_properly_configured()
registration.Name.ShouldBe("documentdb");
check.ShouldBeOfType<DocumentDbHealthCheck>();
}

[Fact]
public void add_named_health_check_when_properly_configured()
{
Expand All @@ -34,4 +35,27 @@ public void add_named_health_check_when_properly_configured()
registration.Name.ShouldBe("my-documentdb-group");
check.ShouldBeOfType<DocumentDbHealthCheck>();
}

[Fact]
public void add_health_check_when_properly_configured_with_database_name_and_collection_name()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddDocumentDb(_ =>
{
_.PrimaryKey = "key";
_.UriEndpoint = "endpoint";
_.DatabaseName = "database";
_.CollectionName = "collection";
});

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("documentdb");
check.ShouldBeOfType<DocumentDbHealthCheck>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace HealthChecks.DocumentDb
public class DocumentDbOptions
{
public DocumentDbOptions() { }
public string CollectionName { get; set; }
public string DatabaseName { get; set; }
cieciurm marked this conversation as resolved.
Show resolved Hide resolved
public string PrimaryKey { get; set; }
public string UriEndpoint { get; set; }
}
Expand Down