public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            var metricsAttributes = input.MethodBase.GetCustomAttributes(true).OfType <IMetricsAttribute>();
            var disposables       = new List <IDisposable>();

            try
            {
                foreach (var metricsAttribute in metricsAttributes)
                {
                    FillMacros(input, metricsAttribute);

                    switch (metricsAttribute)
                    {
                    case MetricsTimerAttribute timerAttribute:
                        var metricTimer = _metricsProvider.Timer(timerAttribute.Context, timerAttribute.Name);
                        disposables.Add(metricTimer);
                        break;

                    case MetricsCounterAttribute counterAttribute:
                        var counterValue = long.Parse(GetMethodParamValue(input, counterAttribute.ParamName) ?? "1");
                        _metricsProvider.Counter(counterAttribute.Context, counterAttribute.Name, counterValue);
                        break;

                    case MetricsMeterAttribute meterAttribute:
                        var meterValue = long.Parse(GetMethodParamValue(input, meterAttribute.ParamName) ?? "1");
                        _metricsProvider.Meter(meterAttribute.Context, meterAttribute.Name, meterValue);
                        break;

                    case MetricsHistogramAttribute histogramAttribute:
                        var histogramValue = long.Parse(GetMethodParamValue(input, histogramAttribute.ParamName) ?? "1");
                        _metricsProvider.Meter(histogramAttribute.Context, histogramAttribute.Name, histogramValue);
                        break;
                    }
                }

                return(getNext()(input, getNext));
            }
            finally
            {
                foreach (var disposable in disposables)
                {
                    disposable?.Dispose();
                }
            }
        }
Esempio n. 2
0
 public async ValueTask Timer(string name, TimeSpan Time, CancellationToken cancellationToken = default)
 {
     var totalMilliseconds = Convert.ToInt32(Time.TotalMilliseconds);
     await _metricsProvider.Timer(name, totalMilliseconds, cancellationToken).ConfigureAwait(false);
 }