Ejemplo n.º 1
0
        public static async Task <T?> Measure <T>(
            Func <Task <T> > action,
            IHistogramMetric metric,
            Func <T, int> getCount,
            ICountMetric?errorCount = null,
            string[]?labels         = null
            ) where T : class
        {
            var stopwatch = Stopwatch.StartNew();

            T?result = null;

            try {
                result = await action().ConfigureAwait(false);
            }
            catch (Exception) {
                errorCount?.Inc(labels);

                throw;
            }
            finally {
                stopwatch.Stop();
                var count = result != null?getCount(result) : 0;

                metric.Observe(stopwatch, labels, count);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="action">Actual operation to measure</param>
        /// <param name="metric">Histogram metrics, which will observe the measurement</param>
        /// <param name="getCount">A function, which is able to get the custom count from the function return</param>
        /// <param name="errorCount">Optional: counter metric to count errors</param>
        /// <param name="labels">Labels for the histogram and errors counter metrics</param>
        /// <typeparam name="T">The observed function result</typeparam>
        /// <returns></returns>
        public static async Task <T?> Measure <T>(
            Func <Task <T> > action,
            IHistogramMetric metric,
            Func <T, int> getCount,
            ICountMetric?errorCount = null,
            string[]?labels         = null
            ) where T : class
        {
            var stopwatch = Stopwatch.StartNew();

            T?result = null;

            try {
                var task = action();
                result = task.IsCompleted ? task.Result : await action();
            }
            catch (Exception) {
                errorCount?.Inc(labels: labels.ValueOrEmpty());

                throw;
            }
            finally {
                stopwatch.Stop();
                metric.Observe(stopwatch, labels, result != null ? getCount(result) : 0);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Helpful function to measure the execution time of a given asynchronous function,
        /// and returns the observed function result.
        /// </summary>
        /// <param name="action">Actual operation to measure</param>
        /// <param name="metric">Histogram metrics, which will observe the measurement</param>
        /// <param name="errorCount">Optional: counter metric to count errors</param>
        /// <param name="labels">Labels for the histogram and errors counter metrics</param>
        /// <param name="count">Optional: count, one by default</param>
        /// <returns></returns>
        public static async Task <T> Measure <T>(
            Func <ValueTask <T> > action,
            IHistogramMetric metric,
            ICountMetric?errorCount = null,
            string[]?labels         = null,
            int count = 1
            )
        {
            var stopwatch = Stopwatch.StartNew();

            T result;

            try {
                var task = action();
                result = task.IsCompleted ? task.Result : await action();
            }
            catch (Exception) {
                errorCount?.Inc(labels: labels.ValueOrEmpty());

                throw;
            }
            finally {
                stopwatch.Stop();
                metric.Observe(stopwatch, labels, count);
            }

            return(result);
        }
        public static async Task Observe(this IHistogramMetric histogram, Func <Task> factory, params string[] labels)
        {
            var   sw = Stopwatch.StartNew();
            var   t  = factory();
            await t;

            sw.Stop();
            histogram.Observe(sw, labels);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Helpful function to measure the execution time of a given asynchronous function
        /// </summary>
        /// <param name="action">Actual operation to measure</param>
        /// <param name="metric">Histogram metrics, which will observe the measurement</param>
        /// <param name="errorCount">Optional: counter metric to count errors</param>
        /// <param name="labels">Labels for the histogram and errors counter metrics</param>
        /// <param name="count">Optional: count, one by default</param>
        /// <returns></returns>
        public static async Task Measure(
            Func <Task> action,
            IHistogramMetric metric,
            ICountMetric?errorCount = null,
            string[]?labels         = null,
            int count = 1
            )
        {
            var stopwatch = Stopwatch.StartNew();

            try {
                await action();
            }
            catch (Exception) {
                errorCount?.Inc(labels: labels.ValueOrEmpty());

                throw;
            }
            finally {
                stopwatch.Stop();
                metric.Observe(stopwatch, labels, count);
            }
        }