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
26 changes: 18 additions & 8 deletions src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,40 @@ namespace HealthChecks.DocumentDb;
public class DocumentDbHealthCheck : IHealthCheck
{
private static readonly ConcurrentDictionary<string, DocumentClient> _connections = new();
private readonly DocumentDbOptions _documentDbOptions = new();
private readonly DocumentDbOptions _options;

public DocumentDbHealthCheck(DocumentDbOptions documentDbOptions)
{
_documentDbOptions.UriEndpoint = Guard.ThrowIfNull(documentDbOptions.UriEndpoint);
_documentDbOptions.PrimaryKey = Guard.ThrowIfNull(documentDbOptions.PrimaryKey);
Guard.ThrowIfNull(documentDbOptions.UriEndpoint);
Guard.ThrowIfNull(documentDbOptions.PrimaryKey);

_options = documentDbOptions;
}

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
if (!_connections.TryGetValue(_documentDbOptions.UriEndpoint, out var documentDbClient))
if (!_connections.TryGetValue(_options.UriEndpoint, out var documentDbClient))
{
documentDbClient = new DocumentClient(new Uri(_documentDbOptions.UriEndpoint), _documentDbOptions.PrimaryKey);
documentDbClient = new DocumentClient(new Uri(_options.UriEndpoint), _options.PrimaryKey);

if (!_connections.TryAdd(_documentDbOptions.UriEndpoint, documentDbClient))
if (!_connections.TryAdd(_options.UriEndpoint, documentDbClient))
{
documentDbClient.Dispose();
documentDbClient = _connections[_documentDbOptions.UriEndpoint];
documentDbClient = _connections[_options.UriEndpoint];
}
}
await documentDbClient.OpenAsync(cancellationToken).ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(_options.DatabaseName) && !string.IsNullOrWhiteSpace(_options.CollectionName))
{
await documentDbClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_options.DatabaseName, _options.CollectionName)).ConfigureAwait(false);
}
else
{
await documentDbClient.OpenAsync(cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
}
Expand Down
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; }

public string? CollectionName { get; set; }
}
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; }
public string PrimaryKey { get; set; }
public string UriEndpoint { get; set; }
}
Expand Down