Ejemplo n.º 1
0
        public JsonOutputFormatterTests(ITestOutputHelper output)
        {
            _output    = output;
            _formatter = new HealthStatusJsonOutputFormatter(DefaultJsonSerializerSettings.CreateSerializerSettings());

            var healthyOne   = new HealthCheck.Result("test_one_healthy", HealthCheckResult.Healthy("first check was good"));
            var healthyTwo   = new HealthCheck.Result("test_two_healthy", HealthCheckResult.Healthy("second check was good"));
            var unhealthyOne = new HealthCheck.Result("test_three_unhealthy", HealthCheckResult.Unhealthy("something failed"));
            var unhealthyTwo = new HealthCheck.Result("test_four_unhealthy", HealthCheckResult.Unhealthy("something else failed"));
            var degradedOne  = new HealthCheck.Result("test_five_degraded", HealthCheckResult.Degraded("degrading service"));

            var checks = new[] { healthyOne, healthyTwo, unhealthyOne, unhealthyTwo, degradedOne };

            _healthStatus = new HealthStatus(checks);
        }
        public override async Task <HealthCheckResult> DoCheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var adminUsername = config.Get(ConfigNames.AdminUsername);
            var cacheKey      = CacheKeys.FailedLoginRequests;
            var attempts      = await cache.TryGetAsync <int>(cacheKey.Name(adminUsername));

            var message = $"There are {attempts.Value} login attempts for the admin user in the last {cacheKey.TimeToLive} hours";

            if (attempts.Success && attempts.Value >= LoginController.MaxLoginTries)
            {
                return(HealthCheckResult.Degraded(message));
            }

            return(HealthCheckResult.Healthy(message));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks the current health state using the gateway Status property.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var parameter = _accessor?.HttpContext?.Request?.Query["time"].ToString();
            int time      = _settings.Roundtrip;

            if (!string.IsNullOrEmpty(parameter) && int.TryParse(parameter, out int value))
            {
                time = (value > 0) ? value : time;
            }

            try
            {
                Ping sender  = new Ping();
                var  options = new PingOptions(_settings.Ttl, _settings.DontFragment);
                var  buffer  = Encoding.ASCII.GetBytes(new string('x', 56));
                var  reply   = sender.Send(_settings.Host, _settings.Timeout, buffer, options);

                if (reply.Status == IPStatus.Success)
                {
                    if (reply.RoundtripTime > time)
                    {
                        return(Task.FromResult(HealthCheckResult.Degraded($"Gateway ping OK ({reply.RoundtripTime} msec)", null,
                                                                          new Dictionary <string, object>()
                        {
                            { "Status", reply.Status }
                        })));
                    }

                    return(Task.FromResult(HealthCheckResult.Healthy("Gateway ping OK",
                                                                     new Dictionary <string, object>()
                    {
                        { "Status", reply.Status }
                    })));
                }
                else
                {
                    return(Task.FromResult(HealthCheckResult.Unhealthy("Gateway ping not OK", null,
                                                                       new Dictionary <string, object>()
                    {
                        { "Status", reply.Status }
                    })));
                }
            }
            catch (PingException pex)
            {
                return(Task.FromResult(HealthCheckResult.Unhealthy("Gateway ping exception", pex)));
            }
        }
Ejemplo n.º 4
0
        public void ConfigureServices(IServiceCollection services)
        {
            var isLive = true;

            services.AddHealthChecks()
            .AddCheck(
                "live",
                () => { return(isLive ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy()); })
            .AddMySql(
                Configuration["ConnectionString"],
                "mysql")
            .AddCheck(
                "his",
                () => { return(HealthCheckResult.Degraded()); });
            services.AddControllers();
        }
Ejemplo n.º 5
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var manager = client.GetGrain <IManagementGrain>(0);

            try
            {
                var hosts = await manager.GetHosts();

                var count = hosts.Values.Where(x => x.IsUnavailable()).Count();
                return(count > 0 ? HealthCheckResult.Degraded($"{count} silo(s) unavailable") : HealthCheckResult.Healthy());
            }
            catch (Exception error)
            {
                return(HealthCheckResult.Unhealthy("Failed to get cluster status", error));
            }
        }
