/// <summary>
        ///     Add the <see cref="DatadogReporter" /> allowing metrics to be reported to Datadog.
        /// </summary>
        /// <param name="metricReporterProviderBuilder">
        ///     The <see cref="IMetricsReportingBuilder" /> used to configure metrics reporters.
        /// </param>
        /// <param name="url">The base url where metrics are written.</param>
        /// <param name="apiKey">The api key used for authentication</param>
        /// <param name="fieldsSetup">The metric fields to report as well as their names.</param>
        /// <param name="datadogOptionsSetup">The setup action to configure the <see cref="MetricsDatadogOptions"/> to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder ToDatadogHttp(
            this IMetricsReportingBuilder metricReporterProviderBuilder,
            string url,
            string apiKey,
            Action <MetricFields> fieldsSetup = null,
            Action <MetricsDatadogOptions> datadogOptionsSetup = null)
        {
            if (metricReporterProviderBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricReporterProviderBuilder));
            }

            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                throw new InvalidOperationException($"{nameof(url)} must be a valid absolute URI");
            }

            var datadogOptions = new MetricsDatadogOptions();

            var options = new MetricsReportingDatadogOptions
            {
                Datadog =
                {
                    BaseUri = uri,
                    ApiKey  = apiKey
                }
            };

            datadogOptionsSetup?.Invoke(datadogOptions);

            IMetricsOutputFormatter formatter;
            MetricFields            fields = null;

            if (fieldsSetup == null)
            {
                formatter = new MetricsDatadogJsonOutputFormatter(options.FlushInterval, datadogOptions);
            }
            else
            {
                fields = new MetricFields();
                fieldsSetup.Invoke(fields);
                formatter = new MetricsDatadogJsonOutputFormatter(options.FlushInterval, datadogOptions, fields);
            }

            options.MetricsOutputFormatter = formatter;

            var httpClient = CreateClient(options, options.HttpPolicy);
            var reporter   = new DatadogReporter(options, httpClient);

            var builder = metricReporterProviderBuilder.Using(reporter);

            builder.OutputMetrics.AsDatadogJson(datadogOptions, options.FlushInterval, fields);

            return(builder);
        }
Esempio n. 2
0
        /// <summary>
        ///     Add the <see cref="GraphiteReporter" /> allowing metrics to be reported to Graphite.
        /// </summary>
        /// <param name="metricReporterProviderBuilder">
        ///     The <see cref="IMetricsReportingBuilder" /> used to configure metrics reporters.
        /// </param>
        /// <param name="url">The base url where metrics are written.</param>
        /// <param name="flushInterval">
        ///     The <see cref="T:System.TimeSpan" /> interval used if intended to schedule metrics
        ///     reporting.
        /// </param>
        /// <param name="fieldsSetup">The metric fields to report as well as thier names.</param>
        /// <param name="optionsSetup">The setup action to configure the <see cref="MetricsGraphitePlainTextProtocolOptions"/> to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder ToGraphite(
            this IMetricsReportingBuilder metricReporterProviderBuilder,
            string url,
            TimeSpan flushInterval,
            Action <MetricFields> fieldsSetup = null,
            Action <MetricsGraphitePlainTextProtocolOptions> optionsSetup = null)
        {
            if (metricReporterProviderBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricReporterProviderBuilder));
            }

            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                throw new InvalidOperationException($"{nameof(url)} must be a valid absolute URI");
            }

            var plainTextProtocolOptions = new MetricsGraphitePlainTextProtocolOptions();

            optionsSetup?.Invoke(plainTextProtocolOptions);

            IMetricsOutputFormatter formatter;
            var defaultFields = new MetricFields();

            defaultFields.DefaultGraphiteMetricFieldNames();

            if (fieldsSetup == null)
            {
                formatter = new MetricsGraphitePlainTextProtocolOutputFormatter(plainTextProtocolOptions, defaultFields);
            }
            else
            {
                fieldsSetup.Invoke(defaultFields);
                formatter = new MetricsGraphitePlainTextProtocolOutputFormatter(plainTextProtocolOptions, defaultFields);
            }

            var options = new MetricsReportingGraphiteOptions
            {
                FlushInterval = flushInterval,
                Graphite      =
                {
                    BaseUri = uri
                },
                MetricsOutputFormatter = formatter
            };

            var httpClient = CreateClient(options, options.ClientPolicy);
            var reporter   = new GraphiteReporter(options, httpClient);

            var builder = metricReporterProviderBuilder.Using(reporter);

            builder.OutputMetrics.AsGraphitePlainTextProtocol(plainTextProtocolOptions, defaultFields);

            return(builder);
        }
