/// <summary>
 /// Adds the Health actuator to the application
 /// </summary>
 /// <param name="hostBuilder">Your HostBuilder</param>
 /// <param name="aggregator">Custom health aggregator</param>
 /// <param name="contributors">Types that contribute to the overall health of the app</param>
 public static IHostBuilder AddHealthActuator(this IHostBuilder hostBuilder, IHealthAggregator aggregator, Type[] contributors)
 => hostBuilder
 .ConfigureServices((context, collection) =>
 {
     collection.AddHealthActuator(context.Configuration, aggregator, contributors);
     ActivateActuatorEndpoints(collection);
 });
 /// <summary>
 /// Adds the Health actuator to the application
 /// </summary>
 /// <param name="hostBuilder">Your HostBuilder</param>
 /// <param name="aggregator">Custom health aggregator</param>
 /// <param name="contributors">Types that contribute to the overall health of the app</param>
 public static IHostBuilder AddHealthActuator(this IHostBuilder hostBuilder, IHealthAggregator aggregator, Type[] contributors)
 {
     return(hostBuilder
            .ConfigureServices((context, collection) =>
     {
         collection.AddHealthActuator(context.Configuration, aggregator, contributors);
         collection.AddTransient <IStartupFilter, HealthStartupFilter>();
     }));
 }
Ejemplo n.º 3
0
 public HealthEndpointCore(IHealthOptions options, IHealthAggregator aggregator, IEnumerable <IHealthContributor> contributors, IOptionsMonitor <HealthCheckServiceOptions> serviceOptions, IServiceProvider provider, ILogger <HealthEndpoint> logger = null)
     : base(options, aggregator, contributors, logger)
 {
     _aggregator     = aggregator ?? throw new ArgumentNullException(nameof(aggregator));
     _serviceOptions = serviceOptions ?? throw new ArgumentNullException(nameof(serviceOptions));
     _provider       = provider ?? throw new ArgumentNullException(nameof(provider));
     _contributors   = contributors.ToList();
     _logger         = logger;
 }
Ejemplo n.º 4
0
        public HealthEndpoint(IHealthOptions options, IHealthAggregator aggregator, IEnumerable <IHealthContributor> contributors, ILogger <HealthEndpoint> logger = null)
            : base(options)
        {
            if (contributors == null)
            {
                throw new ArgumentNullException(nameof(contributors));
            }

            _aggregator   = aggregator ?? throw new ArgumentNullException(nameof(aggregator));
            _contributors = contributors.ToList();
            _logger       = logger;
        }
Ejemplo n.º 5
0
        public static void UseHealthActuator(IConfiguration configuration, IHealthAggregator healthAggregator = null, IEnumerable <IHealthContributor> contributors = null, ILoggerFactory loggerFactory = null)
        {
            var options = new HealthOptions(configuration);

            healthAggregator = healthAggregator ?? new DefaultHealthAggregator();
            contributors     = contributors ?? new List <IHealthContributor>()
            {
                new DiskSpaceContributor(new DiskSpaceContributorOptions(configuration))
            };
            var ep      = new HealthEndpoint(options, healthAggregator, contributors, CreateLogger <HealthEndpoint>(loggerFactory));
            var handler = new HealthHandler(ep, SecurityService, CreateLogger <HealthHandler>(loggerFactory));

            ConfiguredHandlers.Add(handler);
        }
Ejemplo n.º 6
0
        public void AddHealthActuator_ThrowsOnNulls()
        {
            IServiceCollection services = new ServiceCollection();
            var config = new ConfigurationBuilder().Build();
            IHealthAggregator aggregator = null;

            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(null));

            Assert.Equal("services", ex.ParamName);
            var ex2 = Assert.Throws <ArgumentNullException>(() => services.AddHealthActuator());

            Assert.Equal("config", ex2.ParamName);
            var ex3 = Assert.Throws <ArgumentNullException>(() => services.AddHealthActuator(config, aggregator));

            Assert.Contains(nameof(aggregator), ex3.Message);
        }
