コード例 #1
0
        public async Task <TResult> ReceiveAsync <TResult>(IQuery <TResult> query, CancellationToken cancellationToken = default)
        {
            var stopWatch = new Stopwatch();

            _metrics.Increment(new RequestCounterMetric(query));
            _metrics.Increment(new RequestGaugeMetric("generic_request_gauge", "Current request count"));
            try {
                stopWatch.Start();
                var result = await _next.ReceiveAsync(query, cancellationToken);

                stopWatch.Stop();

                _metrics.Observe(new RequestDurationMetric(query), stopWatch.ElapsedMilliseconds);

                return(result);
            }
            catch (Exception exception) {
                stopWatch.Stop();
                _metrics.Increment(new RequestFailureMetric(query, exception));

                throw;
            }
            finally {
                _metrics.Decrement(new RequestGaugeMetric("generic_request_gauge", "Current request count"));
            }
        }
        public async Task ReceiveAsync <TCommand>(TCommand command, CancellationToken cancellationToken = default) where TCommand : ICommand
        {
            var stopWatch = new Stopwatch();

            _metrics.Increment(new RequestCounterMetric(command));
            _metrics.Increment(new RequestGaugeMetric("generic_request_gauge", "Current request count"));

            try {
                stopWatch.Start();
                await _next.ReceiveAsync(command, cancellationToken);

                stopWatch.Stop();

                _metrics.Observe(new RequestDurationMetric(command), stopWatch.ElapsedMilliseconds);
            }
            catch (Exception exception) {
                stopWatch.Stop();
                _metrics.Increment(new RequestFailureMetric(command, exception));

                throw;
            }
            finally {
                _metrics.Decrement(new RequestGaugeMetric("generic_request_gauge", "Current request count"));
            }
        }