Beispiel #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="healthCheck">The Health Check.</param>
 /// <param name="memoryCache">The Memory Cache Provider.</param>
 /// <param name="cacheExpirationMs">The Expiration of the Cache in milliseconds.</param>
 public CachedHealthCheck(IHealthCheck healthCheck, IMemoryCache memoryCache, ulong cacheExpirationMs = DefaultCacheExpirationMs)
 {
     _healthCheck       = healthCheck;
     _memoryCache       = memoryCache;
     _cacheKey          = GetType().FullName + _healthCheck.GetType().FullName;
     _cacheExpirationMs = cacheExpirationMs;
 }
Beispiel #2
0
            public static void HealthCheckEnd(ILogger logger, IHealthCheck healthCheck, HealthReportEntry entry, TimeSpan duration)
            {
                switch (entry.Status)
                {
                case HealthStatus.Healthy:
                    _healthCheckEndHealthy(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, entry.Status, entry.Description, null);
                    break;

                case HealthStatus.Degraded:
                    _healthCheckEndDegraded(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, entry.Status, entry.Description, null);
                    break;

                case HealthStatus.Unhealthy:
                    _healthCheckEndUnhealthy(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, entry.Status, entry.Description, null);
                    break;
                }
            }
Beispiel #3
0
        private BaseIndividualHealthCheckResult IndividualHealthCheck(IHealthCheck healthCheck)
        {
            var healthCheckName = healthCheck.GetType().Name;

            var sw = Stopwatch.StartNew();

            var task = Task.Run <BaseIndividualHealthCheckResult>(
                () =>
            {
                try
                {
                    healthCheck.Check();
                }
                catch (Exception ex)
                {
                    logger.Error($"Health check { healthCheckName} ({ healthCheck.Description}) error.", ex);

                    return(new FailedIndividualHealthCheckResult(
                               healthCheckName,
                               healthCheck.Description,
                               ex.Message,
                               healthCheck.CriticalMarking,
                               sw.Elapsed
                               ));
                }
                finally
                {
                    sw.Stop();
                }

                return(new SuccessfulIndividualHealthCheckResult(
                           healthCheckName,
                           healthCheck.Description,
                           healthCheck.CriticalMarking,
                           sw.Elapsed
                           ));
            }
                );

            var isTaskCompletedInTime = Task.WaitAll(new Task[] { task }, timeout);

            if (!isTaskCompletedInTime)
            {
                logger.Error($"Health check {healthCheckName} ({healthCheck.Description}) timed out.");

                return(new FailedIndividualHealthCheckResult(
                           healthCheckName,
                           healthCheck.Description,
                           "Health check timed out.",
                           healthCheck.CriticalMarking,
                           sw.Elapsed
                           ));
            }

            return(task.Result);
        }
        public HealthCheckWrapper(IHealthCheck healthCheck, string name = null)
        {
            HealthCheck = healthCheck;

            if (string.IsNullOrWhiteSpace(name))
            {
                name = healthCheck.GetType().Name;
            }

            Name = name;
        }
        private static string GetName(IHealthCheck check)
        {
            var name = check.GetType().Name.ToCamelCase();

            if (name.EndsWith(Suffix, StringComparison.OrdinalIgnoreCase))
            {
                name = name.Substring(0, name.Length - Suffix.Length);
            }

            return(name);
        }
Beispiel #6
0
 public static void HealthCheckData(ILogger logger, IHealthCheck healthCheck, HealthReportEntry entry)
 {
     if (entry.Data.Count > 0 && logger.IsEnabled(LogLevel.Debug))
     {
         logger.Log(
             LogLevel.Debug,
             EventIds.HealthCheckData,
             new HealthCheckDataLogValue(healthCheck.GetType().Name, entry.Data),
             null,
             (state, ex) => state.ToString());
     }
 }
Beispiel #7
0
        private IndividualHealthCheckResult CheckIndividual(IHealthCheck healthCheck)
        {
            var healthCheckName = healthCheck.GetType().Name;

            var sw = Stopwatch.StartNew();
            var checkTask = Task.Factory.StartNew(() => CheckIndividualInternal(healthCheck, healthCheckName, sw));
            var checkCompletedInTime = Task.WaitAll(new Task[] {checkTask}, _timeout);
            sw.Stop();

            return checkCompletedInTime
                       ? checkTask.Result
                       : new FailedIndividualHealthCheckResult(healthCheckName, healthCheck.Description, "Health check timed out.", sw.Elapsed);
        }
Beispiel #8
0
        private HealthCheckResult[] GetExceptionHealthCheckResult(IHealthCheck healthCheck, Exception ex)
        {
            var result = healthCheck.CreateHealthCheckResult();

            result.Output = $"Exception in health check of type {healthCheck.GetType()}:\r\n{ex}";

            if (UncaughtExceptionStatus != null)
            {
                result.Status = UncaughtExceptionStatus;
            }

            return(new[] { result });
        }
Beispiel #9
0
        private IndividualHealthCheckResult CheckIndividual(IHealthCheck healthCheck)
        {
            var healthCheckName = healthCheck.GetType().Name;

            var sw                   = Stopwatch.StartNew();
            var checkTask            = Task.Factory.StartNew(() => CheckIndividualInternal(healthCheck, healthCheckName, sw));
            var checkCompletedInTime = Task.WaitAll(new Task[] { checkTask }, _timeout);

            sw.Stop();

            return(checkCompletedInTime
                       ? checkTask.Result
                       : new FailedIndividualHealthCheckResult(healthCheckName, healthCheck.Description, "Health check timed out.", sw.Elapsed));
        }
Beispiel #10
0
 private static HealthCheckResult[] GetExceptionHealthCheckResult(IHealthCheck check, Exception ex)
 {
     return(new[]
     {
         new HealthCheckResult
         {
             ComponentName = check.ComponentName,
             MeasurementName = check.MeasurementName,
             ComponentType = check.ComponentType,
             ComponentId = check.ComponentId,
             Output = $"Exception in health check of type {check.GetType()}:\r\n{ex}",
             Status = HealthStatus.Warn,
             Time = DateTime.UtcNow
         }
     });
 }
        //Use DDS to store check results

        public CheckResult GetLatestResultFrom(IHealthCheck hc)
        {
            string nm = hc.GetType().FullName;

            return(GetStore().Items <CheckResult>().Where(cr => cr.CheckType == nm).OrderByDescending(cr => cr.CheckTime).FirstOrDefault());
        }
Beispiel #12
0
 public static void HealthCheckError(ILogger logger, IHealthCheck healthCheck, Exception exception, TimeSpan duration)
 {
     _healthCheckError(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, exception);
 }
Beispiel #13
0
 public static void HealthCheckBegin(ILogger logger, IHealthCheck healthCheck)
 {
     _healthCheckBegin(logger, healthCheck.GetType().Name, null);
 }