/// <inheritdoc />
        public IHealthBuilder Using(IHealthOutputFormatter formatter)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            _metricsFormatter(true, formatter);

            return(Builder);
        }
        /// <inheritdoc />
        public IHealthBuilder Using(IHealthOutputFormatter formatter, bool replaceExisting)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            _metricsFormatter(replaceExisting, formatter);

            return(Builder);
        }
        public DefaultHealthResponseWriter(
            IHealthOutputFormatter fallbackFormatter,
            IReadOnlyCollection <IHealthOutputFormatter> formatters)
        {
            if (formatters == null)
            {
                throw new ArgumentNullException(nameof(formatters));
            }

            _formatters        = new HealthFormatterCollection(formatters.ToList());
            _fallbackFormatter = fallbackFormatter;
        }
Esempio n. 4
0
 public HealthRoot(
     IHealth health,
     HealthOptions options,
     HealthFormatterCollection metricsOutputFormatters,
     IHealthOutputFormatter defaultMetricsOutputFormatter,
     IRunHealthChecks healthCheckRunner)
 {
     Options = options ?? throw new ArgumentNullException(nameof(options));
     _health = health ?? throw new ArgumentNullException(nameof(health));
     OutputHealthFormatters       = metricsOutputFormatters ?? new HealthFormatterCollection();
     DefaultOutputHealthFormatter = defaultMetricsOutputFormatter;
     HealthCheckRunner            = healthCheckRunner;
 }
Esempio n. 5
0
 public HealthHandler(IHealthRoot healthRoot)
 {
     _healthRoot = healthRoot;
     _formatter  = _healthRoot.DefaultOutputHealthFormatter;
     _formatter  = healthRoot.OutputHealthFormatters
                   .OfType <HealthStatusTextOutputFormatter>()
                   //.OfType<HealthStatusJsonOutputFormatter>()
                   .SingleOrDefault();
     if (_formatter == null)
     {
         throw new ArgumentException("Include App.Metrics.Health!", nameof(healthRoot));
     }
 }
Esempio n. 6
0
        public JsonOutputFormatterTests(ITestOutputHelper output)
        {
            _output    = output;
            _formatter = new HealthStatusJsonOutputFormatter(DefaultJsonSerializerSettings.CreateSerializerSettings());

            var healthyOne   = new HealthCheck.Result("test_one_healthy", HealthCheckResult.Healthy("first check was good"));
            var healthyTwo   = new HealthCheck.Result("test_two_healthy", HealthCheckResult.Healthy("second check was good"));
            var unhealthyOne = new HealthCheck.Result("test_three_unhealthy", HealthCheckResult.Unhealthy("something failed"));
            var unhealthyTwo = new HealthCheck.Result("test_four_unhealthy", HealthCheckResult.Unhealthy("something else failed"));
            var degradedOne  = new HealthCheck.Result("test_five_degraded", HealthCheckResult.Degraded("degrading service"));

            var checks = new[] { healthyOne, healthyTwo, unhealthyOne, unhealthyTwo, degradedOne };

            _healthStatus = new HealthStatus(checks);
        }
        private static void UseHealthMiddleware(
            IApplicationBuilder app,
            IOptions <HealthEndpointsHostingOptions> endpointsHostingOptionsAccessor,
            IOptions <HealthEndpointsOptions> endpointsOptionsAccessor,
            HealthOptions metricsOptions,
            IHealthOutputFormatter formatter = null)
        {
            formatter = formatter ?? endpointsOptionsAccessor.Value.HealthEndpointOutputFormatter;

            app.UseWhen(
                context => ShouldUseHealthEndpoint(endpointsHostingOptionsAccessor, endpointsOptionsAccessor, metricsOptions, context),
                appBuilder =>
            {
                var responseWriter = HealthAspNetCoreHealthEndpointsServiceCollectionExtensions.ResolveHealthResponseWriter(app.ApplicationServices, formatter);
                appBuilder.UseMiddleware <HealthCheckEndpointMiddleware>(responseWriter, endpointsOptionsAccessor.Value.Timeout);
            });
        }
 // ReSharper disable UnusedMember.Global
 public DefaultHealthResponseWriter(IHealthOutputFormatter formatter)
 // ReSharper restore UnusedMember.Global
 {
     _formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
 }
        internal static IHealthResponseWriter ResolveHealthResponseWriter(IServiceProvider provider, IHealthOutputFormatter formatter = null)
        {
            var endpointOptions = provider.GetRequiredService <IOptions <HealthEndpointsOptions> >();
            var health          = provider.GetRequiredService <IHealthRoot>();

            if (health.Options.Enabled && endpointOptions.Value.HealthEndpointEnabled && health.OutputHealthFormatters.Any())
            {
                if (formatter == null)
                {
                    var fallbackFormatter = endpointOptions.Value.HealthEndpointOutputFormatter ?? health.DefaultOutputHealthFormatter;

                    return(new DefaultHealthResponseWriter(fallbackFormatter, health.OutputHealthFormatters));
                }

                return(new DefaultHealthResponseWriter(formatter, health.OutputHealthFormatters));
            }

            return(new NoOpHealthStatusResponseWriter());
        }
        /// <summary>
        ///     Adds App Metrics health endpoint middleware to the <see cref="IApplicationBuilder" /> request execution pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder" />.</param>
        /// <param name="formatter">
        ///     Overrides all configured <see cref="IHealthOutputFormatter" />, matching on accept headers
        ///     won't apply.
        /// </param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseHealthEndpoint(this IApplicationBuilder app, IHealthOutputFormatter formatter)
        {
            EnsureHealthAdded(app);

            var healthOptions = app.ApplicationServices.GetRequiredService <HealthOptions>();
            var endpointHostingOptionsAccessor = app.ApplicationServices.GetRequiredService <IOptions <HealthEndpointsHostingOptions> >();
            var endpointsOptionsAccessor       = app.ApplicationServices.GetRequiredService <IOptions <HealthEndpointsOptions> >();

            UseHealthMiddleware(app, endpointHostingOptionsAccessor, endpointsOptionsAccessor, healthOptions, formatter);

            return(app);
        }