From ee6e2217aa517d92a0ee253bdf739489a862734b Mon Sep 17 00:00:00 2001 From: Philip Reed Date: Thu, 7 Mar 2024 11:51:58 +0000 Subject: [PATCH 1/3] [feat] Extend HealthChecks to allow AzureCosmosDbHealthCheckOptions to be provided --- .../6-miscellaneous/healthchecks/_index.md | 16 +++++++++++ .../HealthChecksBuilderExtensions.cs | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/docs/cosmos-repo/content/6-miscellaneous/healthchecks/_index.md b/docs/cosmos-repo/content/6-miscellaneous/healthchecks/_index.md index 79e9e8e28..94f3c9e47 100644 --- a/docs/cosmos-repo/content/6-miscellaneous/healthchecks/_index.md +++ b/docs/cosmos-repo/content/6-miscellaneous/healthchecks/_index.md @@ -19,6 +19,22 @@ By default, this will scan all of the assemblies in your solution to locate the services.AddHealthChecks().AddCosmosRepository(assemblies: typeof(ExampleItem).Assembly); ``` +Alternatively, use the override to provide your own `AzureCosmosDbHealthCheckOptions` and configure the Containers to check: + +```csharp +services.AddHealthChecks().AddCosmosRepository(optionsFactory: sp => new AzureCosmosDbHealthCheckOptions +{ + DatabaseId = "my-database", + ContainerIds = new[] { "Container1", "Container2" } +}); +``` + +Use `optionsFactory: null` to retain the default behaviour of the [HealthChecks.CosmosDb](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/master/src/HealthChecks.CosmosDb/README.md) package, and only call `CosmosClient.ReadAccountAsync()`. + +```csharp +services.AddHealthChecks().AddCosmosRepository(optionsFactory: null); +``` + The Cosmos Repository Health package supports all of the existing functionality of Health Checks, such as failureStatus and tags, see the [Microsoft Documentation](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks) for configuration details. Don't forget to map the health endpoint with: diff --git a/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs b/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs index cc2d13db4..93a6cc34c 100644 --- a/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs +++ b/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs @@ -55,4 +55,32 @@ public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder return builder; } + + /// + /// Add a health check for Azure Cosmos DB by registering for given . + /// + /// The to add to. + /// The health check name. Optional. If null the name 'azure_cosmosdb' will be used. + /// + /// The that should be reported when the health check fails. Optional. If null then + /// the default status of will be reported. + /// + /// A list of tags that can be used to filter sets of health checks. Optional. + /// An optional representing the timeout of the check. + /// The `AzureCosmosDbHealthCheckOptions` to use. Optional. If null, the health check just calls CosmosClient.ReadAccountAsync. + /// The specified . + public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder builder, + string? healthCheckName = "azure_cosmosdb", + HealthStatus? failureStatus = default, + IEnumerable? tags = default, + TimeSpan? timeout = default, + Func? optionsFactory = default + ) + { + builder.AddAzureCosmosDB( + clientFactory: provider => provider.GetRequiredService().CosmosClient, + optionsFactory: optionsFactory, healthCheckName, failureStatus, tags, timeout); + + return builder; + } } \ No newline at end of file From 48eb8ac3a701de8e778cb51948c16418c8fc7066 Mon Sep 17 00:00:00 2001 From: Philip Reed Date: Thu, 7 Mar 2024 13:57:50 +0000 Subject: [PATCH 2/3] update xml comments --- .../Extensions/HealthChecksBuilderExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs b/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs index 93a6cc34c..e5c546207 100644 --- a/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs +++ b/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs @@ -17,7 +17,7 @@ namespace Microsoft.Azure.CosmosRepository.AspNetCore.Extensions; public static class HealthChecksBuilderExtensions { /// - /// Add a health check for Azure Cosmos DB by registering for given . + /// Add a health check for Azure Cosmos DB by registering for given Use this overload to automatically scan assemblies for configured containerIds. /// /// The to add to. /// The health check name. Optional. If null the name 'azure_cosmosdb' will be used. @@ -57,7 +57,7 @@ public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder } /// - /// Add a health check for Azure Cosmos DB by registering for given . + /// Add a health check for Azure Cosmos DB by registering for given . Use this overload to configure to customise which containers to check. /// /// The to add to. /// The health check name. Optional. If null the name 'azure_cosmosdb' will be used. @@ -67,7 +67,7 @@ public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder /// /// A list of tags that can be used to filter sets of health checks. Optional. /// An optional representing the timeout of the check. - /// The `AzureCosmosDbHealthCheckOptions` to use. Optional. If null, the health check just calls CosmosClient.ReadAccountAsync. + /// The `AzureCosmosDbHealthCheckOptions` to use. Optional. If null, the health check only calls CosmosClient.ReadAccountAsync. /// The specified . public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder builder, string? healthCheckName = "azure_cosmosdb", From faf95a9d456bb048c700b9525b09a1f6c4cd3aa8 Mon Sep 17 00:00:00 2001 From: Philip Reed Date: Thu, 7 Mar 2024 14:02:20 +0000 Subject: [PATCH 3/3] fix bad xml comment ref --- .../Extensions/HealthChecksBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs b/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs index e5c546207..1ced7cdc7 100644 --- a/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs +++ b/src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs @@ -57,7 +57,7 @@ public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder } /// - /// Add a health check for Azure Cosmos DB by registering for given . Use this overload to configure to customise which containers to check. + /// Add a health check for Azure Cosmos DB by registering for given . Use this overload to configure to customise which containers to check. /// /// The to add to. /// The health check name. Optional. If null the name 'azure_cosmosdb' will be used.