Esempio n. 3
0
        /// <summary>
        ///     Add the <see cref="MetricsGraphitePlainTextProtocolOutputFormatter" /> allowing metrics to optionally be reported to
        ///     Graphite using the Plain Text Protocol.
        /// </summary>
        /// <param name="metricFormattingBuilder">s
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure InfluxDB Lineprotocol formatting
        ///     options.
        /// </param>
        /// <param name="setupAction">The InfluxDB LineProtocol formatting options to use.</param>
        /// <param name="fields">The metric fields to report as well as thier names.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsGraphitePlainTextProtocol(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsGraphitePlainTextProtocolOptions> setupAction,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            var options = new MetricsGraphitePlainTextProtocolOptions();

            setupAction.Invoke(options);

            if (fields == null)
            {
                fields = new MetricFields();
                fields.DefaultGraphiteMetricFieldNames();
            }

            var formatter = new MetricsGraphitePlainTextProtocolOutputFormatter(options, fields);

            return(metricFormattingBuilder.Using(formatter, false));
        }
Esempio n. 4
0
        /// <summary>
        ///     Add the <see cref="InfluxDbMetricsReporter" /> allowing metrics to be reported to InfluxDB.
        /// </summary>
        /// <param name="metricReporterProviderBuilder">
        ///     The <see cref="IMetricsReportingBuilder" /> used to configure metrics reporters.
        /// </param>
        /// <param name="url">The base url where InfluxDB is hosted.</param>
        /// <param name="database">The InfluxDB where metrics should be flushed.</param>
        /// <param name="fieldsSetup">The metric fields to report as well as thier names.</param>
        /// <param name="lineProtocolOptionsSetup">The setup action to configure the <see cref="MetricsInfluxDbLineProtocolOptions"/> to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder ToInfluxDb(
            this IMetricsReportingBuilder metricReporterProviderBuilder,
            string url,
            string database,
            Action <MetricFields> fieldsSetup = null,
            Action <MetricsInfluxDbLineProtocolOptions> lineProtocolOptionsSetup = null)
        {
            if (metricReporterProviderBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricReporterProviderBuilder));
            }

            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                throw new InvalidOperationException($"{nameof(url)} must be a valid absolute URI");
            }

            var lineProtocolOptions = new MetricsInfluxDbLineProtocolOptions();

            lineProtocolOptionsSetup?.Invoke(lineProtocolOptions);

            IMetricsOutputFormatter formatter;
            MetricFields            fields = null;

            if (fieldsSetup == null)
            {
                formatter = new MetricsInfluxDbLineProtocolOutputFormatter(lineProtocolOptions);
            }
            else
            {
                fields = new MetricFields();
                fieldsSetup.Invoke(fields);
                formatter = new MetricsInfluxDbLineProtocolOutputFormatter(lineProtocolOptions, fields);
            }

            var options = new MetricsReportingInfluxDbOptions
            {
                InfluxDb =
                {
                    BaseUri  = uri,
                    Database = database
                },
                MetricsOutputFormatter = formatter
            };

            var httpClient = CreateClient(options.InfluxDb, options.HttpPolicy);
            var reporter   = new InfluxDbMetricsReporter(options, httpClient);

            var builder = metricReporterProviderBuilder.Using(reporter);

            builder.OutputMetrics.AsInfluxDbLineProtocol(lineProtocolOptions, fields);

            return(builder);
        }
Esempio n. 5
0
 public MetricFieldsBuilder(
     IMetricsBuilder metricsBuilder,
     MetricFields metricFields,
     Action <MetricFields> configureAction)
 {
     Builder          = metricsBuilder ?? throw new ArgumentNullException(nameof(metricsBuilder));
     _configureAction = configureAction ?? throw new ArgumentNullException(nameof(configureAction));
     _metricFields    = metricFields ?? new MetricFields();
 }
