public HealthCheckResult Aggregate(IList <IHealthContributor> contributors, IOptionsMonitor <HealthCheckServiceOptions> healthServiceOptions, IServiceProvider serviceProvider) { var result = Aggregate(contributors); if (healthServiceOptions == null) { return(result); } var contributorIds = contributors.Select(x => x.Id); foreach (var registration in healthServiceOptions.CurrentValue.Registrations) { HealthCheckResult h = registration.HealthCheck(serviceProvider).GetAwaiter().GetResult(); if (h.Status > result.Status) { result.Status = h.Status; } var key = GetKey(result, registration.Name); result.Details.Add(key, h); var possibleDuplicate = contributorIds.FirstOrDefault(id => id.IndexOf(registration.Name, StringComparison.OrdinalIgnoreCase) >= 0); if (!string.IsNullOrEmpty(possibleDuplicate)) { var logger = serviceProvider.GetService(typeof(ILogger <HealthRegistrationsAggregator>)) as ILogger; logger?.LogDebug($"Possible duplicate HealthCheck registation {registration.Name}, {possibleDuplicate} "); } } return(result); }
public HealthCheckResult Aggregate(IList <IHealthContributor> contributors, ICollection <HealthCheckRegistration> healthCheckRegistrations, IServiceProvider serviceProvider) { // TODO: consider re-writing to run this call to base aggregator in parallel with below checks // get results from DefaultHealthAggregator first var aggregatorResult = Aggregate(contributors); // if there aren't any MSFT interfaced health checks, return now if (healthCheckRegistrations == null) { return(aggregatorResult); } var healthChecks = new ConcurrentDictionary <string, HealthCheckResult>(); var keyList = new ConcurrentBag <string>(contributors.Select(x => x.Id)); // run all HealthCheckRegistration checks in parallel Parallel.ForEach(healthCheckRegistrations, registration => { var contributorName = GetKey(keyList, registration.Name); HealthCheckResult healthCheckResult = null; try { healthCheckResult = registration.HealthCheck(serviceProvider).GetAwaiter().GetResult(); } catch (Exception) { healthCheckResult = new HealthCheckResult(); } healthChecks.TryAdd(contributorName, healthCheckResult); }); return(AddChecksSetStatus(aggregatorResult, healthChecks)); }
public HealthCheckResult Aggregate(IList <IHealthContributor> contributors) { if (contributors == null) { return(new HealthCheckResult()); } var aggregatorResult = new HealthCheckResult(); var healthChecks = new ConcurrentDictionary <string, HealthCheckResult>(); var keyList = new ConcurrentBag <string>(); Parallel.ForEach(contributors, contributor => { var contributorId = GetKey(keyList, contributor.Id); HealthCheckResult healthCheckResult = null; try { healthCheckResult = contributor.Health(); } catch (Exception) { healthCheckResult = new HealthCheckResult(); } healthChecks.TryAdd(contributorId, healthCheckResult); }); return(AddChecksSetStatus(aggregatorResult, healthChecks)); }
public HealthCheckResult Aggregate(IList <IHealthContributor> contributors) { if (contributors == null) { return(new HealthCheckResult()); } var result = new HealthCheckResult(); foreach (var contributor in contributors) { HealthCheckResult h = null; try { h = contributor.Health(); } catch (Exception) { h = new HealthCheckResult(); } if (h.Status > result.Status) { result.Status = h.Status; } string key = GetKey(result, contributor.Id); result.Details.Add(key, h.Details); } return(result); }
public static async Task <HealthCheckResult> HealthCheck(this HealthCheckRegistration registration, IServiceProvider provider) { var context = new HealthCheckContext { Registration = registration }; var healthCheckResult = new HealthCheckResult(); try { var res = await registration.Factory(provider).CheckHealthAsync(context); healthCheckResult = new HealthCheckResult() { Status = res.Status.ToHealthStatus(), Description = res.Description, Details = res.Data?.ToDictionary(i => i.Key, i => i.Value) }; if (res.Exception != null && !string.IsNullOrEmpty(res.Exception.Message)) { healthCheckResult.Details.Add("error", res.Exception.Message); } } catch (Exception) { } return(healthCheckResult); }
protected static string GetKey(HealthCheckResult result, string key) { // add the contribtor with a -n appended to the id if (result.Details.ContainsKey(key)) { return(string.Concat(key, "-", result.Details.Count(k => k.Key == key))); } return(key); }
protected HealthCheckResult AddChecksSetStatus(HealthCheckResult result, ConcurrentDictionary <string, HealthCheckResult> healthChecks) { foreach (var healthCheck in healthChecks) { if (healthCheck.Value.Status > result.Status) { result.Status = healthCheck.Value.Status; } result.Details.Add(healthCheck.Key, healthCheck.Value.Details); } return(result); }
public HealthCheckResult Health() { var count = _timeEntryRepository.List().Count(); var status = count < MaxTimeEntries ? UP : DOWN; var health = new HealthCheckResult { Status = status }; health.Details.Add("threshold", MaxTimeEntries); health.Details.Add("count", count); health.Details.Add("status", status.ToString()); return(health); }
protected virtual HealthCheckResult BuildHealth(IHealthAggregator aggregator, IList <IHealthContributor> contributors, ISecurityContext securityContext, IOptionsMonitor <HealthCheckServiceOptions> svcOptions, IServiceProvider provider) { var registrationAggregator = _aggregator as IHealthRegistrationsAggregator; var result = registrationAggregator == null ? _aggregator.Aggregate(contributors) : registrationAggregator.Aggregate(contributors, svcOptions, provider); var showDetails = Options.ShowDetails; if (showDetails == ShowDetails.Never || (showDetails == ShowDetails.WhenAuthorized && !securityContext.HasClaim(Options.Claim))) { result = new HealthCheckResult { Status = result.Status, Description = result.Description }; } return(result); }
public HealthCheckResult Aggregate(IList <IHealthContributor> contributors, IOptionsMonitor <HealthCheckServiceOptions> healthServiceOptions, IServiceProvider serviceProvider) { var result = Aggregate(contributors); if (healthServiceOptions == null) { return(result); } foreach (var registration in healthServiceOptions.CurrentValue.Registrations) { HealthCheckResult h = registration.HealthCheck(serviceProvider).Result; if (h.Status > result.Status) { result.Status = h.Status; } var key = GetKey(result, registration.Name); result.Details.Add(key, h); } return(result); }
public int GetStatusCode(HealthCheckResult health) { return(health.Status == HealthStatus.DOWN || health.Status == HealthStatus.OUT_OF_SERVICE ? 503 : 200); }