Ejemplo n.º 7
0
        protected virtual HealthCheckResult BuildHealth(IHealthAggregator aggregator, IList <IHealthContributor> contributors, ISecurityContext securityContext)
        {
            var result      = _aggregator.Aggregate(contributors);
            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 void AddHealthActuator_ThrowsOnNulls()
        {
            // Arrange
            IServiceCollection services2 = new ServiceCollection();
            var config2 = new ConfigurationBuilder().Build();
            IHealthAggregator aggregator = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(null));

            Assert.Contains("services", ex.Message);
            var ex2 = Assert.Throws <InvalidOperationException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2));

            Assert.Equal("No service for type 'Microsoft.Extensions.Configuration.IConfiguration' has been registered.", ex2.Message);
            var ex3 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2, config2, aggregator));

            Assert.Contains(nameof(aggregator), ex3.Message);
        }
        public void AddHealthActuator_ThrowsOnNulls()
        {
            // Arrange
            IServiceCollection services   = null;
            IServiceCollection services2  = new ServiceCollection();
            IConfigurationRoot config     = null;
            IConfigurationRoot config2    = new ConfigurationBuilder().Build();
            IHealthAggregator  aggregator = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services, config));

            Assert.Contains(nameof(services), ex.Message);
            var ex2 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2, config));

            Assert.Contains(nameof(config), ex2.Message);
            var ex3 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2, config2, aggregator));

            Assert.Contains(nameof(aggregator), ex3.Message);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Adds components of the Health actuator to Microsoft-DI
        /// </summary>
        /// <param name="services">Service collection to add health to</param>
        /// <param name="config">Application configuration. Retrieved from the <see cref="IServiceCollection"/> if not provided (this actuator looks for a settings starting with management:endpoints:health)</param>
        /// <param name="aggregator">Custom health aggregator</param>
        /// <param name="contributors">Contributors to application health</param>
        public static void AddHealthActuator(this IServiceCollection services, IConfiguration config, IHealthAggregator aggregator, params Type[] contributors)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            config ??= services.BuildServiceProvider().GetService <IConfiguration>();
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (aggregator == null)
            {
                throw new ArgumentNullException(nameof(aggregator));
            }

            services.AddActuatorManagementOptions(config);
            services.AddHealthActuatorServices(config);

            AddHealthContributors(services, contributors);

            services.TryAddSingleton(aggregator);
            services.TryAddScoped <HealthEndpointCore>();
            services.AddActuatorEndpointMapping <HealthEndpointCore>();
            services.TryAddSingleton <ApplicationAvailability>();
        }
        /// <summary>
        /// Adds components of the Health actuator to Microsoft-DI
        /// </summary>
        /// <param name="services">Service collection to add health to</param>
        /// <param name="config">Application configuration (this actuator looks for a settings starting with management:endpoints:health)</param>
        /// <param name="aggregator">Custom health aggregator</param>
        /// <param name="contributors">Contributors to application health</param>
        public static void AddHealthActuator(this IServiceCollection services, IConfiguration config, IHealthAggregator aggregator, params Type[] contributors)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (aggregator == null)
            {
                throw new ArgumentNullException(nameof(aggregator));
            }

            services.TryAddSingleton <IHealthOptions>(new HealthOptions(config));
            AddHealthContributors(services, contributors);
            services.TryAddSingleton <IHealthAggregator>(aggregator);
            services.TryAddScoped <HealthEndpoint>();
        }
Ejemplo n.º 13
0
 public TestHealthEndpoint(IHealthOptions options, IHealthAggregator aggregator, IEnumerable <IHealthContributor> contributors, ILogger <HealthEndpoint> logger = null)
     : base(options, aggregator, contributors, logger)
 {
 }
