Пример #1
0
        public void Add(string name, CheckStatus status, string description, Dictionary <string, object> data)
        {
            Guard.ArgumentNotNullOrEmpty(nameof(name), name);
            Guard.ArgumentValid(status != CheckStatus.Unknown, nameof(status), "Cannot add 'Unknown' status to composite health check result.");
            Guard.ArgumentNotNullOrEmpty(nameof(description), description);

            _results.Add(name, HealthCheckResult.FromStatus(status, description, data));
        }
Пример #2
0
        public static async ValueTask <IHealthCheckResult> DefaultUrlCheck(HttpResponseMessage response)
        {
            var status = response.IsSuccessStatusCode ? CheckStatus.Healthy : CheckStatus.Unhealthy;
            var data   = new Dictionary <string, object>
            {
                { "url", response.RequestMessage.RequestUri.ToString() },
                { "status", (int)response.StatusCode },
                { "reason", response.ReasonPhrase },
                { "body", await response.Content?.ReadAsStringAsync() }
            };

            return(HealthCheckResult.FromStatus(status, $"status code {response.StatusCode} ({(int)response.StatusCode})", data));
        }
Пример #3
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);
        }
Пример #4
0
        // Numeric checks

        public static HealthCheckBuilder AddMinValueCheck <T>(this HealthCheckBuilder builder, string name, T minValue, Func <T> currentValueFunc)
            where T : IComparable <T>
        {
            Guard.ArgumentNotNull(nameof(builder), builder);
            Guard.ArgumentNotNullOrWhitespace(nameof(name), name);
            Guard.ArgumentNotNull(nameof(currentValueFunc), currentValueFunc);

            builder.AddCheck(name, () =>
            {
                var currentValue = currentValueFunc();
                var status       = currentValue.CompareTo(minValue) >= 0 ? CheckStatus.Healthy : CheckStatus.Unhealthy;
                return(HealthCheckResult.FromStatus(
                           status,
                           $"{name}: min={minValue}, current={currentValue}",
                           new Dictionary <string, object> {
                    { "min", minValue }, { "current", currentValue }
                }
                           ));
            });

            return(builder);
        }