1

I am unable to add the Mongo Atlas health check in dot net core 3.1 using the AspNetCore.HealthChecks.MongoDb nuget package. Added below code into the startup.cs

services.AddHealthChecks().AddMongoDb("MongoDbContext");

endpoints.MapHealthChecks("/api/v1.0/health", new HealthCheckOptions()

When I hit the health URL, it is giving an exception as below

"Status": "Unhealthy",
    "Description": null,
    "Exception": "MongoDB.Driver.MongoCommandException: Command listCollections failed: not authorized on test to execute command

2 Answers 2

2

when you have all the configuration stuff done, you should try with adding first the health check definition for MongoDB:

public void ConfigureServices (IServiceCollection services) {
    services.AddControllers ();

    string mongoDBConnection = Configuration.GetValue ("mongoDB:connection");

    services.AddHealthChecks ()
        .AddMongoDb (mongodbConnectionString: mongoDBConnection,
            name: "todo-db-check",
            failureStatus : HealthStatus.Unhealthy,
            tags : new string[] { "todo-api", "mongodb" });
}

Also you need to add connection string info for MongoDB in the app settings file.

"mongoDB:connection": "mongodb://localhost:27017"

Then, add the health check with “/hc” path to the request pipeline.

app.UseEndpoints (endpoints => {
    endpoints.MapControllers ();
    endpoints.MapHealthChecks ("/hc");
});

Now you can run the API and hit the “/hc” endpoint via browser, and it should be in healthy status with its database.

You can also see the documentation for MongoDB (https://docs.mongodb.com/manual/), and and for that specific issue, you should take a look at these pages, to make it clearer.

(https://rmauro.dev/adding-health-checks-to-net-core-application/)

(https://www.gokhan-gokalp.com/en/aspnet-core-series-06-monitor-the-health-of-your-applications-by-implementing-health-checks-and-azure-application-insights/)

1
  • I already have this implementation in place and it is working fine with mongo DB health check. I am having issues with Mongo Atlas health check. Commented Aug 31, 2020 at 7:15
0

Looking at the source code on github

if (!string.IsNullOrEmpty(_specifiedDatabase))
{
    // some users can't list all databases depending on database privileges, with
    // this you can list only collections on specified database.
    // Related with issue #43

    using var cursor = await mongoClient
        .GetDatabase(_specifiedDatabase)
        .ListCollectionNamesAsync(cancellationToken: cancellationToken);
    await cursor.FirstAsync(cancellationToken);
}

the problem seems to be that you don't have enough privileges in Atlas for listing the collections on your database "test"

Not the answer you're looking for? Browse other questions tagged or ask your own question.