/// <summary>
        /// Обработчик сообщения
        /// </summary>
        /// <param name="context"></param>
        /// <param name="reporter"></param>
        /// <returns></returns>
        public Task Invoke(HttpContext context, MetricReporter reporter)
        {
            var path = context.Request.Path.Value;

            if (path == "/metrics" ||
                path == "/health")
            {
                // Call the next delegate/middleware in the pipeline
                return(_next(context));
            }
            // Start the Timer using Stopwatch
            var watch = new Stopwatch();

            watch.Start();

            context.Response.OnCompleted(() => {
                // Stop the timer and get elapsed time
                watch.Stop();
                long responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
                reporter.RegisterRequest();
                reporter.RegisterResponseTime(context.Response.StatusCode, context.Request.Method + ": " + path, watch.Elapsed);
                // Add the Response time information in the Item variable
                return(Task.CompletedTask);
            });
            // Call the next delegate/middleware in the pipeline
            return(_next(context));
        }
예제 #2
0
 public async Task Invoke(HttpContext httpContext, MetricReporter reporter)
 {
     Metrics.CreateCounter("PathCounter", "Count request",
                           new CounterConfiguration {
         LabelNames = new [] { "method", "endpoint" }
     }).WithLabels(httpContext.Request.Method, httpContext.Request.Path).Inc();
     await _next(httpContext);
 }
예제 #3
0
        /// <summary>
        /// Starts tests on the selected provider
        /// </summary>
        private IList <Metric> RunProviderTests(Type type, TestOptions options)
        {
            var reporter = new MetricReporter();
            var provider = (DbProviderTest)Activator.CreateInstance(type);

            provider.Initialize(options, reporter);
            provider.RunTests();

            return(reporter.Metrics);
        }
예제 #4
0
        /// <summary>
        /// Called when the Service Fabric is opening the service (part of the standard life cycle)
        /// </summary>
        /// <param name="cancellationToken">A token to monitor for cancellation</param>
        /// <returns>A task that completes when the service is open</returns>
        protected override async Task OnOpenAsync(CancellationToken cancellationToken)
        {
            Guid traceId = Guid.NewGuid();

            this.logger.OpenAsyncInvoked(traceId, ComponentName, this.GetType().Name);

            this.metricReporter?.Dispose();
            this.metricReporter = new MetricReporter(traceId, this.logger, ComponentName, this.Context, this.Partition, this.MetricsCallback, TimeSpan.FromSeconds(15));

            await this.metricReporter.StartAsync(traceId, cancellationToken).ConfigureAwait(false);

            await base.OnOpenAsync(cancellationToken).ConfigureAwait(false);
        }
예제 #5
0
        /// <summary>
        /// Called when the Service Fabric is closing the service down (part of the standard life cycle)
        /// </summary>
        /// <param name="cancellationToken">A token to monitor for cancellation</param>
        /// <returns>A task that completes when the service is closed</returns>
        protected override async Task OnCloseAsync(CancellationToken cancellationToken)
        {
            Guid traceId = Guid.NewGuid();

            this.logger.CloseAsyncInvoked(traceId, ComponentName, this.GetType().Name);

            if (this.metricReporter != null)
            {
                await this.metricReporter.StopAsync(traceId, cancellationToken).ConfigureAwait(false);

                this.metricReporter.Dispose();
                this.metricReporter = null;
            }

            await base.OnCloseAsync(cancellationToken).ConfigureAwait(false);
        }
예제 #6
0
 public OrderService(OrderContext orderContext
                     , UserContext userContext
                     , BucketRepository bucketRepository
                     , PriceServiceClient pricingClient
                     , WarehouseServiceClient warehouseServiceClient
                     , RabbitMQMessageSender mqSender
                     , MetricReporter metricReporter
                     , IDistributedCache distributedCache)
 {
     _orderContext           = orderContext;
     _userContext            = userContext;
     _pricingClient          = pricingClient;
     _warehouseServiceClient = warehouseServiceClient;
     _bucketRepository       = bucketRepository;
     _mqSender         = mqSender;
     _metricReporter   = metricReporter;
     _distributedCache = distributedCache;
 }
        public async Task Invoke(HttpContext context, MetricReporter reporter)
        {
            if (context.Request.Path.Value == "/metrics-text")
            {
                await _request.Invoke(context);

                return;
            }
            var sw = Stopwatch.StartNew();

            try
            {
                await _request.Invoke(context);
            }
            finally
            {
                sw.Stop();
                reporter.RegisterRequest();
                reporter.RegisterResponseTime(context.Response.StatusCode, context.Request.Method, sw.Elapsed);
            }
        }
예제 #8
0
        /// <summary>
        /// Called when the Service Fabric is aborting the current service (part of the standard life cycle)
        /// </summary>
        protected override void OnAbort()
        {
            Guid traceId = Guid.NewGuid();

            this.logger.AbortInvoked(traceId, ComponentName, this.GetType().Name);

            using (var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)))
            {
                try
                {
                    this.metricReporter.StopAsync(traceId, tokenSource.Token).GetAwaiter().GetResult();
                }
                catch (Exception e)
                {
                    this.logger.Error(traceId, ComponentName, "Error stopping the metrics reporter or timed out", null, e);
                }

                this.metricReporter.Dispose();
                this.metricReporter = null;
            }

            base.OnAbort();
        }
 public ResponseMetricMiddleware(RequestDelegate request, MetricReporter reporter)
 {
     _request  = request ?? throw new ArgumentNullException(nameof(request));
     _reporter = reporter ?? throw new ArgumentNullException(nameof(reporter));
 }
예제 #10
0
 /// <summary>
 /// Provider initialization
 /// </summary>
 public override void Initialize(TestOptions options, MetricReporter reporter)
 {
     base.Initialize(options, reporter);
 }
예제 #11
0
 public CountRequestMiddleware(RequestDelegate next, MetricReporter reporter)
 {
     _next     = next ?? throw new ArgumentNullException(nameof(next));
     _reporter = reporter ?? throw new ArgumentNullException(nameof(reporter));
 }
예제 #12
0
 /// <summary>
 /// Provider initialization
 /// </summary>
 public override void Initialize(TestOptions options, MetricReporter reporter)
 {
     base.Initialize(options, reporter);
 }