public static HealthCheckBuilder AddMongoDbCheck(this HealthCheckBuilder builder, string name, string connectionString, string databaseName, TimeSpan cacheDuration) { builder.AddCheck($"MongoDbCheck({name})", async() => { try { var client = new MongoClient(connectionString); IMongoDatabase database = client.GetDatabase(databaseName); BsonDocument result = await database.RunCommandAsync((Command <BsonDocument>) "{ping:1}").ConfigureAwait(false); ServerState serverState = client.Cluster.Description.Servers.FirstOrDefault()?.State ?? ServerState.Disconnected; if (serverState == ServerState.Disconnected) { return(HealthCheckResult.Unhealthy($"Unhealthy")); } else { return(HealthCheckResult.Healthy($"Healthy")); } } catch (Exception ex) { return(HealthCheckResult.Unhealthy($"{ex.GetType().FullName}")); } }, cacheDuration); return(builder); }
public static HealthCheckBuilder AddMySqlCheck(this HealthCheckBuilder builder, string name, string connectionString, TimeSpan cacheDuration) { builder.AddCheck($"MySqlCheck({name})", async() => { try { using (var connection = new MySqlConnection(connectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = "SELECT 1"; var result = await command.ExecuteScalarAsync().ConfigureAwait(false); if (result.ToString() == "1") { return(HealthCheckResult.Healthy($"Healthy")); } return(HealthCheckResult.Unhealthy($"Unhealthy")); } } } catch (Exception ex) { return(HealthCheckResult.Unhealthy($"{ex.GetType().FullName}")); } }, cacheDuration); return(builder); }
public static HealthCheckBuilder AddSqlCheck(this HealthCheckBuilder builder, string name, string connectionString, TimeSpan cacheDuration) { builder.AddCheck($"SqlServerCheck({name})", async() => { try { //TODO: There is probably a much better way to do this. using (var connection = new SqlConnection(connectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = "SELECT 1"; var result = (int)await command.ExecuteScalarAsync().ConfigureAwait(false); if (result == 1) { return(HealthCheckResult.Healthy($"Healthy")); } return(HealthCheckResult.Unhealthy($"Unhealthy")); } } } catch (Exception ex) { return(HealthCheckResult.Unhealthy($"{ex.GetType().FullName}")); } }, cacheDuration); return(builder); }
public static HealthCheckBuilder AddRedisCheck(this HealthCheckBuilder builder, string name, TimeSpan cacheDuration, string connectionString) { builder.AddCheck($"RedisCheck({name})", () => { try { using (ConnectionMultiplexer connect = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(connectionString))) { var response = connect.GetStatus(); if (response != null && response.Any()) { return(HealthCheckResult.Healthy($"Healthy")); } return(HealthCheckResult.Unhealthy($"Unhealthy")); } } catch (Exception ex) { return(HealthCheckResult.Unhealthy($"{ex.GetType().FullName}")); } }, cacheDuration); return(builder); }
public static HealthCheckBuilder AddUrlCheck(this HealthCheckBuilder builder, string url, Func <HttpResponseMessage, ValueTask <IHealthCheckResult> > checkFunc, TimeSpan cacheDuration) { Guard.ArgumentNotNull(nameof(builder), builder); Guard.ArgumentNotNullOrEmpty(nameof(url), url); Guard.ArgumentNotNull(nameof(checkFunc), checkFunc); var urlCheck = new UrlChecker(checkFunc, url); builder.AddCheck($"UrlCheck({url})", () => urlCheck.CheckAsync(), cacheDuration); return(builder); }
public static HealthCheckBuilder AddRabbitMQCheck(this HealthCheckBuilder builder, string name, Action <RabbitMqOption> setup, TimeSpan cacheDuration) { var option = new RabbitMqOption(); setup(option); var factory = new ConnectionFactory(); factory.HostName = option.HostName; factory.Port = option.Port; factory.Password = option.Password; factory.UserName = option.UserName; factory.VirtualHost = option.VirtualHost; factory.AutomaticRecoveryEnabled = true; factory.TopologyRecoveryEnabled = true; factory.UseBackgroundThreadsForIO = true; var hosts = option.HostName.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); builder.AddCheck($"RabbitMqCheck({name})", () => { try { using (var connection = factory.CreateConnection(hosts, "healthcheck")) { if (connection.IsOpen) { return(HealthCheckResult.Healthy($"Healthy")); } else { return(HealthCheckResult.Unhealthy($"Unhealthy")); } } } catch (Exception ex) { return(HealthCheckResult.Unhealthy($"{ex.GetType().FullName}")); } }, cacheDuration); return(builder); }
public static HealthCheckBuilder AddMaxValueCheck <T>(this HealthCheckBuilder builder, string name, T maxValue, Func <T> currentValueFunc, TimeSpan cacheDuration) where T : IComparable <T> { Guard.ArgumentNotNull(nameof(builder), builder); Guard.ArgumentNotNullOrEmpty(nameof(name), name); Guard.ArgumentNotNull(nameof(currentValueFunc), currentValueFunc); builder.AddCheck(name, () => { var currentValue = currentValueFunc(); var status = currentValue.CompareTo(maxValue) <= 0 ? CheckStatus.Healthy : CheckStatus.Unhealthy; return(HealthCheckResult.FromStatus( status, $"max={maxValue}, current={currentValue}", new Dictionary <string, object> { { "max", maxValue }, { "current", currentValue } } )); }, cacheDuration); return(builder); }
public static HealthCheckBuilder AddKafkaCheck(this HealthCheckBuilder builder, string name, ProducerConfig configuration, string topic, TimeSpan cacheDuration) { IProducer <string, string> _producer = null; builder.AddCheck($"KafkaCheck({name})", async() => { try { if (_producer == null) { _producer = new ProducerBuilder <string, string>(configuration).Build(); } var message = new Message <string, string>() { Key = "healthcheck-key", Value = $"Check Kafka healthy on {DateTime.UtcNow}" }; var result = await _producer.ProduceAsync(topic, message); if (result.Status == PersistenceStatus.NotPersisted) { return(HealthCheckResult.Unhealthy($"Message is not persisted or a failure is raised on health check for kafka.")); } return(HealthCheckResult.Healthy("Healthy")); } catch (Exception ex) { return(HealthCheckResult.Unhealthy(ex.Message)); } }, cacheDuration); return(builder); }
// IHealthCheck versions of AddCheck public static HealthCheckBuilder AddCheck(this HealthCheckBuilder builder, string checkName, IHealthCheck check) { Guard.ArgumentNotNull(nameof(builder), builder); return(builder.AddCheck(checkName, check, builder.DefaultCacheDuration)); }
public static HealthCheckBuilder AddValueTaskCheck(this HealthCheckBuilder builder, string name, Func <CancellationToken, ValueTask <IHealthCheckResult> > check, TimeSpan cacheDuration) { Guard.ArgumentNotNull(nameof(builder), builder); return(builder.AddCheck(name, HealthCheck.FromValueTaskCheck(check), cacheDuration)); }
public static HealthCheckBuilder AddValueTaskCheck(this HealthCheckBuilder builder, string name, Func <ValueTask <IHealthCheckResult> > check) { Guard.ArgumentNotNull(nameof(builder), builder); return(builder.AddCheck(name, HealthCheck.FromValueTaskCheck(check), builder.DefaultCacheDuration)); }
public static HealthCheckBuilder AddCheck(this HealthCheckBuilder builder, string name, Func <CancellationToken, IHealthCheckResult> check) { Guard.ArgumentNotNull(nameof(builder), builder); return(builder.AddCheck(name, HealthCheck.FromCheck(check), builder.DefaultCacheDuration)); }
// Type versions of AddCheck public static HealthCheckBuilder AddCheck <TCheck>(this HealthCheckBuilder builder, string name) where TCheck : class, IHealthCheck { Guard.ArgumentNotNull(nameof(builder), builder); return(builder.AddCheck <TCheck>(name, builder.DefaultCacheDuration)); }