You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe that it leads to having at least two instances of the client created:
one used by the app outside of the health chekc
one used by the health check.
This is bad for perf, but also in some rare edge cases can potentially lead into a situation when the client used by the app is disconnected, but the client used by the HC is connected and HC returns "Healthy".
Moreover, caching is hard and having a custom cache can always lead into bugs that are hard to diagnose. And increased complexity that needs to be maintained.
I would like to propose following changes for health checks that use clients that should be used as singletons:
each HC should expose only one constructor that takes an existing instance of the client (this forces the user to create them, takes complexity away from us).
removal of the static cache (less complexity, better size on disk, smaller chance for bugs)
each HC should expose only one DI extension method. The method should by default resolve an instance of the client from IServiceProvider, but let the user override this behaviour.
This would let us steer the users towards best practices:
the users are responsible for registering the client in the DI (using whatever ctor they choose)
HC reuse the existing singleton instance
This should improve perf (only one instance per app) and complexity (very simple).
The downside is the introduction of breaking changes.
publicvoidConfigure(IHealthChecksBuilderbuilder){
builder.Services.AddSingleton(sp =>new SecretClient(new Uri("azure-key-vault-uri"),new DefaultAzureCredential()));
builder
.AddHealthChecks().AddAzureKeyVaultSecrets();// by default resolves SecretClient from the service provider}
For those who need more than one instance per app, we would update to .NET 8 and let them user the keyed DI:
Azure SDK Clients are recommended to be used as singletons.
From https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/:
Most of the existing Azure Health Checks allow the user to specify a client instance. Example:
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.AzureStorage/AzureBlobStorageHealthCheck.cs
Line 24 in 7060bdd
But they also provide constructors that take the arguments required to create such client instance. Example:
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.AzureStorage/AzureBlobStorageHealthCheck.cs
Line 12 in 7060bdd
In such case, they are adding them to a static cache:
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.AzureStorage/AzureBlobStorageHealthCheck.cs
Line 14 in 7060bdd
I believe that it leads to having at least two instances of the client created:
This is bad for perf, but also in some rare edge cases can potentially lead into a situation when the client used by the app is disconnected, but the client used by the HC is connected and HC returns "Healthy".
Moreover, caching is hard and having a custom cache can always lead into bugs that are hard to diagnose. And increased complexity that needs to be maintained.
I would like to propose following changes for health checks that use clients that should be used as singletons:
Sample:
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.Azure.KeyVault.Secrets/AzureKeyVaultSecretsHealthCheck.cs
Line 31 in 7060bdd
IServiceProvider
, but let the user override this behaviour.Sample:
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.Azure.KeyVault.Secrets/DependencyInjection/AzureKeyVaultHealthChecksBuilderExtensions.cs
Lines 32 to 49 in 7060bdd
This would let us steer the users towards best practices:
This should improve perf (only one instance per app) and complexity (very simple).
The downside is the introduction of breaking changes.
For those who need more than one instance per app, we would update to .NET 8 and let them user the keyed DI:
@unaizorrilla @sungam3r what is your opinion on that?
The text was updated successfully, but these errors were encountered: