Xabaril / AspNetCore.Diagnostics.HealthChecks

Enterprise HealthChecks for ASP.NET Core Diagnostics Package
Apache License 2.0
3.98k stars 770 forks source link

Support for MongoDBStorage #2223

Open officeSpacePirate opened 1 month ago

officeSpacePirate commented 1 month ago

What would you like to be added:

Add support for MongoDB as a storage option for HealthCheck history.

Why is this needed:

Currently, your solution only supports relational databases or an InMemoryCache to store HealthCheck history and the latter poses an obvious issue for application instances that change often. Adding Mongo support likely addresses the largest demographic of teams using non-relational databases.

Note: With .NET v8.0+ and MongoDB.Driver v2.25.0+ as prerequisites, Mongo has recently released MongoDB.EntityFrameworkCore. This hopefully means you could support the feature in a similar fashion to your current architecture.

officeSpacePirate commented 1 month ago

I attempted to add a HealthChecksUIBuilderExtensions.cs to a service directly like so:

using HealthChecks.UI.Data;
using MongoDB.EntityFrameworkCore.Infrastructure;

namespace Microsoft.Extensions.DependencyInjection;

public static class HealthChecksUIBuilderExtensions
{
    public static HealthChecksUIBuilder AddMongoStorage(
        this HealthChecksUIBuilder builder,
        string connectionString,
        string databaseName,
        Action<MongoDbContextOptionsBuilder>? configureMongoOptions = null)
    {
        builder.Services.AddMongoDB<HealthChecksDb>(connectionString, databaseName, options =>
        {
            configureMongoOptions?.Invoke(options);
        });

        return builder;
    }
}

and... Mongo is throwing an exception:

System.NotSupportedException: Unsupported shadow property 'HealthCheckExecutionId' identified on entity type 'HealthCheckExecutionEntry'.

I checked their repository and it looks like EF shadow properties are not supported yet. Unless there is another way to achieve this .Where() in UIApiEndpointMiddleware.cs without using EF shadow properties (which likely doesn't make sense to change anyway), this will likely have to wait until Mongo adds support for them.