Esempio n. 1
0
        /// <summary>
        /// Runs the health check, returning the status of the component being checked.
        /// </summary>
        /// <param name="context">A context object associated with the current execution.</param>
        /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> that can be used to cancel the health check.</param>
        /// <returns>
        /// A <see cref="T:System.Threading.Tasks.Task`1" /> that completes when the health check has finished, yielding the status of the component being checked.
        /// </returns>
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
                                                               CancellationToken cancellationToken = default)
        {
            var client      = new MemoryMetricsClient();
            var metrics     = client.GetMetrics();
            var percentUsed = 100 * metrics.Used / metrics.Total;

            var status = HealthStatus.Healthy;

            if (percentUsed > 80)
            {
                status = HealthStatus.Degraded;
            }

            if (percentUsed > 90)
            {
                status = HealthStatus.Unhealthy;
            }

            var data = new Dictionary <string, object>
            {
                { "Total", metrics.Total }, { "Used", metrics.Used }, { "Free", metrics.Free }
            };

            var result = new HealthCheckResult(status, null, null, data);

            return(await Task.FromResult(result));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var client  = new MemoryMetricsClient();
            var metrics = client.GetMetrics();

            var percentUsed = Math.Abs(metrics.Total) > 0 ? Math.Round(100 * metrics.Used / metrics.Total, 2) : 0;

            var status = HealthStatus.Healthy;

            var degraded  = _memoryHealthConfiguration?.Degraded ?? 80;
            var unhealthy = _memoryHealthConfiguration?.Unhealthy ?? 90;
            var message   = $"Healthy:{percentUsed} < {degraded}%";

            if (percentUsed > degraded || percentUsed == 0)
            {
                message = $"Degraded:{percentUsed}% Range:{unhealthy}% > {degraded}%";
                //TODO REVIEW status = HealthStatus.Degraded;
            }

            if (percentUsed > unhealthy)
            {
                message = $"Unhealthy:{percentUsed}%| > {degraded} (Degraded)%";
                //TODO REVIEW  status = HealthStatus.Unhealthy;
            }

            var data = new Dictionary <string, object>
            {
                { "Total", metrics.Total },
                { "Used", metrics.Used },
                { "Free", metrics.Free }
            };

            _logger.LogInformation("Metrics:{@data}", data);

            var result = new HealthCheckResult(status, message, null, data);

            return(await Task.FromResult(result));
        }