internal HealthOutputFormattingBuilder(
     IHealthBuilder healthBuilder,
     Action <bool, IHealthOutputFormatter> metricsFormatter)
 {
     Builder           = healthBuilder ?? throw new ArgumentNullException(nameof(healthBuilder));
     _metricsFormatter = metricsFormatter ?? throw new ArgumentNullException(nameof(metricsFormatter));
 }
Ejemplo n.º 2
0
 internal HealthCheckBuilder(
     IHealthBuilder healthBuilder,
     Action <HealthCheck> healthCheck)
 {
     Builder      = healthBuilder ?? throw new ArgumentNullException(nameof(healthBuilder));
     _healthCheck = healthCheck ?? throw new ArgumentNullException(nameof(healthCheck));
 }
Ejemplo n.º 3
0
 public HealthReportingBuilder(
     IHealthBuilder healthBuilder,
     Action <IReportHealthStatus> reporters)
 {
     Builder    = healthBuilder ?? throw new ArgumentNullException(nameof(healthBuilder));
     _reporters = reporters ?? throw new ArgumentNullException(nameof(reporters));
 }
Ejemplo n.º 4
0
 internal HealthConfigurationBuilder(
     IHealthBuilder healthBuilder,
     HealthOptions currentOptions,
     Action <HealthOptions> setupAction)
 {
     Builder      = healthBuilder ?? throw new ArgumentNullException(nameof(healthBuilder));
     _setupAction = setupAction ?? throw new ArgumentNullException(nameof(setupAction));
     _options     = currentOptions ?? new HealthOptions();
 }
Ejemplo n.º 5
0
        public static IServiceCollection AddHealth(this IServiceCollection services, IHealthBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var health = builder.Build();

            AddCoreServices(services, health);

            return(services);
        }
Ejemplo n.º 6
0
        private static IHealthRoot BuildHealthRoot(IHealthBuilder builder)
        {
            //配置Health配置信息
            builder.Configuration.Configure(HealthConfiger);

            builder.Report.ToMetrics(Metrics);

            builder.HealthChecks.AddProcessPrivateMemorySizeCheck("Private Memory Size", 200);
            builder.HealthChecks.AddProcessVirtualMemorySizeCheck("Virtual Memory Size", 200);
            builder.HealthChecks.AddProcessPhysicalMemoryCheck("Working Set", 200);

            return(builder.Build());
        }
        private static void SetupChecks(Checks checks, IHealthBuilder healthBuilder)
        {
            void AddMemoryCheck(string name, MemoryCheck memoryCheck, Func <string, long, bool, IHealthBuilder> addAction)
            {
                if (memoryCheck.ThresholdMB > 0)
                {
                    addAction(name, memoryCheck.ThresholdMB * MemoryCheck.Unit.MB, memoryCheck.Degraded);
                }
            }

            if (checks == null || !checks.Memory.Enabled)
            {
                return;
            }

            AddMemoryCheck("Private memory size",
                           checks.Memory.Private,
                           healthBuilder.HealthChecks.AddProcessPrivateMemorySizeCheck);

            AddMemoryCheck("Physical memory size",
                           checks.Memory.Physical,
                           healthBuilder.HealthChecks.AddProcessPhysicalMemoryCheck);

            AddMemoryCheck("Virtual memory size",
                           checks.Memory.Virtual,
                           healthBuilder.HealthChecks.AddProcessVirtualMemorySizeCheck);


            // Ping Checks
            foreach (var(k, v) in checks.Ping)
            {
                healthBuilder.HealthChecks.AddPingCheck($"PING Check {k}", v.Endpoint, v.Timeout, v.Degraded);
            }

            // Http Checks
            foreach (var(k, v) in checks.Http)
            {
                if (Uri.TryCreate(v.Endpoint, UriKind.Absolute, out var uri))
                {
                    healthBuilder.HealthChecks.AddHttpGetCheck($"HTTP Check {k}", uri, v.Timeout, v.Degraded);
                }
            }
        }
        public static IHealthRoot BuildAndAddTo(
            this IHealthBuilder builder,
            IServiceCollection services)
        {
            var health = builder.Build();

            services.TryAddSingleton <IHealth>(provider =>
            {
                var checks = ResolveAllHealthChecks(provider, health);
                return(new DefaultHealth(checks));
            });
            services.TryAddSingleton <IHealthRoot>(
                provider =>
            {
                var resolvedHealth             = provider.GetRequiredService <IHealth>();
                var resolvedHealthChecksRunner = provider.GetRequiredService <IRunHealthChecks>();

                return(new HealthRoot(
                           resolvedHealth,
                           health.Options,
                           health.OutputHealthFormatters as HealthFormatterCollection,
                           health.DefaultOutputHealthFormatter,
                           resolvedHealthChecksRunner,
                           health.Reporters as HealthReporterCollection));
            });
            services.TryAddSingleton <IRunHealthChecks>(provider =>
            {
                var checks = ResolveAllHealthChecks(provider, health).ToList();

                if (!health.Options.Enabled || !checks.Any())
                {
                    return(new NoOpHealthCheckRunner());
                }

                return(new DefaultHealthCheckRunner(checks));
            });

            AppMetricsHealthServiceCollectionExtensions.AddCoreServices(services, health);

            return(health);
        }
        private static void SetupReports(Reports reports, IServiceProvider serviceProvider, IHealthBuilder healthBuilder)
        {
            if (reports == null || !reports.Enabled)
            {
                return;
            }

            if (reports.Metric.Enabled)
            {
                var metricRoot = serviceProvider.GetService <IMetricsRoot>();
                healthBuilder.Report.ToMetrics(metricRoot);
            }

            if (reports.Slack.Enabled)
            {
                healthBuilder.Report.ToSlack(reports.Slack);
            }
        }