Ejemplo n.º 14
0
 protected virtual Health BuildHealth(IHealthAggregator aggregator, IList <IHealthContributor> contributors)
 {
     return(_aggregator.Aggregate(contributors));
 }
        /// <summary>
        /// Add HealthCheck middleware to OWIN Pipeline
        /// </summary>
        /// <param name="builder">OWIN <see cref="IAppBuilder" /></param>
        /// <param name="options"><see cref="IHealthOptions"/> for configuring Health endpoint</param>
        /// <param name="aggregator"><see cref="IHealthAggregator"/> for determining how to report aggregate health of the application</param>
        /// <param name="loggerFactory">For logging within the middleware</param>
        /// <returns>OWIN <see cref="IAppBuilder" /> with Health Endpoint added</returns>
        public static IAppBuilder UseHealthActuator(this IAppBuilder builder, IHealthOptions options, IHealthAggregator aggregator, ILoggerFactory loggerFactory = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (aggregator == null)
            {
                throw new ArgumentNullException(nameof(aggregator));
            }

            return(builder.UseHealthActuator(options, aggregator, GetDefaultHealthContributors(), loggerFactory));
        }
        /// <summary>
        /// Add HealthCheck middleware to OWIN Pipeline
        /// </summary>
        /// <param name="builder">OWIN <see cref="IAppBuilder" /></param>
        /// <param name="options"><see cref="IHealthOptions"/> for configuring Health endpoint</param>
        /// <param name="aggregator"><see cref="IHealthAggregator"/> for determining how to report aggregate health of the application</param>
        /// <param name="contributors">A list of <see cref="IHealthContributor"/> to monitor for determining application health</param>
        /// <param name="loggerFactory">For logging within the middleware</param>
        /// <returns>OWIN <see cref="IAppBuilder" /> with Health Endpoint added</returns>
        public static IAppBuilder UseHealthActuator(this IAppBuilder builder, IHealthOptions options, IHealthAggregator aggregator, IEnumerable <IHealthContributor> contributors, ILoggerFactory loggerFactory = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (aggregator == null)
            {
                throw new ArgumentNullException(nameof(aggregator));
            }

            if (contributors == null)
            {
                throw new ArgumentNullException(nameof(contributors));
            }

            var mgmtOptions = ManagementOptions.Get();

            foreach (var mgmt in mgmtOptions)
            {
                if (!mgmt.EndpointOptions.Contains(options))
                {
                    mgmt.EndpointOptions.Add(options);
                }
            }

            var endpoint = new HealthEndpoint(options, aggregator, contributors, loggerFactory?.CreateLogger <HealthEndpoint>());
            var logger   = loggerFactory?.CreateLogger <HealthEndpointOwinMiddleware>();

            return(builder.Use <HealthEndpointOwinMiddleware>(endpoint, mgmtOptions, logger));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Adds components of the Health actuator to Microsoft-DI
        /// </summary>
        /// <param name="services">Service collection to add health to</param>
        /// <param name="config">Application configuration (this actuator looks for a settings starting with management:endpoints:health)</param>
        /// <param name="aggregator">Custom health aggregator</param>
        /// <param name="contributors">Contributors to application health</param>
        public static void AddHealthActuator(this IServiceCollection services, IConfiguration config, IHealthAggregator aggregator, params Type[] contributors)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (aggregator == null)
            {
                throw new ArgumentNullException(nameof(aggregator));
            }

            services.TryAddEnumerable(ServiceDescriptor.Singleton <IManagementOptions>(new ActuatorManagementOptions(config)));
            services.TryAddEnumerable(ServiceDescriptor.Singleton <IManagementOptions>(new CloudFoundryManagementOptions(config)));

            var options = new HealthEndpointOptions(config);

            services.TryAddSingleton <IHealthOptions>(options);
            services.RegisterEndpointOptions(options);
            AddHealthContributors(services, contributors);

            services.TryAddSingleton <IHealthAggregator>(aggregator);
            services.TryAddScoped <HealthEndpointCore>();
        }
        /// <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>();
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Adds components of the Health actuator to Microsoft-DI
        /// </summary>
        /// <param name="services">Service collection to add health to</param>
        /// <param name="config">Application configuration (this actuator looks for a settings starting with management:endpoints:health)</param>
        /// <param name="aggregator">Custom health aggregator</param>
        /// <param name="contributors">Contributors to application health</param>
        public static void AddHealthActuator(this IServiceCollection services, IConfiguration config, IHealthAggregator aggregator, params Type[] contributors)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (aggregator == null)
            {
                throw new ArgumentNullException(nameof(aggregator));
            }

            services.AddActuatorManagementOptions(config);

            var options = new HealthEndpointOptions(config);

            services.TryAddSingleton <IHealthOptions>(options);
            services.RegisterEndpointOptions(options);
            AddHealthContributors(services, contributors);

            services.TryAddSingleton(aggregator);
            services.TryAddScoped <HealthEndpointCore>();
            services.TryAddSingleton <ApplicationAvailability>();
        }
        /// <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>();
        }