-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
[feat] Add health checks #377
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
18505f1
[feat] Add AzureCosmosDB HealthChecks
next-phil 42268a9
[feat] Add overloads to configure health check options
next-phil 0050956
[tests] Add DefaultCosmosItemConfigurationProvider test
next-phil 2ec1af2
Add docs
next-phil c1eed6e
Update docs
next-phil cff302b
Merge branch 'main' into addHealthChecks
next-phil 068bbae
[chore] fix merge conflicts
next-phil 58968b2
[chore] fix failing build
next-phil b41e588
fix: use framework reference instaed of seperate packages from aspnet…
mumby0168 a8cf0e5
chore: fix failing test
next-phil 3ccc1cf
chore: fix failing test
next-phil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
docs/cosmos-repo/content/6-miscellaneous/healthchecks/_index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: "Health Checks" | ||
weight: 2 | ||
--- | ||
|
||
The `IEvangelist.Azure.CosmosRepository.AspNetCore` package adds support for [AspNet Core Health Checks](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks) using the [HealthChecks.CosmosDb](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/master/src/HealthChecks.CosmosDb/README.md) package. | ||
|
||
## Setup | ||
|
||
To configure Cosmos DB health checks: | ||
|
||
```csharp | ||
services.AddHealthChecks().AddCosmosRepository(); | ||
``` | ||
|
||
By default, this will scan all of the assemblies in your solution to locate the container names for each of your `IItem` types. To refine this, and potentially reduce startup times, pass in the Assemblies containing your `IItem` types: | ||
|
||
```csharp | ||
services.AddHealthChecks().AddCosmosRepository(assemblies: typeof(ExampleItem).Assembly); | ||
``` | ||
|
||
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: | ||
|
||
```csharp | ||
app.MapHealthChecks("/healthz"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Microsoft.Azure.CosmosRepository.AspNetCore/Extensions/HealthChecksBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using HealthChecks.CosmosDb; | ||
using Microsoft.Azure.CosmosRepository.Options; | ||
using Microsoft.Azure.CosmosRepository.Providers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.CosmosRepository.AspNetCore.Extensions; | ||
|
||
public static class HealthChecksBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Add a health check for Azure Cosmos DB by registering <see cref="AzureCosmosDbHealthCheck"/> for given <paramref name="builder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/> to add <see cref="HealthCheckRegistration"/> to.</param> | ||
/// <param name="healthCheckName">The health check name. Optional. If <c>null</c> the name 'azure_cosmosdb' will be used.</param> | ||
/// <param name="failureStatus"> | ||
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then | ||
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported. | ||
/// </param> | ||
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param> | ||
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param> | ||
/// <param name="assemblies">The assemblies to scan for <see cref="IItem"/> types. Optional. If <c>null</c> types are discovered autimatically. Providing a assemblies to scan may reduce start up time.</param> | ||
/// <returns>The specified <paramref name="builder"/>.</returns> | ||
public static IHealthChecksBuilder AddCosmosRepository(this IHealthChecksBuilder builder, | ||
string? healthCheckName = "azure_cosmosdb", | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default, | ||
params Assembly[]? assemblies) | ||
{ | ||
builder.AddAzureCosmosDB( | ||
clientFactory: provider => provider.GetRequiredService<ICosmosClientProvider>().CosmosClient, | ||
optionsFactory: sp => | ||
{ | ||
IOptions<RepositoryOptions> options = sp.GetRequiredService<IOptions<RepositoryOptions>>(); | ||
ICosmosItemConfigurationProvider itemConfigProvider = | ||
sp.GetRequiredService<ICosmosItemConfigurationProvider>(); | ||
IEnumerable<string> containers = itemConfigProvider.GetAllItemConfigurations(assemblies) | ||
.Select(i => i.ContainerName).Distinct(); | ||
|
||
return new AzureCosmosDbHealthCheckOptions | ||
{ | ||
DatabaseId = options.Value.DatabaseId, | ||
ContainerIds = containers | ||
}; | ||
}, healthCheckName, failureStatus, tags, timeout); | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.