Ejemplo n.º 6
0
        public HealthCheckResult GenerateRandomResult()
        {
            int value = random.Next(100);

            switch (value)
            {
            case int n when(n >= 80):
                return(HealthCheckResult.Unhealthy());

            case int n when(n >= 50):
                return(HealthCheckResult.Degraded());

            default:
                return(HealthCheckResult.Healthy());
            }
        }
Ejemplo n.º 7
0
        protected override Task <HealthCheckResult> CheckAsync(CancellationToken token = default(CancellationToken))
        {
            int second = DateTime.Now.Second;

            switch (second % 3)
            {
            case 0:
                return(Task.FromResult(HealthCheckResult.Healthy("OK")));

            case 1:
                return(Task.FromResult(HealthCheckResult.Degraded("Degraded")));

            default:
                return(Task.FromResult(HealthCheckResult.Unhealthy("不健康")));
            }
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        protected override async ValueTask <HealthCheckResult> CheckAsync(CancellationToken cancellationToken = default)
        {
            await Task.Delay(TimeSpan.FromSeconds(6), cancellationToken);

            if (DateTime.UtcNow.Second <= 20)
            {
                return(HealthCheckResult.Degraded());
            }

            if (DateTime.UtcNow.Second >= 40)
            {
                return(HealthCheckResult.Unhealthy());
            }

            return(HealthCheckResult.Healthy());
        }
Ejemplo n.º 9
0
        public Task <HealthCheckResult> CheckAsync(CancellationToken cancellationToken)
        {
            var state = ThreadPoolUtility.GetPoolState();

            if (state.UsedWorkerThreads >= state.MinWorkerThreads)
            {
                return(Task.FromResult(HealthCheckResult.Degraded($"Worker threads in thread pool are exhausted: {state.UsedWorkerThreads}/{state.MinWorkerThreads} (used/min).")));
            }

            if (state.UsedIocpThreads >= state.MinIocpThreads)
            {
                return(Task.FromResult(HealthCheckResult.Degraded($"IOCP threads in thread pool are exhausted: {state.UsedIocpThreads}/{state.MinIocpThreads} (used/min).")));
            }

            return(Task.FromResult(HealthCheckResult.Healthy()));
        }
Ejemplo n.º 10
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                using var ping = new Ping();
                var reply = await ping.SendPingAsync(host);

                var description = $"ICMP to {host} took {reply.RoundtripTime} ms.";  
                return(reply.Status switch
                {
                    IPStatus.Success => reply.RoundtripTime > healthyRoundtripTime
                        ? HealthCheckResult.Degraded(description)
                        : HealthCheckResult.Healthy(description),
                    _ => HealthCheckResult.Unhealthy($"ICMP to {host} failed: {reply.Status}"),
                });
            }
Ejemplo n.º 11
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var responseTime = _random.Next(1, 300);

            if (responseTime > 200)
            {
                return(Task.FromResult(HealthCheckResult.Unhealthy($"The response time is unacceptable ({responseTime})")));
            }

            if (responseTime > 100)
            {
                return(Task.FromResult(HealthCheckResult.Degraded($"The response time is a bit slow ({responseTime})")));
            }

            return(Task.FromResult(HealthCheckResult.Healthy($"The response time looks good ({responseTime})")));
        }
