public IActionResult Get() { try { var data = new List <string> { $"Contributors Count: {healthContributors.Count}" }; var overallHealth = new DefaultHealthAggregator().Aggregate(healthContributors); data.Add($"Status: {overallHealth.Status}"); data.Add($"Description: {overallHealth.Description}"); foreach (var detail in overallHealth.Details) { data.Add($"{detail.Key}: {JsonConvert.SerializeObject(detail.Value)}"); } if (overallHealth.Status == HealthStatus.UP) { return(new OkObjectResult(data)); } return(StatusCode((int)HttpStatusCode.InternalServerError, data)); } catch (Exception ex) { logger.LogError(ex.ToString()); return(StatusCode((int)HttpStatusCode.InternalServerError, ex)); } }
/// <summary> /// Register the Health endpoint, OWIN middleware and options /// </summary> /// <param name="container">Autofac DI <see cref="ContainerBuilder"/></param> /// <param name="config">Your application's <see cref="IConfiguration"/></param> /// <param name="aggregator">Your <see cref="IHealthAggregator"/></param> /// <param name="contributors">Types that implement <see cref="IHealthContributor"/></param> public static void RegisterHealthActuator(this ContainerBuilder container, IConfiguration config, IHealthAggregator aggregator, params Type[] contributors) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } if (aggregator == null) { aggregator = new DefaultHealthAggregator(); } container.RegisterInstance(new HealthOptions(config)).As <IHealthOptions>().SingleInstance(); container.RegisterInstance(aggregator).As <IHealthAggregator>().SingleInstance(); foreach (var c in contributors) { container.RegisterType(c).As <IHealthContributor>(); } container.RegisterType <HealthEndpoint>(); container.RegisterType <HealthEndpointOwinMiddleware>(); }
public void Aggregate_NullContributorList_ReturnsExpectedHealth() { var agg = new DefaultHealthAggregator(); var result = agg.Aggregate(null); Assert.NotNull(result); Assert.Equal(HealthStatus.UNKNOWN, result.Status); Assert.NotNull(result.Details); }
public void Invoke_NoContributors_ReturnsExpectedHealth() { var opts = new HealthEndpointOptions(); var contributors = new List <IHealthContributor>(); var agg = new DefaultHealthAggregator(); var ep = new HealthEndpoint(opts, agg, contributors, GetLogger <HealthEndpoint>()); var health = ep.Invoke(null); Assert.NotNull(health); Assert.Equal(HealthStatus.UNKNOWN, health.Status); }
public void Aggregate_SingleContributor_ReturnsExpectedHealth() { List <IHealthContributor> contribs = new List <IHealthContributor>() { new UpContributor() }; var agg = new DefaultHealthAggregator(); var result = agg.Aggregate(contribs); Assert.NotNull(result); Assert.Equal(HealthStatus.UP, result.Status); Assert.NotNull(result.Details); }
public void Aggregate_DuplicateContributor_ReturnsExpectedHealth() { var contribs = new List <IHealthContributor>() { new UpContributor(), new UpContributor() }; var agg = new DefaultHealthAggregator(); var result = agg.Aggregate(contribs); Assert.NotNull(result); Assert.Equal(HealthStatus.UP, result.Status); Assert.Contains("Up-1", result.Details.Keys); }
public void Aggregate_MultipleContributor_OrderDoesntMatter_ReturnsExpectedHealth() { List <IHealthContributor> contribs = new List <IHealthContributor>() { new UpContributor(), new OutOfSserviceContributor(), new UnknownContributor() }; var agg = new DefaultHealthAggregator(); var result = agg.Aggregate(contribs); Assert.NotNull(result); Assert.Equal(HealthStatus.OUT_OF_SERVICE, result.Status); Assert.NotNull(result.Details); }
public void AggregatesInParallel() { var t = new Stopwatch(); var contribs = new List <IHealthContributor>() { new UpContributor(500), new UpContributor(500), new UpContributor(500) }; var agg = new DefaultHealthAggregator(); t.Start(); var result = agg.Aggregate(contribs); t.Stop(); Assert.NotNull(result); Assert.Equal(HealthStatus.UP, result.Status); Assert.InRange(t.ElapsedMilliseconds, 450, 1200); }
/// <summary> /// Register the Health endpoint, OWIN middleware and options /// </summary> /// <param name="container">Autofac DI <see cref="ContainerBuilder"/></param> /// <param name="config">Your application's <see cref="IConfiguration"/></param> /// <param name="aggregator">Your <see cref="IHealthAggregator"/></param> /// <param name="contributors">Types that implement <see cref="IHealthContributor"/></param> public static void RegisterHealthActuator(this ContainerBuilder container, IConfiguration config, IHealthAggregator aggregator, params Type[] contributors) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } if (aggregator == null) { aggregator = new DefaultHealthAggregator(); } container.Register(c => { var options = new HealthEndpointOptions(config); var mgmtOptions = c.Resolve <IEnumerable <IManagementOptions> >(); foreach (var mgmt in mgmtOptions) { mgmt.EndpointOptions.Add(options); } return(options); }).As <IHealthOptions>().IfNotRegistered(typeof(IHealthOptions)).SingleInstance(); container.RegisterInstance(aggregator).As <IHealthAggregator>().SingleInstance(); foreach (var c in contributors) { container.RegisterType(c).As <IHealthContributor>(); } container.RegisterType <HealthEndpoint>(); container.RegisterType <HealthEndpointOwinMiddleware>(); }