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 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 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 AddUrlCheck(this HealthCheckBuilder builder, string url, Func <HttpResponseMessage, Task <IHealthCheckResult> > checkFunc, TimeSpan cacheDuration) { Guard.ArgumentNotNull(nameof(checkFunc), checkFunc); return(AddUrlCheck(builder, url, response => new ValueTask <IHealthCheckResult>(checkFunc(response)), cacheDuration)); }
public HealthCheckService(HealthCheckBuilder builder, IServiceProvider serviceProvider, IServiceScopeFactory serviceScopeFactory) { _builder = builder; _groups = GetGroups().Where(group => group.GroupName != string.Empty).ToList(); _root = GetGroup(string.Empty); _serviceProvider = serviceProvider; _serviceScopeFactory = serviceScopeFactory; }
/// <summary> /// This constructor should only be used when creating a grouped health check builder. /// </summary> public HealthCheckBuilder(HealthCheckBuilder rootBuilder, HealthCheckGroup currentGroup) { Guard.ArgumentNotNull(nameof(rootBuilder), rootBuilder); Guard.ArgumentNotNull(nameof(currentGroup), currentGroup); _checksByName = rootBuilder._checksByName; _currentGroup = currentGroup; _groups = rootBuilder._groups; DefaultCacheDuration = rootBuilder.DefaultCacheDuration; }
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); }
/// <summary> /// Creates a new health check group, to which you can add one or more health /// checks. /// </summary> public HealthCheckBuilder AddHealthCheckGroup(string groupName, Action <HealthCheckBuilder> groupChecks, CheckStatus partialSuccessStatus) { Guard.ArgumentNotNullOrEmpty(nameof(groupName), groupName); Guard.ArgumentNotNull(nameof(groupChecks), groupChecks); Guard.ArgumentValid(partialSuccessStatus != CheckStatus.Unknown, nameof(partialSuccessStatus), "Check status 'Unknown' is not valid for partial success."); Guard.ArgumentValid(!_groups.ContainsKey(groupName), nameof(groupName), $"A group with name '{groupName}' has already been registered."); Guard.OperationValid(_currentGroup.GroupName == string.Empty, "Nested groups are not supported by HealthCheckBuilder."); var group = new HealthCheckGroup(groupName, partialSuccessStatus); _groups.Add(groupName, group); var innerBuilder = new HealthCheckBuilder(this, group); groupChecks(innerBuilder); return(this); }
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); }
public static HealthCheckBuilder AddWorkingSetCheck(this HealthCheckBuilder builder, long maxSize, TimeSpan cacheDuration) => AddMaxValueCheck(builder, $"WorkingSet({maxSize})", maxSize, () => Process.GetCurrentProcess().WorkingSet64, cacheDuration);
public static HealthCheckBuilder AddVirtualMemorySizeCheck(this HealthCheckBuilder builder, long maxSize, TimeSpan cacheDuration) => AddMaxValueCheck(builder, $"VirtualMemorySize({maxSize})", maxSize, () => Process.GetCurrentProcess().VirtualMemorySize64, cacheDuration);
// System checks public static HealthCheckBuilder AddPrivateMemorySizeCheck(this HealthCheckBuilder builder, long maxSize) => AddMaxValueCheck(builder, $"PrivateMemorySize({maxSize})", maxSize, () => Process.GetCurrentProcess().PrivateMemorySize64);
public static HealthCheckBuilder AddKafkaCheck(this HealthCheckBuilder builder, string name, ProducerConfig configuration, string topic) { Guard.ArgumentNotNull(nameof(builder), builder); return(AddKafkaCheck(builder, name, configuration, topic, 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 AddMaxValueCheck <T>(this HealthCheckBuilder builder, string name, T maxValue, Func <T> currentValueFunc) where T : IComparable <T> { Guard.ArgumentNotNull(nameof(builder), builder); return(AddMaxValueCheck(builder, name, maxValue, currentValueFunc, builder.DefaultCacheDuration)); }
public static HealthCheckBuilder AddRabbitMQCheck(this HealthCheckBuilder builder, string name, Action <RabbitMqOption> setup) { Guard.ArgumentNotNull(nameof(builder), builder); return(AddRabbitMQCheck(builder, name, setup, 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)); }
public static HealthCheckBuilder AddMySqlCheck(this HealthCheckBuilder builder, string name, string connectionString) { Guard.ArgumentNotNull(nameof(builder), builder); return(AddMySqlCheck(builder, name, connectionString, builder.DefaultCacheDuration)); }
public static HealthCheckBuilder AddUrlCheck(this HealthCheckBuilder builder, string url, TimeSpan cacheDuration) => AddUrlCheck(builder, url, response => UrlChecker.DefaultUrlCheck(response), cacheDuration);
// Default URL check public static HealthCheckBuilder AddUrlCheck(this HealthCheckBuilder builder, string url) { Guard.ArgumentNotNull(nameof(builder), builder); return(AddUrlCheck(builder, url, builder.DefaultCacheDuration)); }
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)); }
// 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)); }
// Func returning ValueTask<IHealthCheckResult> public static HealthCheckBuilder AddUrlCheck(this HealthCheckBuilder builder, string url, Func <HttpResponseMessage, ValueTask <IHealthCheckResult> > checkFunc) { Guard.ArgumentNotNull(nameof(builder), builder); return(AddUrlCheck(builder, url, checkFunc, 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)); }