コード例 #1
0
        public void can_report_health_checks()
        {
            var expected   = StringReporterSamples.HealthChecks.ExtractStringReporterSampleFromResourceFile();
            var globalTags = new GlobalMetricTags(new Dictionary <string, string> {
                { "tag_key", "tag_value" }
            });

            var healthyChecks = new[]
            {
                new HealthCheck.Result("healthy check", HealthCheckResult.Healthy("healthy message"))
            }.AsEnumerable();

            var degradedChecks = new[]
            {
                new HealthCheck.Result("degraded check", HealthCheckResult.Degraded("degraded message"))
            }.AsEnumerable();

            var unhealthyChecks = new[]
            {
                new HealthCheck.Result("unhealthy check", HealthCheckResult.Unhealthy("unhealthy message"))
            }.AsEnumerable();

            var sr = new StringReporter();

            sr.ReportHealth(globalTags, healthyChecks, degradedChecks, unhealthyChecks);

            AssertReportResult(sr.Result, expected);
        }
コード例 #2
0
        public void Can_set_global_tags_on_metric_options()
        {
            // Arrange
            var environmentBuilder = new EnvironmentInfoProvider();
            var environmentInfo    = environmentBuilder.Build();
            var expected           = new GlobalMetricTags(
                new Dictionary <string, string>
            {
                { "machine_name", environmentInfo.MachineName },
                { "app_name", environmentInfo.EntryAssemblyName },
                { "app_version", environmentInfo.EntryAssemblyVersion }
            });
            var options = new MetricsOptions();

            // Act
            options.WithGlobalTags(
                (globalTags, envInfo) =>
            {
                globalTags.Add("machine_name", envInfo.MachineName);
                globalTags.Add("app_name", envInfo.EntryAssemblyName);
                globalTags.Add("app_version", envInfo.EntryAssemblyVersion);
            });

            // Assert
            options.GlobalTags.Should().Equal(expected);
        }
コード例 #3
0
        public void ReportHealth(GlobalMetricTags globalTags,
                                 IEnumerable <HealthCheck.Result> healthyChecks,
                                 IEnumerable <HealthCheck.Result> degradedChecks,
                                 IEnumerable <HealthCheck.Result> unhealthyChecks)
        {
            var passed   = healthyChecks.ToList();
            var failed   = unhealthyChecks.ToList();
            var degraded = degradedChecks.ToList();

            var status = Constants.Health.DegradedStatusDisplay;

            if (!degraded.Any() && !failed.Any())
            {
                status = Constants.Health.HealthyStatusDisplay;
            }

            if (failed.Any())
            {
                status = Constants.Health.UnhealthyStatusDisplay;
            }

            _buffer.WriteHealthStatus(status);

            _buffer.WritePassedHealthChecksHeader();
            ;
            passed.ForEach(c => _buffer.WriteHealthCheckResult(c));

            _buffer.WriteDegradedHealthChecksHeader();
            ;
            degraded.ForEach(c => _buffer.WriteHealthCheckResult(c));

            _buffer.WriteFailedHealthChecksHeader();

            failed.ForEach(c => _buffer.WriteHealthCheckResult(c));
        }
コード例 #4
0
 public void ReportHealth(
     GlobalMetricTags globalTags,
     IEnumerable <HealthCheck.Result> healthyChecks,
     IEnumerable <HealthCheck.Result> degradedChecks,
     IEnumerable <HealthCheck.Result> unhealthyChecks)
 {
 }
コード例 #5
0
 public void ReportHealth(
     GlobalMetricTags globalTags,
     IEnumerable <HealthCheck.Result> healthyChecks,
     IEnumerable <HealthCheck.Result> degradedChecks,
     IEnumerable <HealthCheck.Result> unhealthyChecks)
 {
     // Health checks are reported as metrics as well
 }
コード例 #6
0
        public override void Setup()
        {
            var tags = new GlobalMetricTags
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            _registry = new DefaultMetricContextRegistry("context_label", tags);
        }
