Exemplo n.º 1
0
        public static IHealthCheckFactory RegisterHttpGetHealthCheck(
            this IHealthCheckFactory factory,
            string name,
            Uri uri,
            TimeSpan timeout,
            CancellationToken token = default(CancellationToken))
        {
            factory.Register(
                name,
                async() =>
            {
                using (var tokenWithTimeout = CancellationTokenSource.CreateLinkedTokenSource(token))
                {
                    tokenWithTimeout.CancelAfter(timeout);

                    var response = await HttpClient.GetAsync(uri, tokenWithTimeout.Token).ConfigureAwait(false);

                    return(response.IsSuccessStatusCode
                            ? HealthCheckResult.Healthy($"OK. {uri}")
                            : HealthCheckResult.Unhealthy($"FAILED. {uri} status code was {response.StatusCode}"));
                }
            });

            return(factory);
        }
Exemplo n.º 2
0
        public static IHealthCheckFactory RegisterPingHealthCheck(
            this IHealthCheckFactory factory,
            string name,
            string host,
            TimeSpan timeout,
            bool degradedOnError = false)
        {
            factory.Register(
                name,
                async() =>
            {
                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));
                }
            });

            return(factory);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Registers a health check on the process confirming that the current amount of physical memory is below the
        ///     threshold.
        /// </summary>
        /// <param name="factory">The health check factory where the health check is registered.</param>
        /// <param name="name">The name of the health check.</param>
        /// <param name="thresholdBytes">The physical memory threshold in bytes.</param>
        /// <returns>The health check factory instance</returns>
        public static IHealthCheckFactory RegisterProcessPhysicalMemoryHealthCheck(this IHealthCheckFactory factory, string name, long thresholdBytes)
        {
            factory.Register(name, () =>
            {
                var currentSize = Process.GetCurrentProcess().WorkingSet64;
                return(Task.FromResult(currentSize <= thresholdBytes
                    ? HealthCheckResult.Healthy($"OK. {thresholdBytes} bytes")
                    : HealthCheckResult.Unhealthy($"FAILED. {currentSize} > {thresholdBytes}")));
            });

            return(factory);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Registers a health check on the process confirming that the current amount of virtual memory is below the
        ///     threshold.
        /// </summary>
        /// <param name="factory">The health check factory where the health check is registered.</param>
        /// <param name="name">The name of the health check.</param>
        /// <param name="thresholdBytes">The virtual memory threshold in bytes.</param>
        /// <param name="degradedOnError">
        ///     WHen the check fails, if true, create a degraded status response.
        ///     Otherwise create an unhealthy status response. (default: false)
        /// </param>
        /// <returns>The health check factory instance</returns>
        public static IHealthCheckFactory RegisterProcessVirtualMemorySizeHealthCheck(
            this IHealthCheckFactory factory,
            string name,
            long thresholdBytes,
            bool degradedOnError = false)
        {
            factory.Register(
                name,
                () =>
            {
                var currentSize = Process.GetCurrentProcess().VirtualMemorySize64;
                return(Task.FromResult(
                           currentSize <= thresholdBytes
                            ? HealthCheckResult.Healthy($"OK. {thresholdBytes} bytes")
                            : HealthCheckResultOnError($"FAILED. {currentSize} > {thresholdBytes} bytes", degradedOnError)));
            });

            return(factory);
        }
Exemplo n.º 5
0
        public static IHealthCheckFactory RegisterPingHealthCheck(
            this IHealthCheckFactory factory,
            string name,
            string host,
            TimeSpan timeout)
        {
            factory.Register(
                name,
                async() =>
            {
                var ping   = new Ping();
                var result = await ping.SendPingAsync(host, (int)timeout.TotalMilliseconds).ConfigureAwait(false);

                return(result.Status == IPStatus.Success
                        ? HealthCheckResult.Healthy($"OK. {host}")
                        : HealthCheckResult.Unhealthy($"FAILED. {host} ping result was {result.Status}"));
            });

            return(factory);
        }