Esempio n. 1
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var usageBody = SetUsageBody(context);

            var watch = ValueStopwatch.StartNew();

            try
            {
                await next(context);
            }
            finally
            {
                if (context.Response.StatusCode != StatusCodes.Status429TooManyRequests)
                {
                    var appId = context.Features.Get <IAppFeature>()?.App.Id;

                    if (appId != null)
                    {
                        var bytes = usageBody.BytesWritten;

                        if (context.Request.ContentLength != null)
                        {
                            bytes += context.Request.ContentLength.Value;
                        }

                        var(_, clientId) = context.User.GetClient();

                        var request = default(RequestLog);

                        request.Bytes         = bytes;
                        request.CacheStatus   = "MISS";
                        request.CacheHits     = 0;
                        request.Costs         = context.Features.Get <IApiCostsFeature>()?.Costs ?? 0;
                        request.ElapsedMs     = watch.Stop();
                        request.RequestMethod = context.Request.Method;
                        request.RequestPath   = context.Request.Path;
                        request.Timestamp     = clock.GetCurrentInstant();
                        request.StatusCode    = context.Response.StatusCode;
                        request.UserId        = context.User.OpenIdSubject();
                        request.UserClientId  = clientId;

#pragma warning disable MA0040 // Flow the cancellation token
                        await usageLog.LogAsync(appId.Value, request);

                        if (request.Costs > 0)
                        {
                            var date = request.Timestamp.ToDateTimeUtc().Date;

                            await usageTracker.TrackAsync(date, appId.Value.ToString(),
                                                          request.UserClientId,
                                                          request.Costs,
                                                          request.ElapsedMs,
                                                          request.Bytes);
                        }
#pragma warning restore MA0040 // Flow the cancellation token
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task Should_not_track_if_app_not_defined()
        {
            await sut.InvokeAsync(httpContext, next);

            Assert.True(isNextCalled);

            var date = instant.ToDateTimeUtc().Date;

            A.CallTo(() => usageTracker.TrackAsync(date, A <string> ._, A <string> ._, A <double> ._, A <long> ._, A <long> ._))
            .MustNotHaveHappened();
        }
Esempio n. 3
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var usageBody = SetUsageBody(context);

            var watch = ValueStopwatch.StartNew();

            try
            {
                await next(context);
            }
            finally
            {
                if (context.Response.StatusCode != StatusCodes.Status429TooManyRequests)
                {
                    var appId = context.Features.Get <IAppFeature>()?.AppId;

                    var costs = context.Features.Get <IApiCostsFeature>()?.Costs ?? 0;

                    if (appId != null)
                    {
                        var elapsedMs = watch.Stop();

                        var now = clock.GetCurrentInstant();

                        var userId     = context.User.OpenIdSubject();
                        var userClient = context.User.OpenIdClientId();

                        await log.LogAsync(appId.Id, now,
                                           context.Request.Method,
                                           context.Request.Path,
                                           userId,
                                           userClient,
                                           elapsedMs,
                                           costs);

                        if (costs > 0)
                        {
                            var bytes = usageBody.BytesWritten;

                            if (context.Request.ContentLength != null)
                            {
                                bytes += context.Request.ContentLength.Value;
                            }

                            var date = now.ToDateTimeUtc().Date;

                            await usageTracker.TrackAsync(date, appId.Id.ToString(), userClient, costs, elapsedMs, bytes);
                        }
                    }
                }
            }
        }