Ejemplo n.º 1
0
        internal async Task Generate(IMetricReporter reporter,
                                     IMetrics metrics,
                                     IMetricsFilter reporterMetricsFilter,
                                     CancellationToken token)
        {
            if (reporterMetricsFilter == default(IMetricsFilter))
            {
                reporterMetricsFilter = metrics.Advanced.GlobalFilter;
            }

            reporter.StartReport(metrics);

            var data = await metrics.Advanced.Data.ReadDataAsync(reporterMetricsFilter);

            if (data.Environment.Entries.Any() && reporterMetricsFilter.ReportEnvironment)
            {
                reporter.StartMetricTypeReport(typeof(EnvironmentInfo));

                reporter.ReportEnvironment(data.Environment);

                reporter.EndMetricTypeReport(typeof(EnvironmentInfo));
            }

            foreach (var contextValueSource in data.Contexts)
            {
                ReportMetricType(reporter, contextValueSource.Counters,
                                 c => { reporter.ReportMetric($"{contextValueSource.Context}", c); }, token);

                ReportMetricType(reporter, contextValueSource.Gauges,
                                 g => { reporter.ReportMetric($"{contextValueSource.Context}", g); }, token);

                ReportMetricType(reporter, contextValueSource.Histograms,
                                 h => { reporter.ReportMetric($"{contextValueSource.Context}", h); }, token);

                ReportMetricType(reporter, contextValueSource.Meters,
                                 m => { reporter.ReportMetric($"{contextValueSource.Context}", m); }, token);

                ReportMetricType(reporter, contextValueSource.Timers,
                                 t => { reporter.ReportMetric($"{contextValueSource.Context}", t); }, token);
            }

            if (reporterMetricsFilter.ReportHealthChecks)
            {
                var healthStatus = await metrics.Advanced.Health.ReadStatusAsync();

                reporter.StartMetricTypeReport(typeof(HealthStatus));

                var passed   = healthStatus.Results.Where(r => r.Check.Status.IsHealthy());
                var failed   = healthStatus.Results.Where(r => r.Check.Status.IsUnhealthy());
                var degraded = healthStatus.Results.Where(r => r.Check.Status.IsDegraded());

                reporter.ReportHealth(passed, degraded, failed);

                reporter.EndMetricTypeReport(typeof(HealthStatus));
            }

            reporter.EndReport(metrics);
        }