From a99570e3265e421ce054f01ed3d435fedec266d2 Mon Sep 17 00:00:00 2001 From: Phil Reed Date: Thu, 7 Mar 2024 14:07:42 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20Extend=20HealthChecks=20to=20allow=20Az?= =?UTF-8?q?ureCosmosDbHealthCheckOptions=20t=E2=80=A6=20(#418)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [feat] Extend HealthChecks to allow AzureCosmosDbHealthCheckOptions to be provided * update xml comments * fix bad xml comment ref --------- Co-authored-by: Philip Reed --- .../6-miscellaneous/healthchecks/_index.md | 16 ++++++++++ .../HealthChecksBuilderExtensions.cs | 30 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) 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..1ced7cdc7 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. @@ -55,4 +55,32 @@ public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder return builder; } + + /// + /// 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. + /// + /// 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 only 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