コード例 #7
0
        public MetricContextTestFixture()
        {
            ApdexOptions = new ApdexOptions
            {
                Name = "apdex"
            };

            CounterOptions = new CounterOptions
            {
                Name = "counter"
            };

            GaugeOptions = new GaugeOptions
            {
                Name = "gauge"
            };

            HistogramOptions = new HistogramOptions
            {
                Name = "histogram"
            };

            MeterOptions = new MeterOptions
            {
                Name = "meter"
            };

            TimerOptions = new TimerOptions
            {
                Name = "timer"
            };

            var tags = new GlobalMetricTags
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var contextualTags = new ContextualMetricTagProviders
            {
                { "key1", () => new Guid().ToString() },
                { "key2", () => new Guid().ToString() }
            };

            var samplingProvider = new DefaultSamplingReservoirProvider(() => new DefaultForwardDecayingReservoir());

            Registry         = new DefaultMetricContextRegistry("context_label", tags, contextualTags);
            ApdexBuilder     = new DefaultApdexBuilder(samplingProvider);
            HistogramBuilder = new DefaultHistogramBuilder(samplingProvider);
            CounterBuilder   = new DefaultCounterBuilder();
            GaugeBuilder     = new DefaultGaugeBuilder();
            MeterBuilder     = new DefaultMeterBuilder();
            TimerBuilder     = new DefaultTimerBuilder(samplingProvider);
            Clock            = new StopwatchClock();
        }
コード例 #8
0
        public void ReportHealth(GlobalMetricTags globalMetrics,
                                 IEnumerable <HealthCheck.Result> healthyChecks,
                                 IEnumerable <HealthCheck.Result> degradedChecks,
                                 IEnumerable <HealthCheck.Result> unhealthyChecks)
        {
            _logger.LogDebug($"Writing Health Checks for {Name}");

            _stringReporter.ReportHealth(globalMetrics, healthyChecks, degradedChecks, unhealthyChecks);

            _logger.LogDebug($"Writing Health Checks for {Name}");
        }
コード例 #9
0
        public void ReportHealth(
            GlobalMetricTags globalTags,
            IEnumerable <HealthCheck.Result> healthyChecks,
            IEnumerable <HealthCheck.Result> degradedChecks,
            IEnumerable <HealthCheck.Result> unhealthyChecks)
        {
            _logger.LogTrace($"Packing Health Checks for {Name}");

            var unhealthy = unhealthyChecks as HealthCheck.Result[] ?? unhealthyChecks.ToArray();
            var degraded  = degradedChecks as HealthCheck.Result[] ?? degradedChecks.ToArray();

            var isUnhealthy = unhealthy.Any();
            var isDegraded  = degraded.Any();
            var healthy     = !isUnhealthy && !isDegraded;

            var healthStatusValue = 2;

            if (isUnhealthy)
            {
                healthStatusValue = 3;
            }
            else if (healthy)
            {
                healthStatusValue = 1;
            }

            var tags = new MetricTags(globalTags.Select(t => t.Key).ToArray(), globalTags.Select(t => t.Value).ToArray());

            _payloadBuilder.Pack("health", healthStatusValue, tags);

            var checks = unhealthy.Concat(degraded).Concat(healthyChecks);

            foreach (var healthCheck in checks)
            {
                var allTags = MetricTags.Concat(tags, new MetricTags("health_check", healthCheck.Name));

                if (healthCheck.Check.Status == HealthCheckStatus.Unhealthy)
                {
                    _payloadBuilder.Pack("health_checks__unhealhty", healthCheck.Check.Message, allTags);
                }
                else if (healthCheck.Check.Status == HealthCheckStatus.Healthy)
                {
                    _payloadBuilder.Pack("health_checks__healthy", healthCheck.Check.Message, allTags);
                }
                else if (healthCheck.Check.Status == HealthCheckStatus.Degraded)
                {
                    _payloadBuilder.Pack("health_checks__degraded", healthCheck.Check.Message, allTags);
                }
            }

            _logger.LogTrace($"Packed Health Checks for {Name}");
        }
コード例 #10
0
        public void Setup()
        {
            var tags = new GlobalMetricTags
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var contextualTags = new ContextualMetricTagProviders
            {
                { "key3", () => new Guid().ToString() },
                { "key4", () => new Guid().ToString() }
            };

            _registry = new DefaultMetricContextRegistry("context_label", tags, contextualTags);
        }
コード例 #11
0
        public void ReportHealth(GlobalMetricTags globalTags,
                                 IEnumerable <HealthCheck.Result> healthyChecks,
                                 IEnumerable <HealthCheck.Result> degradedChecks,
                                 IEnumerable <HealthCheck.Result> unhealthyChecks)
        {
            _logger.LogDebug("Packing Health Checks for InfluxDB");

            var unhealthy = unhealthyChecks.Any();
            var degraded  = degradedChecks.Any();
            var healthy   = !unhealthy && !degraded;

            var healthStatusValue = 2;

            if (unhealthy)
            {
                healthStatusValue = 3;
            }
            else if (healthy)
            {
                healthStatusValue = 1;
            }

            Pack("health", healthStatusValue, new MetricTags(globalTags));

            var checks = unhealthyChecks.Concat(degradedChecks).Concat(healthyChecks);

            foreach (var healthCheck in checks)
            {
                var tags = new MetricTags(globalTags).With("health_check", healthCheck.Name);

                if (healthCheck.Check.Status == HealthCheckStatus.Unhealthy)
                {
                    Pack("health_checks__unhealhty", healthCheck.Check.Message, tags);
                }
                else if (healthCheck.Check.Status == HealthCheckStatus.Healthy)
                {
                    Pack("health_checks__healthy", healthCheck.Check.Message, tags);
                }
                else if (healthCheck.Check.Status == HealthCheckStatus.Degraded)
                {
                    Pack("health_checks__degraded", healthCheck.Check.Message, tags);
                }
            }

            _logger.LogDebug("Packed Health Checks for InfluxDB");
        }
