Skip to content

Instantly share code, notes, and snippets.

View adrianiftode's full-sized avatar
🏠
Working from home

Adrian Iftode adrianiftode

🏠
Working from home
View GitHub Profile
public void ConfigureServices(IServiceCollection services)
{
services
.AddHealthChecks()
.AddRabbitMQ()
.CheckOnlyWhen(Registrations.RabbitMQ,
conditionToRun: () => Configuration["Store"] == "RabbitMQ",
options: new ConditionalHealthCheckOptions
{
HealthStatusWhenNotChecked = HealthStatus.Degraded,
{
"status": "Healthy",
"totalDuration": "00:00:00.0152933",
"entries": [
{
"name": "redis",
"status": "Healthy",
"tags": [
"NotChecked"
],
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
.AddRedis(Configuration["Redis:ConnectionString"])
.AddRabbitMQ()
.CheckOnlyWhen<FeatureFlagsPolicy>(new[] { Registrations.Redis, Registrations.RabbitMQ },
conditionalHealthCheckPolicyCtorArgs: "BlackFridayPromotion");
}
public class FeatureFlagsPolicy : IConditionalHealthCheckPolicy
{
private readonly IFeatureFlags _featureFlags;
private readonly ILogger<FeatureFlagsPolicy> _logger;
private string FeatureName { get; }
public FeatureFlagsPolicy(string featureName,
IFeatureFlags featureFlags,
ILogger<FeatureFlagsPolicy> logger)
public interface IFeatureFlags
{
Task<bool> IsSet(string featureName);
}
public class FeatureFlags : IFeatureFlags
{
public Task<bool> IsSet(string featureName)
=> Task.FromResult(false);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
// Specify a typed policy to enable executing a health check
// This policy could have an exhaustive implementation and
// reused with other dependencies.
.AddCheck("MyDependency", () => HealthCheckResult.Healthy())
.CheckOnlyWhen<FeatureFlagsPolicy>("MyDependency",
conditionalHealthCheckPolicyCtorArgs: "NewFeature");
}
public interface IConditionalHealthCheckPolicy
{
Task<bool> Evaluate(HealthCheckContext context);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
// Check on RabbitMQ only a specific feature flag is set.
// The condition is executed asynchronously with every health check request
// and it has access to the ServiceProvider and the HealthCheckContext.
.AddRabbitMQ()
.CheckOnlyWhen(Registrations.RabbitMQ, async (sp, context, token) =>
{
var featureFlags = sp.GetService<IFeatureFlags>();
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
// Express the condition as a predicate.
// Due to this, the condition is evaluated with every request
//
// In this example when the `ADependency:Setting` config is changed,
// then the health check on `A Dependency` is re-evaluated as usual.
//
// In the Redis example such a condition is evaluated once, at startup.
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
// Check on Redis only when the environment is other than development
.AddRedis(Configuration["Redis:ConnectionString"])
.CheckOnlyWhen(Registrations.Redis, !Environment.IsDevelopment());
}