Beispiel #1
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="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,
            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
            {
                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);
        }
        /// <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="options">The GrafanaCloud Hosted Metrics 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 AsGrafanaCloudHostedMetricsGraphiteSyntax(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            MetricsHostedMetricsOptions options,
            TimeSpan flushInterval,
            MetricFields fields = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

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

            return(metricFormattingBuilder.Using(formatter, false));
        }
        /// <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="setupAction">The GrafanaCloud Hosted Metrics formatting options to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsHostedMetricsOptions> setupAction)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

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

            var options = new MetricsHostedMetricsOptions();

            setupAction.Invoke(options);

            var formatter = new MetricsHostedMetricsJsonOutputFormatter(options);

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