forked from loic-sharma/BaGet
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add database health check * test: Clear health check options when the application under test is constructed * feat(Health check): Make overall status property name configurable * docs: Describe expanded health check endpoint
- Loading branch information
1 parent
440f930
commit 872afce
Showing
8 changed files
with
125 additions
and
26 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace BaGetter.Core.Extensions; | ||
|
||
public static class HealthCheckExtensions | ||
{ | ||
private static readonly JsonSerializerOptions SerializerOptions = new() | ||
{ | ||
WriteIndented = true, | ||
Converters = { new JsonStringEnumConverter() } | ||
}; | ||
|
||
/// <summary> | ||
/// Formats the <see cref="HealthReport"/> as JSON and writes it to the specified <see cref="Stream"/>. | ||
/// </summary> | ||
/// <param name="report">The report to format.</param> | ||
/// <param name="stream">A writable stream to write the report to. Will not be closed.</param> | ||
/// <param name="detailedReport">Whether to include detailed information about each health check.</param> | ||
/// <param name="statusPropertyName">The name of the property that will contain the overall status.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>A <see cref="Task"/> completing when the report is completely written to the stream.</returns> | ||
public static async Task FormatAsJson(this HealthReport report, Stream stream, bool detailedReport, string statusPropertyName = "Status", | ||
CancellationToken cancellationToken = default) | ||
{ | ||
// Always include the overall status. | ||
IEnumerable<(string Key, HealthStatus Value)> entries = [(statusPropertyName, report.Status)]; | ||
|
||
// Include details if requested. | ||
if (detailedReport) | ||
{ | ||
entries = entries.Concat(report.Entries.Select(entry => (entry.Key, entry.Value.Status))); | ||
} | ||
|
||
await JsonSerializer.SerializeAsync( | ||
stream, | ||
entries.ToDictionary(entry => entry.Key, entry => entry.Value), | ||
SerializerOptions, | ||
cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Determine whether a health check is configured for BaGetter. | ||
/// </summary> | ||
/// <param name="check">The <see cref="HealthCheckRegistration"/>.</param> | ||
/// <param name="options">The current BaGetter configuration. Will be checked for configured services.</param> | ||
/// <returns>A boolean representing whether the given check is configured in this BaGetter instance.</returns> | ||
public static bool IsConfigured(this HealthCheckRegistration check, BaGetterOptions options) | ||
{ | ||
return check.Tags.Count == 0 || // General checks | ||
check.Tags.Contains(options.Database.Type) || // Database check | ||
check.Tags.Contains(options.Storage.Type) || // Storage check | ||
check.Tags.Contains(options.Search.Type); // Search check | ||
} | ||
} |
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