<commit_before>﻿
using System;
using Metrics;
using Nancy.Bootstrapper;
namespace Nancy.Metrics
{
    public class NancyGlobalMetrics : IHideObjectMembers
    {
        private const string TimerItemsKey = "__Mertics.RequestTimer__";

        private readonly MetricsRegistry registry;

        public NancyGlobalMetrics(MetricsRegistry repository)
        {
            this.registry = repository;
            this.MetricsPrefix = "NancyFx";
        }

        public string MetricsPrefix { get; set; }

        /// <summary>
        /// Registers a Meter metric named "NancyFx.Errors" that records the rate at witch unhanded errors occurred while 
        /// processing Nancy requests.
        /// Registers a Timer metric named "NancyFx.Requests" that records how many requests per second are handled and also
        /// keeps a histogram of the request duration.
        /// </summary>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        public void RegisterAllMetrics(IPipelines nancyPipelines)
        {
            RegisterRequestTimer(nancyPipelines);
            RegisterErrorsMeter(nancyPipelines);
            RegisterActiveRequestCounter(nancyPipelines);
        }

        /// <summary>
        /// Registers a Timer metric named "NancyFx.Requests" that records how many requests per second are handled and also
        /// keeps a histogram of the request duration.
        /// </summary>
        /// <param name="metricName">Name of the metric.</param>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        public void RegisterRequestTimer(IPipelines nancyPipelines, string metricName = "Requests")
        {
            var requestTimer = Metric.Timer(Name(metricName), Unit.Requests, SamplingType.FavourRecent);

            nancyPipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
            {
                ctx.Items[TimerItemsKey] = requestTimer.NewContext();
                return null;
            });

            nancyPipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
            {
                using (ctx.Items[TimerItemsKey] as IDisposable) { }
                ctx.Items.Remove(TimerItemsKey);
            });
        }

        /// <summary>
        /// Registers a Meter metric named "NancyFx.Errors" that records the rate at witch unhanded errors occurred while 
        /// processing Nancy requests.
        /// </summary>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        /// <param name="metricName">Name of the metric.</param>
        public void RegisterErrorsMeter(IPipelines nancyPipelines, string metricName = "Errors")
        {
            var errorMeter = this.registry.Meter(Name(metricName), Unit.Errors, TimeUnit.Seconds);

            nancyPipelines.OnError.AddItemToStartOfPipeline((ctx, ex) =>
            {
                errorMeter.Mark();
                return null;
            });
        }

        /// <summary>
        /// Registers a Counter metric named "NancyFx.ActiveRequests" that shows the current number of active requests
        /// </summary>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        /// <param name="metricName">Name of the metric.</param>
        public void RegisterActiveRequestCounter(IPipelines nancyPipelines, string metricName = "ActiveRequests")
        {
            var counter = Metric.Counter(Name(metricName), Unit.Custom("ActiveRequests"));

            nancyPipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
            {
                counter.Increment();
                return null;
            });

            nancyPipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
            {
                counter.Decrement();
            });
        }

        private string Name(string name)
        {
            if (!string.IsNullOrEmpty(this.MetricsPrefix))
            {
                return this.MetricsPrefix + "." + name;
            }
            return name;
        }
    }
}
<commit_msg>remove dependency on Metric static class<commit_after>﻿
using System;
using Metrics;
using Nancy.Bootstrapper;
namespace Nancy.Metrics
{
    public class NancyGlobalMetrics : IHideObjectMembers
    {
        private const string TimerItemsKey = "__Mertics.RequestTimer__";

        private readonly MetricsRegistry registry;

        public NancyGlobalMetrics(MetricsRegistry repository)
        {
            this.registry = repository;
            this.MetricsPrefix = "NancyFx";
        }

        public string MetricsPrefix { get; set; }

        /// <summary>
        /// Registers a Meter metric named "NancyFx.Errors" that records the rate at witch unhanded errors occurred while 
        /// processing Nancy requests.
        /// Registers a Timer metric named "NancyFx.Requests" that records how many requests per second are handled and also
        /// keeps a histogram of the request duration.
        /// </summary>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        public void RegisterAllMetrics(IPipelines nancyPipelines)
        {
            RegisterRequestTimer(nancyPipelines);
            RegisterErrorsMeter(nancyPipelines);
            RegisterActiveRequestCounter(nancyPipelines);
        }

        /// <summary>
        /// Registers a Timer metric named "NancyFx.Requests" that records how many requests per second are handled and also
        /// keeps a histogram of the request duration.
        /// </summary>
        /// <param name="metricName">Name of the metric.</param>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        public void RegisterRequestTimer(IPipelines nancyPipelines, string metricName = "Requests")
        {
            var requestTimer = this.registry.Timer(Name(metricName), Unit.Requests, SamplingType.FavourRecent, TimeUnit.Seconds, TimeUnit.Milliseconds);

            nancyPipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
            {
                ctx.Items[TimerItemsKey] = requestTimer.NewContext();
                return null;
            });

            nancyPipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
            {
                using (ctx.Items[TimerItemsKey] as IDisposable) { }
                ctx.Items.Remove(TimerItemsKey);
            });
        }

        /// <summary>
        /// Registers a Meter metric named "NancyFx.Errors" that records the rate at witch unhanded errors occurred while 
        /// processing Nancy requests.
        /// </summary>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        /// <param name="metricName">Name of the metric.</param>
        public void RegisterErrorsMeter(IPipelines nancyPipelines, string metricName = "Errors")
        {
            var errorMeter = this.registry.Meter(Name(metricName), Unit.Errors, TimeUnit.Seconds);

            nancyPipelines.OnError.AddItemToStartOfPipeline((ctx, ex) =>
            {
                errorMeter.Mark();
                return null;
            });
        }

        /// <summary>
        /// Registers a Counter metric named "NancyFx.ActiveRequests" that shows the current number of active requests
        /// </summary>
        /// <param name="nancyPipelines">Pipelines to hook on.</param>
        /// <param name="metricName">Name of the metric.</param>
        public void RegisterActiveRequestCounter(IPipelines nancyPipelines, string metricName = "ActiveRequests")
        {
            var counter = this.registry.Counter(Name(metricName), Unit.Custom("ActiveRequests"));

            nancyPipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
            {
                counter.Increment();
                return null;
            });

            nancyPipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
            {
                counter.Decrement();
            });
        }

        private string Name(string name)
        {
            if (!string.IsNullOrEmpty(this.MetricsPrefix))
            {
                return this.MetricsPrefix + "." + name;
            }
            return name;
        }
    }
}

{'fraction_non_alphanumeric': 0.09589377919842636, 'fraction_numerical': 0.0, 'mean_word_length': 2.037714712471994, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2883143', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2035, 'n_words': 511}