Ejemplo n.º 1
0
        protected void SetupServices(
            IServiceCollection services,
            HealthEndpointsOptions healthMiddlewareCoreChecksOptions,
            IEnumerable <HealthCheckResult> healthChecks = null)
        {
            services.AddOptions();
            services.AddLogging();

            // TODO: scan for healthchecks
            // var startupAssemblyName = typeof(TestStartup).Assembly.GetName().Name;

            var builder = new HealthBuilder()
                          .Configuration.Configure(options => options.Enabled = true)
                          .OutputHealth.AsPlainText()
                          .OutputHealth.AsJson();

            var checks = healthChecks?.ToList() ?? new List <HealthCheckResult>();

            for (var i = 0; i < checks.Count; i++)
            {
                var check = checks[i];
                builder.HealthChecks.AddCheck("Check" + i, () => new ValueTask <HealthCheckResult>(check));
            }

            services.AddHealth(builder)
            .AddHealthEndpoints(
                options =>
            {
                options.HealthEndpointEnabled = healthMiddlewareCoreChecksOptions.HealthEndpointEnabled;
                options.PingEndpointEnabled   = healthMiddlewareCoreChecksOptions.PingEndpointEnabled;
                options.Timeout = healthMiddlewareCoreChecksOptions.Timeout;
            });
        }
Ejemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsMiddlewareHealthChecksOptions = new HealthEndpointsOptions {
                HealthEndpointEnabled = true
            };

            SetupServices(services, appMetricsMiddlewareHealthChecksOptions);
        }
        public void ConfigureServices(IServiceCollection services)
        {
            var healthEndpointsOptions = new HealthEndpointsOptions
            {
                PingEndpointEnabled = false
            };

            SetupServices(services, healthEndpointsOptions, healthChecks: new[] { HealthCheckResult.Healthy() });
        }
Ejemplo n.º 4
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsMiddlewareHelathCheckOptions = new HealthEndpointsOptions();

            SetupServices(
                services,
                appMetricsMiddlewareHelathCheckOptions,
                healthChecks: new[] { HealthCheckResult.Healthy() });
        }
Ejemplo n.º 5
0
        public void Can_load_settings_from_configuration()
        {
            var endpointOptions = new HealthEndpointsOptions();

            var    provider = SetupServicesAndConfiguration();
            Action resolveEndpointsOptions = () => { endpointOptions = provider.GetRequiredService <IOptions <HealthEndpointsOptions> >().Value; };

            resolveEndpointsOptions.Should().NotThrow();

            endpointOptions.HealthEndpointEnabled.Should().Be(false);
        }
Ejemplo n.º 6
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsMiddlewareHelathCheckOptions = new HealthEndpointsOptions {
                HealthEndpointEnabled = true
            };

            SetupServices(
                services,
                appMetricsMiddlewareHelathCheckOptions,
                healthChecks: new[] { HealthCheckResult.Healthy(), HealthCheckResult.Degraded(), HealthCheckResult.Unhealthy() });
        }
Ejemplo n.º 7
0
        public void Can_override_settings_from_configuration()
        {
            var options  = new HealthEndpointsOptions();
            var provider = SetupServicesAndConfiguration(
                o =>
            {
                o.HealthEndpointEnabled = true;
                o.Timeout = TimeSpan.FromDays(1);
            });

            Action resolveOptions = () => { options = provider.GetRequiredService <IOptions <HealthEndpointsOptions> >().Value; };

            resolveOptions.Should().NotThrow();
            options.HealthEndpointEnabled.Should().Be(true);
            options.PingEndpointEnabled.Should().Be(false);
            options.Timeout.Should().Be(TimeSpan.FromDays(1));
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Adds App Metrics Health services, configuration and middleware to the
        ///     <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" />.
        /// </summary>
        /// <param name="hostBuilder">The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" />.</param>
        /// <param name="setupDelegate">A callback to configure <see cref="HealthEndpointsOptions" />.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" /> cannot be null
        /// </exception>
        public static IWebHostBuilder UseHealthEndpoints(
            this IWebHostBuilder hostBuilder,
            Action <WebHostBuilderContext, HealthEndpointsOptions> setupDelegate)
        {
            hostBuilder.ConfigureHealth();

            hostBuilder.ConfigureServices(
                (context, services) =>
            {
                var endpointOptions = new HealthEndpointsOptions();
                services.AddHealthEndpoints(
                    options => setupDelegate(context, endpointOptions),
                    context.Configuration);
                services.AddSingleton <IStartupFilter>(new DefaultHealthEndpointsStartupFilter());
            });

            return(hostBuilder);
        }