Ejemplo n.º 1
0
        /// <inheritdoc />
        public IMetricsBuilder Using <TReportMetrics>(IMetricsOutputFormatter formatter)
            where TReportMetrics : IReportMetrics, new()
        {
            var reporter = new TReportMetrics {
                Formatter = formatter
            };

            return(Using(reporter));
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public IMetricsBuilder Using <TReportMetrics>(IMetricsOutputFormatter formatter, TimeSpan flushInterval)
            where TReportMetrics : IReportMetrics, new()
        {
            var reporter = new TReportMetrics {
                Formatter = formatter, FlushInterval = flushInterval
            };

            return(Using(reporter));
        }
Ejemplo n.º 3
0
        private static async Task PrintMetrics(MetricsDataValueSource snapshot, IMetricsOutputFormatter formatter)
        {
            using (var stream = new MemoryStream())
            {
                await formatter.WriteAsync(stream, snapshot);

                var result = Encoding.UTF8.GetString(stream.ToArray());

                Console.WriteLine(result);
            }
        }
Ejemplo n.º 4
0
 public MetricsPrometheusHandler(IMetricsRoot metricsRoot)
 {
     _metricsRoot = metricsRoot;
     _formatter   = _metricsRoot.OutputMetricsFormatters
                    .OfType <MetricsPrometheusTextOutputFormatter>()
                    .SingleOrDefault();
     if (_formatter == null)
     {
         throw new ArgumentException("Include App.Metrics.Formatters.Prometheus!", nameof(metricsRoot));
     }
 }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public IMetricsBuilder Using(IMetricsOutputFormatter formatter, bool replaceExisting)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            _metricsFormatter(replaceExisting, formatter);

            return(Builder);
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        public IMetricsBuilder Using(IMetricsOutputFormatter formatter)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            _metricsFormatter(true, formatter);

            return(Builder);
        }
Ejemplo n.º 7
0
        public async Task <byte[]> GetSnapshot(CancellationToken cancellationToken)
        {
            using (var ms = new MemoryStream())
            {
                MetricsDataValueSource  metricsData = this.metricsRoot.Snapshot.Get();
                IMetricsOutputFormatter formatter   = this.metricsRoot.DefaultOutputMetricsFormatter;
                await formatter.WriteAsync(ms, metricsData, cancellationToken);

                return(ms.ToArray());
            }
        }
        public DefaultMetricsResponseWriter(
            IMetricsOutputFormatter fallbackFormatter,
            IReadOnlyCollection <IMetricsOutputFormatter> formatters)
        {
            if (formatters == null)
            {
                throw new ArgumentNullException(nameof(formatters));
            }

            _formatters        = new MetricsFormatterCollection(formatters.ToList());
            _fallbackFormatter = fallbackFormatter;
        }
Ejemplo n.º 9
0
        private static DefaultMetricsResponseWriter GetMetricsResponseWriter(
            IServiceProvider serviceProvider,
            IMetricsOutputFormatter formatter = null)
        {
            var formatters = serviceProvider.GetRequiredService <IReadOnlyCollection <IMetricsOutputFormatter> >();

            if (formatter != null)
            {
                return(new DefaultMetricsResponseWriter(formatter, formatters));
            }

            var options = serviceProvider.GetRequiredService <IOptions <MetricEndpointsOptions> >();

            return(new DefaultMetricsResponseWriter(options.Value.MetricsEndpointOutputFormatter, formatters));
        }
Ejemplo n.º 10
0
        private static void UseMetricsTextMiddleware(
            IApplicationBuilder app,
            IOptions <MetricsEndpointsHostingOptions> endpointsHostingOptionsAccessor,
            IOptions <MetricEndpointsOptions> endpointsOptionsAccessor,
            MetricsOptions metricsOptions,
            IMetricsOutputFormatter formatter = null)
        {
            formatter = formatter ?? endpointsOptionsAccessor.Value.MetricsTextEndpointOutputFormatter;

            app.UseWhen(
                context => ShouldUseMetricsTextEndpoint(endpointsHostingOptionsAccessor, endpointsOptionsAccessor, metricsOptions, context),
                appBuilder =>
            {
                var responseWriter = GetMetricsTextResponseWriter(appBuilder.ApplicationServices, formatter);
                appBuilder.UseMiddleware <MetricsEndpointMiddleware>(responseWriter);
            });
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Add the <see cref="SocketMetricsReporter" /> allowing metrics to be reported over Unix Domain Sockets.
        /// </summary>
        /// <param name="reportingBuilder">
        ///     The <see cref="IMetricsReportingBuilder" /> used to configure metrics reporters.
        /// </param>
        /// <param name="metricsOutputFormatter">
        ///     The <see cref="IMetricsOutputFormatter" /> used to configure metrics output formatter.
        /// </param>
        /// <param name="address">The Unix Domain Socket endpoint address where metrics are POSTed.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder OverUds(
            this IMetricsReportingBuilder reportingBuilder,
            IMetricsOutputFormatter metricsOutputFormatter,
            string address)
        {
            if (reportingBuilder == null)
            {
                throw new ArgumentNullException(nameof(reportingBuilder));
            }

            var options = new MetricsReportingSocketOptions
            {
                SocketSettings         = new SocketSettings(ProtocolType.IP, address, 0),
                MetricsOutputFormatter = metricsOutputFormatter
            };
            var provider = new SocketMetricsReporter(options);

            return(reportingBuilder.Using(provider));
        }
