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

Replace Cosmos Db metadata query with container query #2215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/HealthChecks.CosmosDb/AzureCosmosDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,21 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
if (_options is {DatabaseId: not null, ContainerIds: not null})
{
foreach (var containerId in _options.ContainerIds)
{
var container = _cosmosClient.GetContainer(_options.DatabaseId, containerId);
var query = new QueryDefinition($"SELECT 1 FROM {containerId}");
using var queryStream = container.GetItemQueryStreamIterator(query);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to call .ReadNextAsync() to execute the operation against the container?

}
}
await _cosmosClient.ReadAccountAsync().ConfigureAwait(false);

if (_options.DatabaseId != null)
{
var database = _cosmosClient.GetDatabase(_options.DatabaseId);
await database.ReadAsync(cancellationToken: cancellationToken).ConfigureAwait(false);

if (_options.ContainerIds != null)
{
foreach (var container in _options.ContainerIds)
{
await database
.GetContainer(container)
.ReadContainerAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
}
}

return HealthCheckResult.Healthy();
Expand Down
148 changes: 39 additions & 109 deletions test/HealthChecks.CosmosDb.Tests/CosmosDbHealthCheckTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,166 +107,96 @@ public async Task return_healthy_when_checking_healthy_service_database_and_cont
{
using var tokenSource = new CancellationTokenSource();

_cosmosClient
.ReadAccountAsync()
.Returns(Substitute.For<AccountProperties>());

_database
.ReadAsync(null, tokenSource.Token)
.Returns(Substitute.For<DatabaseResponse>());
_options.ContainerIds = ContainerIds;
_options.DatabaseId = DatabaseId;

foreach (var container in _containers.Values)
for (var containerIndex = 0; containerIndex < _options.ContainerIds.Count(); containerIndex++)
{
container
.ReadContainerAsync(null, tokenSource.Token)
.Returns(Substitute.For<ContainerResponse>());
_cosmosClient.GetContainer(_options.DatabaseId, _options.ContainerIds.ElementAt(containerIndex)).Returns(_containers[ContainerIds[containerIndex]]);
}

_options.ContainerIds = ContainerIds;
_options.DatabaseId = DatabaseId;
var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token);

await _cosmosClient
.Received(1)
.ReadAccountAsync();

await _database
.Received(1)
.ReadAsync(null, tokenSource.Token);

await Task.WhenAll(_containers
.Values
.Select(x => x
.Received(1)
.ReadContainerAsync(null, tokenSource.Token)));
for (int containerIndex = 0; containerIndex < _options.ContainerIds.Count(); containerIndex++)
{
_cosmosClient.Received(1)
.GetContainer(_options.DatabaseId, _options.ContainerIds.ElementAt(containerIndex));
_containers.ElementAt(containerIndex).Value.Received(1)
.GetItemQueryStreamIterator(Arg.Is<QueryDefinition>(q => q.QueryText.Equals($"SELECT 1 FROM {_options.ContainerIds.ElementAt(containerIndex)}")));
}

actual.Status.ShouldBe(HealthStatus.Healthy);
}

[Theory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(true, true)]
public async Task return_unhealthy_when_checking_unhealthy_service(bool checkDatabase, bool checkContainers)
[Fact]
public async Task return_unhealthy_when_checking_unhealthy_database()
{
using var tokenSource = new CancellationTokenSource();
_options.ContainerIds = ContainerIds;
_options.DatabaseId = DatabaseId;

_cosmosClient
.ReadAccountAsync()
.ThrowsAsync(new RequestFailedException((int)HttpStatusCode.Unauthorized, "Unable to authorize access."));
_cosmosClient.GetContainer(_options.DatabaseId, _options.ContainerIds.ElementAt(0)).Throws(new RequestFailedException((int)HttpStatusCode.Unauthorized, "Unable to authorize access."));

_options.ContainerIds = checkContainers ? ContainerIds : null;
_options.DatabaseId = checkDatabase ? DatabaseId : null;
var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token);

await _cosmosClient
.Received(1)
.ReadAccountAsync();

await _database
.DidNotReceiveWithAnyArgs()
.ReadAsync(default);

