Exemple #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);
        }
        /// <summary>
        ///     Adds App Metrics Health Checks 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="ArgumentNullException">
        ///     <see cref="T:Microsoft.AspNetCore.Builder.IApplicationBuilder" /> cannot be null
        /// </exception>
        public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            // Verify if AddHealthChecks was done before calling UseHealthChecks
            // We use the HealthCheckMarkerService to make sure if all the services were added.
            AppMetricsHealthServicesHelper.ThrowIfHealthChecksNotRegistered(app.ApplicationServices);

            var aspNetMetricsMiddlewareHealthChecksOptions = app.ApplicationServices.GetRequiredService<AppMetricsMiddlewareHealthChecksOptions>();

            if (aspNetMetricsMiddlewareHealthChecksOptions.HealthEndpointEnabled)
            {
                HealthServicesHelper.ThrowIfHealthChecksNotRegistered(app.ApplicationServices);

                app.UseMiddleware<HealthCheckEndpointMiddleware>();
            }

            return app;
        }