コード例 #12
0
        public void ReportHealth(GlobalMetricTags globalTags,
                                 IEnumerable <HealthCheck.Result> healthyChecks,
                                 IEnumerable <HealthCheck.Result> degradedChecks,
                                 IEnumerable <HealthCheck.Result> unhealthyChecks)
        {
            WriteLine(typeof(HealthStatus).HumanzeStartMetricType());
            _logger.LogDebug("Writing Health Checks for Console");

            var passed      = healthyChecks.ToList();
            var failed      = unhealthyChecks.ToList();
            var degraded    = degradedChecks.ToList();
            var isHealthy   = !failed.Any() && !degraded.Any();
            var isUnhealthy = failed.Any();

            var status = "Degraded";

            if (isHealthy)
            {
                status = "Healthy";
            }

            if (isUnhealthy)
            {
                status = "Unhealthy";
            }


            WriteLine(string.Format(Environment.NewLine + $"\tHealth Status = {status}" + Environment.NewLine));

            WriteLine("\tPASSED CHECKS");

            passed.ForEach(c => WriteLine(c.Hummanize()));

            WriteLine("\tDEGRADED CHECKS");

            degraded.ForEach(c => WriteLine(c.Hummanize()));

            WriteLine("\tFAILED CHECKS");

            failed.ForEach(c => WriteLine(c.Hummanize()));

            _logger.LogDebug("Writing Health Checks for Console");
            WriteLine(typeof(HealthStatus).HumanzeEndMetricType());
        }
コード例 #13
0
        public DefaultMetricContextRegistry(string context, GlobalMetricTags globalTags)
        {
            _globalTags = globalTags ?? throw new ArgumentNullException(nameof(globalTags));

            if (context.IsMissing())
            {
                throw new ArgumentException("Registry Context cannot be null or empty", nameof(context));
            }

            Context = context;

            DataProvider = new DefaultMetricRegistryManager(
                () => _gauges.All,
                () => _counters.All,
                () => _meters.All,
                () => _histograms.All,
                () => _timers.All,
                () => _apdexScores.All);
        }
コード例 #14
0
        private static IMetricsRoot InitAppMetrics(InitAppMetricsModel initAppMetricsModel)
        {
            GlobalMetricTags globalMetricTags = new GlobalMetricTags();

            if (initAppMetricsModel.GlobalTags != null)
            {
                foreach (var item in initAppMetricsModel.GlobalTags)
                {
                    globalMetricTags.Add(item.Key, item.Value);
                }
            }

            var metrics = new MetricsBuilder()
                          .Configuration.Configure(options =>
            {
                options.DefaultContextLabel = initAppMetricsModel.DefaultContextLabel;
                options.AddAppTag(Assembly.GetExecutingAssembly().GetName().Name);
                options.AddServerTag(Environment.MachineName);
                options.AddEnvTag(initAppMetricsModel.EnvTag);
                options.GlobalTags = globalMetricTags;
            })
                          .Report.ToInfluxDb(options =>
            {
                options.InfluxDb.BaseUri                 = new Uri(initAppMetricsModel.BaseUri);
                options.InfluxDb.Database                = initAppMetricsModel.Database;
                options.InfluxDb.UserName                = initAppMetricsModel.UserName;
                options.InfluxDb.Password                = initAppMetricsModel.Password;
                options.HttpPolicy.BackoffPeriod         = TimeSpan.FromSeconds(30);
                options.HttpPolicy.FailuresBeforeBackoff = 5;
                options.HttpPolicy.Timeout               = TimeSpan.FromSeconds(3);
                options.FlushInterval = TimeSpan.FromSeconds(5);

                options.InfluxDb.CreateDataBaseIfNotExists = true;                //如果没有库,则创建
                options.MetricsOutputFormatter             = new MetricsInfluxDbLineProtocolOutputFormatter();
            })
                          .Build();

            return(metrics);
        }