Example #1
0
        /// <inheritdoc />
        public IMetricsRoot Build()
        {
            IMetricsRegistry registry = new NullMetricsRegistry();

            if (_options == null)
            {
                _options = new MetricsOptions();
            }

            if (_metricFields == null)
            {
                _metricFields = new MetricFields();
            }

            if (_options.Enabled)
            {
                registry = new DefaultMetricsRegistry(_options.DefaultContextLabel, _clock, ContextRegistry);
            }

            if (_metricsOutputFormatters.Count == 0)
            {
                _metricsOutputFormatters.Add(new MetricsTextOutputFormatter(_metricFields));
            }
            else
            {
                foreach (var metricsOutputFormatter in _metricsOutputFormatters)
                {
                    if (metricsOutputFormatter.MetricFields == null)
                    {
                        metricsOutputFormatter.MetricFields = _metricFields;
                    }
                }
            }

            if (_reporters != null && _reporters.Any())
            {
                foreach (var reporter in _reporters)
                {
                    if (reporter.Formatter != null && reporter.Formatter.MetricFields == null)
                    {
                        reporter.Formatter.MetricFields = _metricFields;
                    }
                }
            }

            if (_envFormatters.Count == 0)
            {
                _envFormatters.Add(new EnvInfoTextOutputFormatter());
            }

            var builderFactory = new DefaultMetricsBuilderFactory(_defaultSamplingReservoir);
            var measure        = new DefaultMeasureMetricsProvider(registry, builderFactory, _clock);
            var provider       = new DefaultMetricsProvider(registry, builderFactory, _clock);
            var snapshot       = new DefaultMetricValuesProvider(_metricsFilter, registry);
            var manage         = new DefaultMetricsManager(registry);
            var metrics        = new DefaultMetrics(_clock, _metricsFilter, measure, builderFactory, provider, snapshot, manage);
            var defaultMetricsOutputFormatter = _defaultMetricsOutputFormatter ?? _metricsOutputFormatters.FirstOrDefault();
            var defaultEnvOutputFormatter     = _defauEnvOutputFormatter ?? _envFormatters.FirstOrDefault();

            if (CanReport())
            {
                _metricsReportRunner = new DefaultMetricsReportRunner(metrics, _reporters);
            }

            return(new MetricsRoot(
                       metrics,
                       _options,
                       _metricsOutputFormatters,
                       _envFormatters,
                       defaultMetricsOutputFormatter,
                       defaultEnvOutputFormatter,
                       _environmentInfoProvider,
                       _reporters,
                       _metricsReportRunner));

            IMetricContextRegistry ContextRegistry(string context) =>
            new DefaultMetricContextRegistry(context, new GlobalMetricTags(_options.GlobalTags));
        }
Example #2
0
 private bool CanReport()
 {
     return(_options.Enabled && _options.ReportingEnabled && _reporters.Any());
 }