private async Task <HealthReportEntry> GetTimeoutEntry(CancellationToken ct, HealthCheckRegistration registration)
        {
            await Task.Delay(registration.Timeout ?? Timeout.InfiniteTimeSpan, ct);

            return(new HealthReportEntry(
                       HealthStatus.TimedOut,
                       description: $"The health check for registration {registration} has timed out ({registration.Timeout.Value.TotalMilliseconds} ms).",
                       registration.Timeout.Value,
                       exception: null,
                       data: null,
                       tags: null));
        }
Пример #2
0
        public IHealthCheckBuilder Register(
            string name,
            HealthCheckDelegate healthCheck,
            HealthStatus failureStatus = HealthStatus.Unhealthy,
            IEnumerable <string> tags  = null,
            TimeSpan?timeout           = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new InvalidOperationException("Please provide a name for this health check");
            }

            var registration = new HealthCheckRegistration(name, _ => new DelegatingHealthCheck(healthCheck), failureStatus, tags, timeout);

            _healthCheckRegistrations.Add(registration);

            return(this);
        }
        private async Task <HealthReportEntry> CheckHealthCore(CancellationToken ct, HealthCheckRegistration registration)
        {
            var          healthCheck = registration.Factory(_container);
            HealthStatus status;
            string       description;
            Exception    exception;
            IReadOnlyDictionary <string, object> data = null;

            var stopWatch = Stopwatch.StartNew();

            try
            {
                var result = await healthCheck.CheckHealth(ct, new HealthCheckContext
                {
                    Registration    = registration,
                    ServiceProvider = _container,
                });

                status      = result.Status;
                description = result.Description;
                exception   = result.Exception;
                data        = result.Data;
            }
            catch (NotImplementedException)
            {
                return(new HealthReportEntry(HealthStatus.NotImplemented, "This health check is not implemented.", stopWatch.Elapsed));
            }
            catch (Exception ex) when(!(ex is OperationCanceledException))
            {
                status      = registration.FailureStatus;
                description = ex.Message;
                exception   = ex;
                data        = ex.Data is IDictionary <string, object> dataValues ? new ReadOnlyDictionary <string, object>(dataValues) : default(ReadOnlyDictionary <string, object>);
            }

            stopWatch.Stop();

            return(new HealthReportEntry(status, description, stopWatch.Elapsed, exception, data, registration.Tags));
        }