Esempio n. 6
0
        /// <inheritdoc />
        public IMetricsBuilder Using <TMetricsOutputFormatter>(bool replaceExisting, MetricFields fields = null)
            where TMetricsOutputFormatter : IMetricsOutputFormatter, new()
        {
            var formatter = new TMetricsOutputFormatter {
                MetricFields = fields
            };

            _metricsFormatter(replaceExisting, formatter);

            return(Builder);
        }
Esempio n. 7
0
        /// <inheritdoc />
        public IMetricsBuilder Using <TMetricsOutputFormatter>(MetricFields fields = null)
            where TMetricsOutputFormatter : IMetricsOutputFormatter, new()
        {
            var formatter = new TMetricsOutputFormatter {
                MetricFields = fields
            };

            _metricsFormatter(true, formatter);

            return(Builder);
        }
        /// <summary>
        ///     Add the <see cref="MetricsInfluxDbLineProtocolOutputFormatter" /> allowing metrics to optionally be reported to
        ///     InfluxDB using the LineProtocol.
        /// </summary>
        /// <param name="metricFormattingBuilder">s
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure InfluxDB Lineprotocol formatting
        ///     options.
        /// </param>
        /// <param name="fields">The metric fields to report as well as thier names.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsInfluxDbLineProtocol(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var formatter = new MetricsInfluxDbLineProtocolOutputFormatter(fields);

            return(metricFormattingBuilder.Using(formatter, false));
        }
Esempio n. 9
0
        /// <summary>
        ///     Add the <see cref="MetricsTextOutputFormatter" /> allowing metrics to optionally be reported as plain text.
        /// </summary>
        /// <param name="metricFormattingBuilder">
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure formatting
        ///     options.
        /// </param>
        /// <param name="metricFields">The metric fields to output as well as their names. This will override the globally configured metric fields.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsPlainText(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            MetricFields metricFields)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var formatter = new MetricsTextOutputFormatter(metricFields);

            return(metricFormattingBuilder.Using(formatter));
        }
        /// <summary>
        ///     Add the <see cref="MetricsHostedMetricsJsonOutputFormatter" /> allowing metrics to optionally be reported to
        ///     GrafanaCloud Hosted Metrics Graphite syntax.
        /// </summary>
        /// <param name="metricFormattingBuilder">s
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure GrafanaCloud Hosted Metrics formatting
        ///     options.
        /// </param>
        /// <param name="flushInterval">Interval is required my hosted metrics.</param>
        /// <param name="fields">The metric fields to report as well as their names.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            TimeSpan flushInterval,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var formatter = new MetricsHostedMetricsJsonOutputFormatter(flushInterval, fields);

            return(metricFormattingBuilder.Using(formatter, false));
        }
Esempio n. 11
0
        /// <summary>
        ///     Add the <see cref="MetricsDatadogJsonOutputFormatter" /> allowing metrics to optionally be reported to Datadog
        /// </summary>
        /// <param name="metricFormattingBuilder">s
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure Datadog formatting
        ///     options.
        /// </param>
        /// <param name="flushInterval">Interval is required my hosted metrics.</param>
        /// <param name="fields">The metric fields to report as well as their names.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsDatadogJson(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            TimeSpan flushInterval,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var formatter = new MetricsDatadogJsonOutputFormatter(flushInterval, fields);

            return(metricFormattingBuilder.Using(formatter, false));
        }
        /// <summary>
        ///     Add the <see cref="MetricsInfluxDbLineProtocolOutputFormatter" /> allowing metrics to optionally be reported to
        ///     InfluxDB using the LineProtocol.
        /// </summary>
        /// <param name="metricFormattingBuilder">s
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure InfluxDB Lineprotocol formatting
        ///     options.
        /// </param>
        /// <param name="setupAction">The InfluxDB LineProtocol formatting options to use.</param>
        /// <param name="fields">The metric fields to report as well as thier names.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsInfluxDbLineProtocol(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsInfluxDbLineProtocolOptions> setupAction,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var options = new MetricsInfluxDbLineProtocolOptions();

            setupAction?.Invoke(options);

            var formatter = new MetricsInfluxDbLineProtocolOutputFormatter(options, fields);

            return(metricFormattingBuilder.Using(formatter, false));
        }