Ejemplo n.º 12
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var nowSecs = DateTime.UtcNow.Second;

            if (nowSecs % 2 == 0)
            {
                return(Task.FromResult(HealthCheckResult.Healthy()));
            }

            if (nowSecs % 3 == 0)
            {
                return(Task.FromResult(HealthCheckResult.Degraded()));
            }

            return(Task.FromResult(HealthCheckResult.Unhealthy()));
        }
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                _connection ??= CreateConnection(_options);
                if (_connection is null)
                {
                    throw new ArgumentNullException(nameof(_connection));
                }
                var healthCheckResult = GetHealthCheckResultFromState(_connection);
                return(Task.FromResult(healthCheckResult));
            }
            catch (Exception ex)
            {
                var unhealthy = new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
                return(Task.FromResult(unhealthy));
            }

            IConnection CreateConnection(NatsOptions options)
            {
                if (!string.IsNullOrWhiteSpace(options.CredentialsPath))
                {
                    return(_connectionFactory.CreateConnection(options.Url, options.CredentialsPath));
                }
                if (!string.IsNullOrWhiteSpace(options.Jwt) && !string.IsNullOrWhiteSpace(options.PrivateNKey))
                {
                    return(_connectionFactory.CreateConnection(options.Url, options.Jwt, options.PrivateNKey));
                }
                return(_connectionFactory.CreateConnection(options.Url));
            }

            HealthCheckResult GetHealthCheckResultFromState(IConnection connection)
            {
                string description = GetDescription(connection);

                return(connection.State switch
                {
                    ConnState.CONNECTED => HealthCheckResult.Healthy(description, GetStatsData(connection)),
                    ConnState.CONNECTING
                    or ConnState.RECONNECTING
                    or ConnState.DRAINING_SUBS
                    or ConnState.DRAINING_PUBS => HealthCheckResult.Degraded(description),
                    ConnState.CLOSED
                    or ConnState.DISCONNECTED => HealthCheckResult.Unhealthy(description),
                    _ => new HealthCheckResult(context.Registration.FailureStatus, description),
                });
            }
Ejemplo n.º 14
0
        private static ValueTask <HealthCheckResult> ExecuteSqlCheckAsync(
            string name,
            Func <IDbConnection> newDbConnection,
            TimeSpan timeout,
            bool degradedOnError,
            CancellationToken cancellationToken)
        {
            var sw = new Stopwatch();

            try
            {
                using (var tokenWithTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
                {
                    tokenWithTimeout.CancelAfter(timeout);

                    sw.Start();
                    using (var connection = newDbConnection())
                    {
                        if (connection.State == ConnectionState.Closed)
                        {
                            connection.Open();
                        }

                        using (var command = connection.CreateCommand())
                        {
                            command.CommandType = CommandType.Text;
                            command.CommandText = "SELECT 1";
                            var commandResult = Convert.ToInt64(command.ExecuteScalar());

                            var result = commandResult == 1
                                ? HealthCheckResult.Healthy($"OK. {name}.")
                                : HealthCheckResultOnError($"FAILED. {name} SELECT failed. Time taken: {sw.ElapsedMilliseconds}ms.", degradedOnError);

                            return(new ValueTask <HealthCheckResult>(result));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var failedResult = degradedOnError
                    ? HealthCheckResult.Degraded(ex)
                    : HealthCheckResult.Unhealthy(ex);

                return(new ValueTask <HealthCheckResult>(failedResult));
            }
        }
Ejemplo n.º 15
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            int responseTimeInMs = rdn.Next(1, 300);

            if (responseTimeInMs < 100)
            {
                return(Task.FromResult(HealthCheckResult.Healthy($"The response time looks good ({ responseTimeInMs }ms).")));
            }
            else if (responseTimeInMs < 200)
            {
                return(Task.FromResult(HealthCheckResult.Degraded($"The response time is a bit slow ({ responseTimeInMs }ms).")));
            }
            else
            {
                return(Task.FromResult(HealthCheckResult.Unhealthy($"The response time is unacceptable ({ responseTimeInMs }ms).")));
            }
        }
Ejemplo n.º 16
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            CircuitBreakerPolicy policy;
            HealthCheckResult    result = HealthCheckResult.Healthy();

            foreach (string name in options.Value.CircuitBreakerNames)
            {
                if (registry.TryGet <CircuitBreakerPolicy>(name, out policy))
                {
                    if (policy.CircuitState == CircuitState.Isolated || policy.CircuitState == CircuitState.HalfOpen)
                    {
                        result = HealthCheckResult.Degraded(description: "Too many circuit breakers are (half)open.");
                    }
                }
            }
            return(Task.FromResult(result));
        }
Ejemplo n.º 17
0
        public static void Main()
        {
            var random = new Random();

            Host.CreateDefaultBuilder()
            .ConfigureWebHostDefaults(builder => builder.ConfigureServices(svcs => svcs.AddHealthChecks()
                                                                           .AddCheck("default", Check)).Configure(app => app.UseHealthChecks("/healthcheck")))
            .Build()
            .Run();

            HealthCheckResult Check() => (random.Next(1, 4)) switch
            {
                1 => HealthCheckResult.Unhealthy(),
                2 => HealthCheckResult.Degraded(),
                _ => HealthCheckResult.Healthy(),
            };
        }
Ejemplo n.º 18
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
        {
            if (_hostEnvironmentStatistics.CpuUsage > UnhealthyThreshold)
            {
                return(Task.FromResult(HealthCheckResult.Unhealthy(
                                           $"CPU utilization is unhealthy at {_hostEnvironmentStatistics.CpuUsage}%.")));
            }

            if (_hostEnvironmentStatistics.CpuUsage > DegradedThreshold)
            {
                return(Task.FromResult(HealthCheckResult.Degraded(
                                           $"CPU utilization is degraded at {_hostEnvironmentStatistics.CpuUsage}%.")));
            }

            return(Task.FromResult(HealthCheckResult.Healthy(
                                       $"CPU utilization is healthy at {_hostEnvironmentStatistics.CpuUsage}%.")));
        }
Ejemplo n.º 19
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var(IsHealthy, time) = await CheckConnection();

            if (IsHealthy)
            {
                if (time > 10)
                {
                    return(HealthCheckResult.Degraded("Connection Is Poor"));
                }
                return(HealthCheckResult.Healthy("healthed"));
            }
            else
            {
                return(HealthCheckResult.Unhealthy("Unhealthy"));
            }
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var forcast = await _weatherService.GetDailyForeCastAsync(_testableCoordinates);

                if (forcast.Forecasts.Any())
                {
                    return(HealthCheckResult.Healthy());
                }
                return(HealthCheckResult.Degraded());
            }
            catch (Exception ex)
            {
                return(HealthCheckResult.Degraded(nameof(IWeatherService), ex));
            }
        }
