Ejemplo n.º 1
0
        private IHealthCheckResult BuildHealthCheckAndSetExpectations(bool categoryFound, bool[] counterExistence)
        {
            IPerformanceCounterInquisitor inquisitor = m_MockRepository.StrictMock <IPerformanceCounterInquisitor>();
            IHealthCheck healthCheck = PerformanceCounterRegistrationHealthCheck.Create(inquisitor);

            using (m_MockRepository.Record())
            {
                Expect.Call(inquisitor.CheckCategoryExists(PerformanceCounterRegistrar.CategoryName)).Return(
                    categoryFound);

                Debug.Assert(counterExistence.Length == m_PerformanceCounterDefinitions.Count);

                if (categoryFound)
                {
                    for (int index = 0; index < m_PerformanceCounterDefinitions.Count; index++)
                    {
                        Expect.Call(inquisitor.CheckCounterExists(m_PerformanceCounterDefinitions[index].CounterName)).
                        Return(counterExistence[index]);
                    }
                }

                m_MockRepository.ReplayAll();
            }

            return(healthCheck.Execute());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a new health check with the specified name and implementation.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="name">The name of the health check.</param>
        /// <param name="instance">An <see cref="IHealthCheck"/> instance.</param>
        /// <param name="failureStatus">
        /// The <see cref="HealthStatus"/> that should be reported when the health check reports a failure. If the provided value
        /// is <c>null</c>, then <see cref="HealthStatus.Unhealthy"/> will be reported.
        /// </param>
        /// <param name="tags">A list of tags that can be used to filter health checks.</param>
        /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
        public static IHealthChecksBuilder AddCheck(
            this IHealthChecksBuilder builder,
            string name,
            IHealthCheck instance,
            HealthStatus?failureStatus = null,
            IEnumerable <string> tags  = null,
            TimeSpan?timeout           = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            return(builder.Add(new HealthCheckRegistration(name, instance, failureStatus, tags, timeout)));
        }
Ejemplo n.º 3
0
        public void ExceptionThrownInHealthCheckImplementationIsReportedAsFailedHealthCheckResult()
        {
            IHealthCheck         firstHealthCheck     = m_Repository.StrictMock <IHealthCheck>();
            HealthCheckException healthCheckException = m_Repository.StrictMock <HealthCheckException>();

            using (m_Repository.Record())
            {
                Expect.Call(firstHealthCheck.Execute()).Throw(healthCheckException);
                Expect.Call(healthCheckException.Message).Return("Sample Exception");
                m_Repository.ReplayAll();

                HealthCheckResultCollection resultCollection =
                    HealthCheckRunner.RunHealthChecks(new HealthChecklistCollection(new[] { firstHealthCheck }));

                Assert.AreEqual(1, resultCollection.Count);

                IHealthCheckResult result = resultCollection[0];

                Assert.IsFalse(result.Passed);
                StringAssert.StartsWith(result.Message,
                                        "An exception was thrown during the execution of the Health Check");
                StringAssert.Contains(result.Message, "Sample Exception");

                m_Repository.VerifyAll();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an instance of <see cref="HealthCheckResult"/> with its properties set from
        /// the properties of an instance of <see cref="IHealthCheck"/>. In addition, its
        /// <see cref="HealthCheckResult.Time"/> property is also set to the current UTC time.
        /// </summary>
        /// <param name="healthCheck">An instance of <see cref="IHealthCheck"/>.</param>
        /// <returns>An instance of <see cref="HealthCheckResult"/>.</returns>
        public static HealthCheckResult CreateHealthCheckResult(this IHealthCheck healthCheck)
        {
            if (healthCheck == null)
            {
                throw new ArgumentNullException(nameof(healthCheck));
            }

            var result = new HealthCheckResult
            {
                ComponentName   = healthCheck.ComponentName,
                MeasurementName = healthCheck.MeasurementName,
                Time            = DateTime.UtcNow
            };

            if (healthCheck.ComponentId != null)
            {
                result.ComponentId = healthCheck.ComponentId;
            }

            if (healthCheck.ComponentType != null)
            {
                result.ComponentType = healthCheck.ComponentType;
            }

            return(result);
        }
Ejemplo n.º 5
0
 public StoreController(
     IConfiguration configuration,
     IHealthCheck healthCheck)
 {
     _configuration = configuration;
     _healthCheck   = healthCheck;
 }
Ejemplo n.º 6
0
        // 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));
        }
Ejemplo n.º 7
0
 private static IHealthChecksBuilder AddQuartzHealthCheck(
     this IHealthChecksBuilder builder,
     string suffix,
     IHealthCheck healthCheck)
 {
     return(builder.AddCheck($"quartz-{suffix}", healthCheck, HealthStatus.Unhealthy, new[] { "ready" }));
 }
Ejemplo n.º 8
0
 private static IHealthChecksBuilder AddQuartzHealthCheck(
     this IHealthChecksBuilder builder,
     string suffix,
     IHealthCheck healthCheck)
 {
     return(builder.AddCheck($"quartz-{suffix}", healthCheck));
 }
Ejemplo n.º 9
0
        private IHealthCheck GetHealthCheck(Tuple <bool, string>[] results, bool readReturnValue)
        {
            DbConnection connection   = m_MockRepository.StrictMock <DbConnection>();
            DbCommand    sprocCommand = m_MockRepository.StrictMock <DbCommand>();
            DbDataReader dataReader   = m_MockRepository.StrictMock <DbDataReader>();

            IHealthCheck healthCheck = SqlServerStorageContainerObjectExistenceHealthCheck.Create(connection);

            using (m_MockRepository.Record())
            {
                Expect.Call(connection.CreateCommand()).Return(sprocCommand);
                Expect.Call(sprocCommand.ExecuteReader()).Return(dataReader);
                Expect.Call(() => { sprocCommand.CommandText = "IsHealthy_ObjectExistenceCheck"; });
                Expect.Call(() => { sprocCommand.CommandType = CommandType.StoredProcedure; });
                Expect.Call(dataReader.Read()).Return(readReturnValue);
                Expect.Call(((IDisposable)dataReader).Dispose);

                if (readReturnValue)
                {
                    int fieldCount = results.Length;
                    Expect.Call(dataReader.FieldCount).Return(fieldCount);

                    for (int index = 0; index < fieldCount; index++)
                    {
                        Expect.Call(dataReader.GetBoolean(index)).Return(results[index].Item1);
                        Expect.Call(dataReader.GetName(index)).Return(results[index].Item2);
                    }
                }
            }

            m_MockRepository.ReplayAll();
            return(healthCheck);
        }
Ejemplo n.º 10
0
        public HealthController(
            IHealthCheck healthCheck)
        {
            EnsureArg.IsNotNull(healthCheck, nameof(healthCheck));

            _healthCheck = healthCheck;
        }
Ejemplo n.º 11
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;
 }
Ejemplo n.º 12
0
        public void CreateHealthCheckResult_GivenNullHealthCheck_Throws()
        {
            IHealthCheck healthCheck = null;

            Action act = () => healthCheck.CreateHealthCheckResult();

            act.Should().ThrowExactly <ArgumentNullException>().WithMessage("*healthCheck*");
        }
        /// <summary>
        /// Initializes a new instance of the type
        /// </summary>
        /// <param name="loggerFactory">Factory to create loggers</param>
        /// <param name="healthCheck">Logic to perform health checks</param>
        public HealthProbeLogic(ILoggerFactory loggerFactory, IHealthCheck healthCheck)
        {
            Guard.NotNull(nameof(loggerFactory), loggerFactory);
            Guard.NotNull(nameof(healthCheck), healthCheck);

            _healthLogic = healthCheck;
            _logger      = loggerFactory.CreateLogger(Name);
        }
Ejemplo n.º 14
0
        public HealthCheckBuilder AddCheck(string name, IHealthCheck check)
        {
            Guard.ArgumentNotNullOrWhitespace(nameof(name), name);
            Guard.ArgumentNotNull(nameof(check), check);

            _checks.Add(name, check);
            return(this);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="logger"></param>
 public HealthController(
     ILogger <HealthController> logger,
     IHealthCheck healthCheck
     )
 {
     _logger      = logger ?? throw new ArgumentNullException(nameof(logger));
     _healthCheck = healthCheck ?? throw new ArgumentNullException(nameof(healthCheck));
 }
Ejemplo n.º 16
0
 internal StatusBuilder(IHealthCheck sourceHealthCheck, bool isPrivileged)
 {
     MessageBuilder     = new StringBuilder();
     _sourceHealthCheck = sourceHealthCheck;
     CurrentLevel       = sourceHealthCheck.MaximumSeverityLevel;
     _isPrivileged      = isPrivileged;
     _stopwatch.Start();
 }
Ejemplo n.º 17
0
        public void NoResultsReturnedFromSproc()
        {
            IHealthCheck       healthCheck = GetHealthCheck(null, false);
            IHealthCheckResult result      = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "The IsHealthy_ObjectExistenceCheck sproc produced no result row.");
        }
Ejemplo n.º 18
0
        public void AddCheck_ShouldAddCheckOnlyOnce_WhenSameCheckAddedMultipleTimes(IHealthCheck check)
        {
            HealthMonitor.AddCheck(check).Should().BeTrue();
            HealthMonitor.AddCheck(check).Should().BeFalse();
            HealthMonitor.AddCheck(check).Should().BeFalse();
            HealthMonitor.AddCheck(check).Should().BeFalse();

            HealthMonitor.Checks.Count.Should().Be(1);
        }
Ejemplo n.º 19
0
 public AppInnit(IHealthCheck hc, IGetProducts gp, IGetProductions gpr, IStoreCatalogReadyService scrs, IGetProductChangedService gpcs, IGetProductionAreaChangedService gpacs)
 {
     _healthCheck                     = hc;
     _getProducts                     = gp;
     _getProductions                  = gpr;
     _catalogReadyMessage             = scrs;
     _getProductChangedService        = gpcs;
     _getProductionAreaChangedService = gpacs;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Adds a new health check with the specified name and implementation.
 /// </summary>
 /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
 /// <param name="name">The name of the health check.</param>
 /// <param name="instance">An <see cref="IHealthCheck"/> instance.</param>
 /// <param name="failureStatus">
 /// The <see cref="HealthStatus"/> that should be reported when the health check reports a failure. If the provided value
 /// is <c>null</c>, then <see cref="HealthStatus.Unhealthy"/> will be reported.
 /// </param>
 /// <param name="tags">A list of tags that can be used to filter health checks.</param>
 /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
 // 2.0 BACKCOMPAT OVERLOAD -- DO NOT TOUCH
 public static IHealthChecksBuilder AddCheck(
     this IHealthChecksBuilder builder,
     string name,
     IHealthCheck instance,
     HealthStatus?failureStatus,
     IEnumerable <string> tags)
 {
     return(AddCheck(builder, name, instance, failureStatus, tags, default));
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Creates a new instance.
        /// </summary>
        /// <param name="healthCheck">The health check to cache</param>
        /// <param name="cache">The memory cache</param>
        /// <param name="properties">The health check properties</param>
        /// <param name="logger">An optional logger instance</param>
        /// <exception cref="ArgumentNullException"></exception>
        public CachedHealthCheck(IHealthCheck healthCheck, IMemoryCache cache, CachedHealthCheckProperties properties, ILogger <CachedHealthCheck> logger = null)
        {
            HealthCheck = healthCheck ?? throw new ArgumentNullException(nameof(healthCheck));
            Cache       = cache ?? throw new ArgumentNullException(nameof(cache));
            Properties  = properties ?? throw new ArgumentNullException(nameof(properties));
            Logger      = logger ?? NullLogger <CachedHealthCheck> .Instance;

            CacheKey = nameof(CachedHealthCheck) + "->" + HealthCheck.Name;
        }
 public static IHostBuilder AddHealthCheckItem(this IHostBuilder @this, IHealthCheck healthCheckItem)
 {
     if ([email protected](out HealthCheckModule module))
     {
         throw new ModuleNotFoundException(typeof(HealthCheckModule).Name);
     }
     module.HealthChecks.Add(healthCheckItem);
     return(@this);
 }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
0
        private async Task <HealthCheckResult> CheckHealthAsync(HealthCheckPolicy policy, IHealthCheckSettings settings)
        {
            var checkCache = _resultCache[settings.Name];

            var utcNow = _clock.UtcNow;
            HealthCheckResult result = null;

            if (!checkCache.ShouldCheck(utcNow))
            {
                result = checkCache.Result;
            }
            else
            {
                using (CancellationTokenSource cts = new CancellationTokenSource(settings.Timeout))
                {
                    IHealthCheck check         = checkCache.Check;
                    var          healthContext = new HealthCheckContext(settings);
                    healthContext.CancellationToken = cts.Token;
                    try
                    {
                        await check.CheckHealthAsync(healthContext);

                        result = new HealthCheckResult
                        {
                            Name       = settings.Name,
                            Elapsed    = healthContext.ElapsedMilliseconds,
                            Message    = healthContext.Message,
                            Status     = healthContext.HasSucceeded ? HealthStatus.Healthy : healthContext.HasWarned ? HealthStatus.Warning : HealthStatus.Unhealthy,
                            Issued     = utcNow.ToUnixTimeSeconds(),
                            NextTry    = utcNow.AddSeconds(settings.Frequency),
                            Critical   = settings.Critical,
                            Properties = healthContext.Properties?.ToDictionary(kvp => kvp.Key, kvp => JToken.FromObject(kvp.Value))
                        };

                        checkCache.Result = result;
                    }
                    catch (Exception e)
                    {
                        result = new HealthCheckResult
                        {
                            Name      = settings.Name,
                            Elapsed   = healthContext.ElapsedMilliseconds,
                            Message   = "An error occured. See logs for more details.",
                            Status    = HealthStatus.Unhealthy,
                            Issued    = utcNow.ToUnixTimeSeconds(),
                            NextTry   = utcNow.AddSeconds(settings.Frequency),
                            Critical  = settings.Critical,
                            Exception = e
                        };
                        checkCache.Result = result;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Initializes a new instance of the type
        /// </summary>
        /// <param name="loggerFactory">Factory to create loggers</param>
        /// <param name="availabilityCheck">Logic to perform availability checks</param>
        /// <param name="healthCheck">Logic to perform health checks</param>
        public DualProbeLogic(ILoggerFactory loggerFactory, IAvailabilityCheck availabilityCheck, IHealthCheck healthCheck)
        {
            Guard.NotNull(nameof(loggerFactory), loggerFactory);
            Guard.NotNull(nameof(availabilityCheck), availabilityCheck);
            Guard.NotNull(nameof(healthCheck), healthCheck);

            _availabilityLogic = availabilityCheck;
            _healthLogic       = healthCheck;
            _logger            = loggerFactory.CreateLogger(Name);
        }
Ejemplo n.º 26
0
        public void UserIsNotFoundInRole()
        {
            IHealthCheck       healthCheck = BuildHealthCheckAndSetExpectations(false, true);
            IHealthCheckResult result      = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "not found in the necessary role.");

            m_MockRepository.VerifyAll();
        }
        public HealthCheckWrapper(IHealthCheck healthCheck, string name = null)
        {
            HealthCheck = healthCheck;

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

            Name = name;
        }
Ejemplo n.º 28
0
        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);
        }
Ejemplo n.º 29
0
        public HealthCheckMiddleware(RequestDelegate next, PathString healthCheckRoute, IHealthCheck healthCheck)
        {
            if (string.IsNullOrEmpty(healthCheckRoute))
            {
                throw new ArgumentException(nameof(healthCheckRoute));
            }

            _next             = next;
            _healthCheck      = healthCheck ?? throw new ArgumentException(nameof(healthCheck));
            _healthCheckRoute = healthCheckRoute;
        }
Ejemplo n.º 30
0
        public IHealthCheckBuilder AddCheck(IHealthCheck healthCheck)
        {
            if (healthCheck == null)
            {
                throw new ArgumentNullException(nameof(healthCheck));
            }

            _checks.Add(healthCheck);

            return(this);
        }
Ejemplo n.º 31
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);
        }
Ejemplo n.º 32
0
 private IndividualHealthCheckResult CheckIndividualInternal(IHealthCheck healthCheck, string healthCheckName, Stopwatch sw)
 {
     try
     {
         healthCheck.Check();
         return new SuccessfulIndividualHealthCheckResult(healthCheckName, healthCheck.Description, sw.Elapsed);
     }
     catch (Exception ex)
     {
         var message = "Health check {0} failed: {1}".FormatWith(healthCheckName, ex.Message);
         _logException(ex, message);
         return new FailedIndividualHealthCheckResult(healthCheckName, healthCheck.Description, ex.Message, sw.Elapsed);
     }
 }
Ejemplo n.º 33
0
        /// <summary>
        /// Gets the output message.
        /// </summary>
        /// <param name="result">if set to <c>true</c> health check was successful; otherwise <c>failed</c>.</param>
        /// <param name="hc">The hc.</param>
        /// <returns>The Formatted Message</returns>
        private HealthCheckMessage GetOutputMessage(bool result, IHealthCheck hc)
        {
            //Green Condition
            if (result && _failureTracking.ContainsKey(hc.Id))
            {
                var dt = _failureTracking[hc.Id];
                _failureTracking.Remove(hc.Id);
                return new HealthCheckMessage
                    {
                        Type = HealthCheckMessageType.CheckSucceeded,
                        Message = hc.GetSuccessMessage(dt),
                        SendMail = true
                    };
            }

            //Red Condition - New
            if (!result && !_failureTracking.ContainsKey(hc.Id))
            {
                _failureTracking.Add(hc.Id, DateTime.Now);
                return new HealthCheckMessage
                    {
                        Type = HealthCheckMessageType.CheckFailed,
                        Message = hc.GetFailureMessage(),
                        SendMail = true
                    };
            }

            //Red Condition - Repeated Issue
            if (!result && _failureTracking.ContainsKey(hc.Id))
            {
                var dt = _failureTracking[hc.Id];

                if (dt.AddMilliseconds(HeartBeatSettings.SettingsManager.RenotifyAfter) < DateTime.Now)
                {
                    //If the time it was logged at + the renotify after value < that now... alert again & bump the notification.
                    _failureTracking[hc.Id] = DateTime.Now;

                    return new HealthCheckMessage
                        {
                            Type = HealthCheckMessageType.CheckFailed,
                            Message = hc.GetFailureMessage(),
                            SendMail = true
                        };
                }

            }

            //Status Unchanged since last time - just log to file
            return new HealthCheckMessage
                {
                    Type = result ? HealthCheckMessageType.CheckSucceeded : HealthCheckMessageType.CheckFailed,
                    Message = result ? hc.GetSuccessMessage() : hc.GetFailureMessage(),
                    SendMail = false
                };
        }