Esempio n. 13
0
        /// <summary>
        ///     Add the <see cref="MetricsTextOutputFormatter" /> allowing metrics to optionally be reported as plain text.
        /// </summary>
        /// <param name="metricFormattingBuilder">
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure formatting
        ///     options.
        /// </param>
        /// <param name="setupAction">The plain text formatting options to use.</param>
        /// <param name="metricFields">The metric fields to output as well as their names. This will override the globally configured metric fields.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsPlainText(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsTextOptions> setupAction = null,
            MetricFields metricFields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var options = new MetricsTextOptions();

            setupAction?.Invoke(options);

            var formatter = new MetricsTextOutputFormatter(options)
            {
                MetricFields = metricFields
            };

            return(metricFormattingBuilder.Using(formatter));
        }
Esempio n. 14
0
        /// <summary>
        ///     Add the <see cref="MetricsDatadogJsonOutputFormatter" /> allowing metrics to optionally be reported to Datadog
        /// </summary>
        /// <param name="metricFormattingBuilder">s
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure Datadog formatting
        ///     options.
        /// </param>
        /// <param name="setupAction">The Datadog formatting options to use.</param>
        /// <param name="flushInterval">Interval is required my hosted metrics.</param>
        /// <param name="fields">The metric fields to report as well as their names.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsDatadogJson(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsDatadogOptions> setupAction,
            TimeSpan flushInterval,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            var options = new MetricsDatadogOptions();

            setupAction.Invoke(options);

            var formatter = new MetricsDatadogJsonOutputFormatter(flushInterval, options, fields);

            return(metricFormattingBuilder.Using(formatter, false));
        }
Esempio n. 15
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));
        }
Esempio n. 16
0
        /// <summary>
        ///     Add the <see cref="HostedMetricsReporter" /> allowing metrics to be reported to HostedMetrics.
        /// </summary>
        /// <param name="metricReporterProviderBuilder">
        ///     The <see cref="IMetricsReportingBuilder" /> used to configure metrics reporters.
        /// </param>
        /// <param name="url">The base url where metrics are written.</param>
        /// <param name="apiKey">The api key used for authentication</param>
        /// <param name="flushInterval">
        ///     The <see cref="T:System.TimeSpan" /> interval used if intended to schedule metrics
        ///     reporting.
        /// </param>
        /// <param name="fieldsSetup">The metric fields to report as well as their names.</param>
        /// <param name="hostedMetricsOptionsSetup">The setup action to configure the <see cref="MetricsHostedMetricsOptions"/> to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder ToHostedMetrics(
            this IMetricsReportingBuilder metricReporterProviderBuilder,
            string url,
            string apiKey,
            TimeSpan flushInterval,
            Action <MetricFields> fieldsSetup = null,
            Action <MetricsHostedMetricsOptions> hostedMetricsOptionsSetup = null)
        {
            if (metricReporterProviderBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricReporterProviderBuilder));
            }

            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                throw new InvalidOperationException($"{nameof(url)} must be a valid absolute URI");
            }

            var hostedMetricsOptions = new MetricsHostedMetricsOptions();

            var options = new MetricsReportingHostedMetricsOptions
            {
                FlushInterval = flushInterval,
                HostedMetrics =
                {
                    BaseUri = uri,
                    ApiKey  = apiKey
                }
            };

            hostedMetricsOptionsSetup?.Invoke(hostedMetricsOptions);

            IMetricsOutputFormatter formatter;
            MetricFields            fields = null;

            if (fieldsSetup == null)
            {
                formatter = new MetricsHostedMetricsJsonOutputFormatter(options.FlushInterval, hostedMetricsOptions);
            }
            else
            {
                fields = new MetricFields();
                fieldsSetup.Invoke(fields);
                formatter = new MetricsHostedMetricsJsonOutputFormatter(options.FlushInterval, hostedMetricsOptions, fields);
            }

            options.MetricsOutputFormatter = formatter;

            var httpClient = CreateClient(options, options.HttpPolicy);
            var reporter   = new HostedMetricsReporter(options, httpClient);

            var builder = metricReporterProviderBuilder.Using(reporter);

            builder.OutputMetrics.AsGrafanaCloudHostedMetricsGraphiteSyntax(hostedMetricsOptions, options.FlushInterval, fields);

            return(builder);
        }