Exemplo n.º 1
0
        private static async Task <HealthCheckResult> ExecutePingCheckAsync(string host, TimeSpan timeout, bool degradedOnError)
        {
            try
            {
                var ping   = new Ping();
                var result = await ping.SendPingAsync(host, (int)timeout.TotalMilliseconds).ConfigureAwait(false);

                return(result.Status == IPStatus.Success
                    ? HealthCheckResult.Healthy($"OK. {host}")
                    : HealthCheckResultOnError($"FAILED. {host} ping result was {result.Status}", degradedOnError));
            }
            catch (Exception ex)
            {
                return(degradedOnError
                    ? HealthCheckResult.Degraded(ex)
                    : HealthCheckResult.Unhealthy(ex));
            }
        }
        private static Func <ValueTask <HealthCheckResult> > CheckMessageCount(string name, CloudStorageAccount storageAccount, string queueName, long degradedThreshold, long?unhealthyThreshold)
        {
            var queue = storageAccount
                        .CreateCloudQueueClient()
                        .GetQueueReference(queueName);

            return(async() =>
            {
                int?result = null;

                try
                {
                    await queue.FetchAttributesAsync().ConfigureAwait(false);

                    result = queue.ApproximateMessageCount;
                }
                catch (Exception ex)
                {
                    Logger.ErrorException($"{name} failed.", ex);

                    return HealthCheckResult.Unhealthy($"Failed. Unable to check queue '{queueName}'.");
                }

                if (result > 0 && result >= unhealthyThreshold)
                {
                    return HealthCheckResult.Unhealthy($"Unhealthy. '{queueName}' has {result.Value} messages.");
                }

                if (result > 0 && result >= degradedThreshold)
                {
                    return HealthCheckResult.Degraded($"Degraded. '{queueName}' has {result.Value} messages.");
                }

                return HealthCheckResult.Healthy($"OK. '{queueName}' has {result} messages.");
            });
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Create a failure (degraded or unhealthy) status response.
 /// </summary>
 /// <param name="message">Status message.</param>
 /// <param name="degradedOnError">
 ///     If true, create a degraded status response.
 ///     Otherwise create an unhealthy status response. (default: false)
 /// </param>
 /// <returns>Failure status response.</returns>
 private static HealthCheckResult HealthCheckResultOnError(string message, bool degradedOnError)
 {
     return(degradedOnError
         ? HealthCheckResult.Degraded(message)
         : HealthCheckResult.Unhealthy(message));
 }