await Task.WhenAll(_containers
.Values
.Select(x => x
.DidNotReceiveWithAnyArgs()
.ReadContainerAsync(default, default)));

actual.Status.ShouldBe(HealthStatus.Unhealthy);
actual
.Exception!.ShouldBeOfType<RequestFailedException>()
.Status.ShouldBe((int)HttpStatusCode.Unauthorized);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task return_unhealthy_when_checking_unhealthy_database(bool checkContainers)
[Fact]
public async Task return_unhealthy_when_checking_unhealthy_container()
{
using var tokenSource = new CancellationTokenSource();

_cosmosClient
.ReadAccountAsync()
.Returns(Substitute.For<AccountProperties>());

_database
.ReadAsync(null, tokenSource.Token)
.ThrowsAsync(new RequestFailedException((int)HttpStatusCode.NotFound, "Database not found"));

_options.ContainerIds = checkContainers ? ContainerIds : null;
_options.ContainerIds = ContainerIds;
_options.DatabaseId = DatabaseId;
var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token);

await _cosmosClient
.Received(1)
.ReadAccountAsync();
_cosmosClient.GetContainer(_options.DatabaseId, _options.ContainerIds.ElementAt(0)).Returns(_containers[ContainerIds[0]]);
_containers.ElementAt(0).Value.GetItemQueryStreamIterator(Arg.Any<QueryDefinition>()).Throws(new RequestFailedException((int)HttpStatusCode.Unauthorized, "Unable to authorize access."));

await _database
.Received(1)
.ReadAsync(null, tokenSource.Token);

await Task.WhenAll(_containers
.Values
.Select(x => x
.DidNotReceiveWithAnyArgs()
.ReadContainerAsync(default, default)));
var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token);

actual.Status.ShouldBe(HealthStatus.Unhealthy);
actual
.Exception!.ShouldBeOfType<RequestFailedException>()
.Status.ShouldBe((int)HttpStatusCode.NotFound);
.Status.ShouldBe((int)HttpStatusCode.Unauthorized);
}

[Fact]
public async Task return_unhealthy_when_checking_unhealthy_container()
[Theory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(true, true)]
public async Task return_unhealthy_when_checking_unhealthy_service(bool checkDatabase, bool checkContainers)
{
using var tokenSource = new CancellationTokenSource();

_cosmosClient
.ReadAccountAsync()
.Returns(Substitute.For<AccountProperties>());

_database
.ReadAsync(null, tokenSource.Token)
.Returns(Substitute.For<DatabaseResponse>());

_containers["one"]
.ReadContainerAsync(null, tokenSource.Token)
.Returns(Substitute.For<ContainerResponse>());

_containers["two"]
.ReadContainerAsync(null, tokenSource.Token)
.ThrowsAsync(new RequestFailedException((int)HttpStatusCode.NotFound, "Container not found"));
.ThrowsAsync(new RequestFailedException((int)HttpStatusCode.Unauthorized, "Unable to authorize access."));

_options.ContainerIds = ContainerIds;
_options.DatabaseId = DatabaseId;
_options.ContainerIds = checkContainers ? ContainerIds : null;
_options.DatabaseId = checkDatabase ? DatabaseId : null;
var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token);

await _cosmosClient
.Received(1)
.ReadAccountAsync();

await _database
.Received(1)
.ReadAsync(null, tokenSource.Token);

await _containers["one"]
.Received(1)
.ReadContainerAsync(null, tokenSource.Token);

await _containers["two"]
.Received(1)
.ReadContainerAsync(null, tokenSource.Token);

await _containers["three"]
.DidNotReceiveWithAnyArgs()
.ReadContainerAsync(default, default);
.ReadAsync(default);

await Task.WhenAll(_containers
.Values
.Select(x => x
.DidNotReceiveWithAnyArgs()
.ReadContainerAsync(default, default)));

actual.Status.ShouldBe(HealthStatus.Unhealthy);
actual
.Exception!.ShouldBeOfType<RequestFailedException>()
.Status.ShouldBe((int)HttpStatusCode.NotFound);
.Status.ShouldBe((int)HttpStatusCode.Unauthorized);
}

[Fact]
Expand Down
Loading