/// <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);
        }
Beispiel #2
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="options">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,
            MetricsDatadogOptions options,
            TimeSpan flushInterval,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

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

            return(metricFormattingBuilder.Using(formatter, false));
        }