Ejemplo n.º 21
0
        public async Task Status_is_degraded_if_one_check_is_degraded()
        {
            // Arrange
            var checks = new[]
            {
                new HealthCheck("ok", () => new ValueTask <HealthCheckResult>(HealthCheckResult.Healthy())),
                new HealthCheck("degraded", () => new ValueTask <HealthCheckResult>(HealthCheckResult.Degraded()))
            };
            var runner = new DefaultHealthCheckRunner(checks);

            // Act
            var status = await runner.ReadAsync();

            // Assert
            status.Status.Should().Be(HealthCheckStatus.Degraded);
            status.Results.Count().Should().Be(2);
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var description = $"{typeof(CourseCurrentOpportunitiesRefresh).Namespace} - SearchKeywords used [{courseSearchSettings.HealthCheckKeyWords}]";

            logger.LogInformation($"{nameof(CheckHealthAsync)} has been called - service {description}");

            var result = await courseSearchApiService.GetCoursesAsync(courseSearchSettings.HealthCheckKeyWords).ConfigureAwait(false);

            if (result.Any())
            {
                return(HealthCheckResult.Healthy(description));
            }
            else
            {
                return(HealthCheckResult.Degraded(description));
            }
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var description = $"{typeof(AVAPIService).Namespace} - Mappings used standards[{aVAPIServiceSettings.StandardsForHealthCheck}]";

            logger.LogInformation($"{nameof(CheckHealthAsync)} has been called - service {description}");

            var apprenticeshipVacancySummaryResponse = await GetAVSumaryPageAsync(new AVMapping { Standards = aVAPIServiceSettings.StandardsForHealthCheck.Split(',') }, 1).ConfigureAwait(false);

            if (apprenticeshipVacancySummaryResponse.Results.Any())
            {
                return(HealthCheckResult.Healthy(description));
            }
            else
            {
                return(HealthCheckResult.Degraded(description));
            }
        }
