Skip to content
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

CosmosDbHealthCheck - consider registration overloads that accept a CosmosClient instance #1384

Closed
danielmpetrov opened this issue Aug 8, 2022 · 9 comments · Fixed by #1419

Comments

@danielmpetrov
Copy link
Contributor

danielmpetrov commented Aug 8, 2022

Hi. I appreciate the library and your effort. 👍

What would you like to be added:

I would like to see an overload for CosmosDb health checks that accepts a CosmosClient instance.

Why is this needed:

According to the official Cosmos SDK v3 documentation (see https://docs.microsoft.com/en-us/azure/cosmos-db/sql/performance-tips-dotnet-sdk-v3-sql#sdk-usage), the CosmosClient was designed to be singleton for optimal performance. However, the library does not expose a dependency injection overload that accepts a client, but instead internally creates and maintains a new client for each health check - https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/master/src/HealthChecks.CosmosDb/CosmosDbHealthCheck.cs#L58.

This can cause a frustrating developer experience when reading cosmos diagnostic logs and seeing more client instances than expected.

@sungam3r
Copy link
Collaborator

sungam3r commented Aug 9, 2022

instead internally creates and maintains a new client for each health check

Not true. Clients are cached in static dictionary by connection.

@danielmpetrov
Copy link
Contributor Author

danielmpetrov commented Aug 9, 2022

You're right, it's a new client per connection. The main point still stands, that is an additional client per connection, which is not ideal. Would you be open to a pull request?

@sungam3r
Copy link
Collaborator

I don't understand yet how it can be client not per connection if CosmosClient constructor accepts connectionString argument 😕 .

@danielmpetrov
Copy link
Contributor Author

danielmpetrov commented Aug 10, 2022

It cannot be, but the app connecting to cosmos db must create a client for business operations, saving, updating, deleting records, etc. The health check then also creates a client, to ping the database. The end result in two clients in the same application for the same connection string.

@sungam3r
Copy link
Collaborator

Understood.

I would like to see an overload for CosmosDb health checks that accepts a CosmosClient instance.

Sure. PR is welcome.

@danielmpetrov
Copy link
Contributor Author

Good news, #1405 has added the exact API that we needed. 🎉

Thank you @wsugarman for the contribution. 👍

@wsugarman
Copy link
Contributor

@danielmpetrov Haha! Happy to (inadvertently) help!

@danielmpetrov
Copy link
Contributor Author

danielmpetrov commented Aug 25, 2022

It's a really good change, but registration methods that directly accept a client will still be beneficial in cases where we have apps that reach out to multiple cosmos accounts.

Code sample:

var client1 = new CosmosClient(connection1);
var client2 = new CosmosClient(connection2);

// ...

services
    .AddHealthChecks()
    .AddCosmosDb(client1, name: "sample 1")
    .AddCosmosDb(client2, name: "sample 2");

In this case we don't directly register the clients in the container because they share a type, so the overloads from #1405 won't work. However, that PR sets this change to be a very simple one, since the health check now has the appropriate constructor overloads.

I could add the registration methods if this still makes sense to everyone. I think this will give the user the ultimate flexibility.

@danielmpetrov danielmpetrov reopened this Aug 25, 2022
@danielmpetrov danielmpetrov changed the title CosmosDbHealthCheck - consider a constructor overload that accepts a CosmosClient instance CosmosDbHealthCheck - consider registration overloads that accept a CosmosClient instance Aug 25, 2022
@sungam3r
Copy link
Collaborator

PR is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants