Beispiel #1
0
        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));
        }
Beispiel #2
0
 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;
 }
Beispiel #3
0
        /// <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;
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        /// <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);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
 public static HealthCheckBuilder AddVirtualMemorySizeCheck(this HealthCheckBuilder builder, long maxSize, TimeSpan cacheDuration)
 => AddMaxValueCheck(builder, $"VirtualMemorySize({maxSize})", maxSize, () => Process.GetCurrentProcess().VirtualMemorySize64, cacheDuration);
Beispiel #8
0
        // System checks

        public static HealthCheckBuilder AddPrivateMemorySizeCheck(this HealthCheckBuilder builder, long maxSize)
        => AddMaxValueCheck(builder, $"PrivateMemorySize({maxSize})", maxSize, () => Process.GetCurrentProcess().PrivateMemorySize64);
Beispiel #9
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));
        }
Beispiel #10
0
        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));
        }
Beispiel #11
0
        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));
        }
Beispiel #12
0
        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));
        }
Beispiel #13
0
 public static HealthCheckBuilder AddUrlCheck(this HealthCheckBuilder builder, string url, TimeSpan cacheDuration)
 => AddUrlCheck(builder, url, response => UrlChecker.DefaultUrlCheck(response), cacheDuration);
Beispiel #14
0
        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));
        }
Beispiel #15
0
        // 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));
        }
Beispiel #16
0
 public static HealthCheckBuilder AddWorkingSetCheck(this HealthCheckBuilder builder, long maxSize, TimeSpan cacheDuration)
 => AddMaxValueCheck(builder, $"WorkingSet({maxSize})", maxSize, () => Process.GetCurrentProcess().WorkingSet64, cacheDuration);
Beispiel #17
0
        // 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));
        }
Beispiel #18
0
        // Default URL check

        public static HealthCheckBuilder AddUrlCheck(this HealthCheckBuilder builder, string url)
        {
            Guard.ArgumentNotNull(nameof(builder), builder);

            return(AddUrlCheck(builder, url, builder.DefaultCacheDuration));
        }