Ejemplo n.º 24
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            int responseTimeMS = rnd.Next(1, 300);

            if (responseTimeMS < 100)
            {
                return(Task.FromResult(HealthCheckResult.Healthy($"The response time looks good ({responseTimeMS }")));
            }
            else if (responseTimeMS < 200)
            {
                return(Task.FromResult(HealthCheckResult.Degraded($"The response time is a bit slow ({responseTimeMS }")));
            }
            else
            {
                return(Task.FromResult(HealthCheckResult.Unhealthy($"The response time is too long ({responseTimeMS }")));
            }
        }
Ejemplo n.º 25
0
        public Task <HealthCheckResult> CheckHealthAsync(
            HealthCheckContext context,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Execute health check logic here. This example sets a dummy
            // variable to true.
            var healthCheckResultHealthy = true;

            if (healthCheckResultHealthy)
            {
                return(Task.FromResult(
                           HealthCheckResult.Healthy("The check indicates a healthy result.")));
            }

            return(Task.FromResult(
                       HealthCheckResult.Degraded("The check indicates an unhealthy result.")));
        }
Ejemplo n.º 26
0
        public Task <HealthCheckResult> CheckHealthAsync(
            HealthCheckContext context,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Debug.WriteLine("Executing StartupHealthCheck");

            switch (this._statusSvc.HealthStatusStartup)
            {
            case HealthStatus.Unhealthy:
                return(Task.FromResult(HealthCheckResult.Unhealthy("Reported Unhealthy")));

            case HealthStatus.Degraded:
                return(Task.FromResult(HealthCheckResult.Degraded("Reported Degraded")));

            default:
                return(Task.FromResult(HealthCheckResult.Healthy("Reported Healthy")));
            }
        }
Ejemplo n.º 27
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                var status = await _taxasClient.GetHealthCheck(cancellationToken);

                if (status == HealthStatus.Healthy)
                {
                    return(HealthCheckResult.Healthy("Consulta de taxas está operacional"));
                }

                return(HealthCheckResult.Degraded("Consulta de taxas não está operacional"));
            }
            catch (Exception ex)
            {
                return(HealthCheckResult.Degraded("Consulta de taxas não está operacional", ex));
            }
        }
Ejemplo n.º 28
0
        public async Task <HealthCheckResult> CheckHealth()
        {
            var response = await _httpClient.GetAsync("/health");

            if (!response.IsSuccessStatusCode)
            {
                return(HealthCheckResult.Unhealthy("CertServer reported Unhealthy"));
            }

            string content = await response.Content.ReadAsStringAsync();

            if (content == nameof(HealthCheckResult.Degraded))
            {
                return(HealthCheckResult.Degraded("CertServer reported Degraded"));
            }

            return(HealthCheckResult.Healthy());
        }
Ejemplo n.º 29
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
        {
            var info = CommonUtils.GetStorageInfo(_path);

            var data = new Dictionary <string, object>
            {
                { "StorageTotalSize", info?.TotalSize },
                { "StorageFreeSpace", info?.FreeSpace },
                { "StorageFreeSpacePerc", info?.FreeSpacePerc }
            };

            if (info != null && info.FreeSpacePerc <= _freeSpacePercWarningThreshold)
            {
                return(Task.FromResult(HealthCheckResult.Degraded("Low on available disk space", null, data)));
            }

            return(Task.FromResult(HealthCheckResult.Healthy("Free disk space is fine", data)));
        }
Ejemplo n.º 30
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            await Task.Delay(3000);

            var r = new Random();
            var i = r.Next(3);

            switch (i)
            {
            case 1: return(HealthCheckResult.Healthy(""));

            case 2: return(HealthCheckResult.Degraded(""));

            case 3: return(HealthCheckResult.Unhealthy(""));

            default: return(HealthCheckResult.Unhealthy(""));
            }
        }