Exemple #1
0
        /// <summary>
        /// Add Metrics Middleware to the Owin pipeline.
        /// Sample: Metric.Config.WithOwin( m => app.Use(m))
        /// Where app is the IAppBuilder
        /// </summary>
        /// <param name="config">Chainable configuration object.</param>
        /// <param name="middlewareRegistration">Action used to register middleware. This should generally be app.Use(middleware)</param>
        /// <param name="owinConfig">Action used to configure Owin metrics.</param>
        /// <returns>Chainable configuration object.</returns>
        public static MetricsConfig WithOwin(this MetricsConfig config, Action <object> middlewareRegistration, Action <OwinMetricsConfig> owinConfig)
        {
            var owin = config.WithConfigExtension((ctx, hs) => new OwinMetricsConfig(middlewareRegistration, ctx, hs));

            owinConfig(owin);
            return(config);
        }
        public static MetricsConfig WithHapper(this MetricsConfig metricsConfig, IPipelines pipelines, Action <HapperMetricsConfig> configHapper)
        {
            var happerConfig = metricsConfig
                               .WithConfigExtension(
                (ctx, hs) => new HapperMetricsConfig(ctx, hs, pipelines),
                () => HapperMetricsConfig.Disabled);

            configHapper(happerConfig);
            return(metricsConfig);
        }
        public static MetricsConfig WithWebServerCounters(this MetricsConfig config)
        {
            Require.NotNull(config, nameof(config));

            config.WithConfigExtension((context, hs) =>
            {
                context.Advanced.Gauge("websrv_conn_attempts_rate", () => new WebServicePerfCounterValueProvider(WebServicePerfCounters.CONNECTION_ATTEMPTS_PER_SECOND), Unit.Custom("connections/s"));
                context.Advanced.Gauge("websrv_current_conn_cnt", () => new WebServicePerfCounterValueProvider(WebServicePerfCounters.CURRENT_CONNECTIONS), Unit.Custom("connections"));
            });

            return(config);
        }
        public static MetricsConfig WithSystemCounters(this MetricsConfig config)
        {
            Require.NotNull(config, nameof(config));

            config.WithConfigExtension((context, hs) =>
            {
                context.Advanced.Gauge("cpu", () => new PerfCounterValueProvider("Processor", "% Processor Time", "_Total"), Unit.Percent);
                context.Advanced.Gauge("memory", () => new MemoryLoadValueProvider(), Unit.Percent);
            });

            return(config);
        }
        public static MetricsConfig WithAspNetCounters(this MetricsConfig config)
        {
            Require.NotNull(config, nameof(config));

            config.WithConfigExtension((context, hs) =>
            {
                context.Advanced.Gauge("aspnet_req_rate", () => new AspNetAppPerfCounterValueProvider(AspNetAppPerfCounters.REQUESTS_PER_SECOND), Unit.Custom("requests/s"));
                context.Advanced.Gauge("aspnet_req_exec_time", () => new AspNetPerfCounterValueProvider(AspNetPerfCounters.REQUEST_EXECUTION_TIME), Unit.Custom("ms"));
                context.Advanced.Gauge("aspnet_req_wait_time", () => new AspNetPerfCounterValueProvider(AspNetPerfCounters.REQUEST_WAIT_TIME), Unit.Custom("ms"));
                context.Advanced.Gauge("aspnet_current_req_cnt", () => new AspNetPerfCounterValueProvider(AspNetPerfCounters.REQUESTS_CURRENT), Unit.Requests);
                context.Advanced.Gauge("aspnet_disc_req_cnt", () => new AspNetPerfCounterValueProvider(AspNetPerfCounters.REQUESTS_DISCONNECTED), Unit.Requests);
                context.Advanced.Gauge("aspnet_queued_req_cnt", () => new AspNetPerfCounterValueProvider(AspNetPerfCounters.REQUESTS_QUEUED), Unit.Requests);
                context.Advanced.Gauge("aspnet_rejected_req_cnt", () => new AspNetPerfCounterValueProvider(AspNetPerfCounters.REQUESTS_REJECTED), Unit.Requests);
            });

            return(config);
        }
        public static IApplicationBuilder UseMetrics(this IApplicationBuilder app, MetricsConfig config, Clock clock)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            // Verify if AddMetrics was done before calling UseMetrics
            // We use the MetricsMarkerService to make sure if all the services were added.
            MetricsServicesHelper.ThrowIfMetricsNotRegistered(app.ApplicationServices);

            var options = app.ApplicationServices.GetService <IOptions <MetricsOptions> >().Value;

            config.WithConfigExtension((ctx, hs) =>
            {
                var metricsContext = new AspNetMetricsContext(ctx, hs, clock);

                // Metrics Endpoint Middleware
                app.Use(next => new MetricsEndpointMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new PingEndpointEndpointMiddleware(next, options).Invoke);
                app.Use(next => new HealthEndpointEndpointMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new MetricsEndpointTextEndpointMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new MetricsEndpointVisualizationEndpointMiddleware(next, options).Invoke);

                // Web Metrics Middleware
                app.Use(next => new ErrorRequestMeterMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new OAuth2ClientWebRequestMeterMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new PerRequestTimerMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new RequestTimerMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new ActiveRequestCounterEndpointMiddleware(next, options, metricsContext).Invoke);
                app.Use(next => new PostAndPutRequestSizeHistogramMiddleware(next, options, metricsContext).Invoke);
            });

            UseHealthChecks(app);

            return(app);
        }