Ejemplo n.º 12
0
 public MetricsRoot(
     IMetrics metrics,
     MetricsOptions options,
     MetricsFormatterCollection metricsOutputFormatters,
     EnvFormatterCollection envOutputFormatters,
     IMetricsOutputFormatter defaultMetricsOutputFormatter,
     IEnvOutputFormatter defaultEnvOutputFormatter,
     EnvironmentInfoProvider environmentInfoProvider,
     MetricsReporterCollection reporterCollection,
     IRunMetricsReports reporter)
 {
     Options                       = options ?? throw new ArgumentNullException(nameof(options));
     _metrics                      = metrics ?? throw new ArgumentNullException(nameof(metrics));
     ReportRunner                  = reporter ?? throw new ArgumentNullException(nameof(reporter));
     _environmentInfoProvider      = new EnvironmentInfoProvider();
     Reporters                     = reporterCollection ?? new MetricsReporterCollection();
     OutputMetricsFormatters       = metricsOutputFormatters ?? new MetricsFormatterCollection();
     OutputEnvFormatters           = envOutputFormatters ?? new EnvFormatterCollection();
     DefaultOutputMetricsFormatter = defaultMetricsOutputFormatter;
     DefaultOutputEnvFormatter     = defaultEnvOutputFormatter;
     _environmentInfoProvider      = environmentInfoProvider;
 }
Ejemplo n.º 13
0
        /// <summary>
        ///     Add the <see cref="SocketMetricsReporter" /> allowing metrics to be reported over TCP.
        /// </summary>
        /// <param name="reportingBuilder">
        ///     The <see cref="IMetricsReportingBuilder" /> used to configure metrics reporters.
        /// </param>
        /// <param name="metricsOutputFormatter">
        ///     The <see cref="IMetricsOutputFormatter" /> used to configure metrics reporters.
        /// </param>
        /// <param name="address">The TCP endpoint address where metrics are POSTed.</param>
        /// <param name="port">The TCP endpoint port where metrics are POSTed.</param>
        /// <param name="flushInterval">
        ///     The <see cref="T:System.TimeSpan" /> interval used if intended to schedule metrics
        ///     reporting.
        /// </param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder OverTcp(
            this IMetricsReportingBuilder reportingBuilder,
            IMetricsOutputFormatter metricsOutputFormatter,
            string address,
            int port,
            TimeSpan flushInterval)
        {
            if (reportingBuilder == null)
            {
                throw new ArgumentNullException(nameof(reportingBuilder));
            }

            var options = new MetricsReportingSocketOptions
            {
                SocketSettings         = new SocketSettings(ProtocolType.Tcp, address, port),
                MetricsOutputFormatter = metricsOutputFormatter,
                FlushInterval          = flushInterval
            };

            var provider = new SocketMetricsReporter(options);

            return(reportingBuilder.Using(provider));
        }
 // ReSharper disable UnusedMember.Global
 public DefaultMetricsResponseWriter(IMetricsOutputFormatter formatter)
 // ReSharper restore UnusedMember.Global
 {
     _formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
 }
Ejemplo n.º 15
0
 public MetricsDataFormattingTests(ITestOutputHelper output, MetricProviderTestFixture fixture)
 {
     _output    = output;
     _formatter = new MetricsJsonOutputFormatter();
     _metrics   = fixture.DataWithOneContext;
 }
 public BucketTimerFormattingTests(ITestOutputHelper output, MetricProviderTestFixture fixture)
 {
     _output    = output;
     _formatter = new MetricsJsonOutputFormatter();
     _metrics   = fixture.BucketTimerContext;
 }
Ejemplo n.º 17
0
 public ApdexFormattingTests(ITestOutputHelper output, MetricProviderTestFixture fixture)
 {
     _output    = output;
     _formatter = new MetricsJsonOutputFormatter();
     _metrics   = fixture.ApdexContext;
 }
Ejemplo n.º 18
0
        /// <summary>
        ///     Adds App Metrics metrics text middleware to the <see cref="IApplicationBuilder" /> request execution pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder" />.</param>
        /// <param name="formatter">
        ///     Overrides the default use of <see cref="MetricsTextOutputFormatter" /> with the
        ///     <see cref="IMetricsOutputFormatter" /> specified.
        /// </param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseMetricsTextEndpoint(this IApplicationBuilder app, IMetricsOutputFormatter formatter)
        {
            EnsureMetricsAdded(app);

            var metricsOptions = app.ApplicationServices.GetRequiredService <MetricsOptions>();
            var endpointHostingOptionsAccessor = app.ApplicationServices.GetRequiredService <IOptions <MetricsEndpointsHostingOptions> >();
            var endpointsOptionsAccessor       = app.ApplicationServices.GetRequiredService <IOptions <MetricEndpointsOptions> >();

            UseMetricsTextMiddleware(app, endpointHostingOptionsAccessor, endpointsOptionsAccessor, metricsOptions, formatter);

            return(app);
        }