public async Task Invoke(HttpContext context)
 {
     using (_inProgressGauge.TrackInProgress())
     {
         await _next(context);
     }
 }
Ejemplo n.º 2
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                // In ASP.NET Core 3 with endpoint routing, the routing step happens separately and we can observe
                // the route data before the controller action is executed, thereby allowing us to use this data in metrics.
                var routeDataBeforeInnerHandler = TryCaptureRouteData(context);

                IGauge?inProgressGauge = null;

                if (routeDataBeforeInnerHandler != null)
                {
                    // We got some route data! This means we can create the "in progress" metric with accurate data.
                    // This assumes the relevant feature exists (true only if the metric has the label names defined).
                    var inProgressFeature = context.Features.Get <IHttpInProgressMetricWithRouteLabelsFeature>();
                    inProgressGauge = inProgressFeature?.CreateGauge(routeDataBeforeInnerHandler.Values);
                }

                using (inProgressGauge?.TrackInProgress())
                {
                    await _next(context);
                }
            }
            finally
            {
                // In ASP.NET Core 2, we have to capture it after executing the inner handler because
                // the routing happens as part of the MVC middleware, which is a single black box for us.
                if (context.Features.Get <ICapturedRouteDataFeature>() == null)
                {
                    TryCaptureRouteData(context);
                }
            }
        }
        public async Task InvokeAsync(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var patternNotMatching = string.IsNullOrWhiteSpace(_pattern) ||
                                     !Regex.IsMatch(context.Request.Path, _pattern, RegexOptions.IgnoreCase);

            using (patternNotMatching ? _inProgressGauge.TrackInProgress() : null)
            {
                await _next(context);
            }
        }