Esempio n. 1
0
        /// <summary>
        ///     Adds App Metrics Middleware to the <see cref="T:Microsoft.AspNetCore.Builder.IApplicationBuilder" /> request
        ///     execution pipeline.
        /// </summary>
        /// <param name="app">The <see cref="T:Microsoft.AspNetCore.Builder.IApplicationBuilder" />.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <exception cref="System.ArgumentNullException">
        ///     <see cref="T:Microsoft.AspNetCore.Builder.IApplicationBuilder" /> cannot be null
        /// </exception>
        public static IApplicationBuilder UseMetrics(this IApplicationBuilder app)
        {
            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 appMetricsOptions    = app.ApplicationServices.GetRequiredService <AppMetricsOptions>();
            var aspNetMetricsOptions = app.ApplicationServices.GetRequiredService <AspNetMetricsOptions>();

            if (aspNetMetricsOptions.PingEndpointEnabled)
            {
                app.UseMiddleware <PingEndpointMiddleware>();
            }

            if (aspNetMetricsOptions.HealthEndpointEnabled)
            {
                HealthServicesHelper.ThrowIfMetricsNotRegistered(app.ApplicationServices);

                app.UseMiddleware <HealthCheckEndpointMiddleware>();
            }

            if (aspNetMetricsOptions.MetricsTextEndpointEnabled && appMetricsOptions.MetricsEnabled)
            {
                app.UseMiddleware <MetricsEndpointTextEndpointMiddleware>();
            }

            if (aspNetMetricsOptions.MetricsEndpointEnabled && appMetricsOptions.MetricsEnabled)
            {
                app.UseMiddleware <MetricsEndpointMiddleware>();
            }

            if (aspNetMetricsOptions.EnvironmentInfoEndpointEnabled)
            {
                app.UseMiddleware <EnvironmentInfoMiddleware>();
            }

            if (appMetricsOptions.MetricsEnabled && aspNetMetricsOptions.DefaultTrackingEnabled)
            {
                app.UseMiddleware <ActiveRequestCounterEndpointMiddleware>();
                app.UseMiddleware <ErrorRequestMeterMiddleware>();
                app.UseMiddleware <PerRequestTimerMiddleware>();
                app.UseMiddleware <OAuthTrackingMiddleware>();
                app.UseMiddleware <PostAndPutRequestSizeHistogramMiddleware>();
                app.UseMiddleware <RequestTimerMiddleware>();
            }

            if (appMetricsOptions.MetricsEnabled && aspNetMetricsOptions.ApdexTrackingEnabled)
            {
                app.UseMiddleware <ApdexMiddleware>();
            }

            return(app);
        }
        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            // Verify if AddMetrics was done before calling UseMetricsWithMvc
            // We use the MetricsMarkerService to make sure if all the services were added.
            MetricsServicesHelper.ThrowIfMetricsNotRegistered(context.HttpContext.RequestServices);
            EnsureServices(context.HttpContext);

            var templateRoute = await _routeNameResolver.ResolveMatchingTemplateRoute(context.RouteData);

            if (!string.IsNullOrEmpty(templateRoute))
            {
                context.HttpContext.AddMetricsCurrentRouteName(templateRoute);
            }

            await next.Invoke();
        }
        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);
        }