<commit_before>module Trashed
  module Rack
    class MeasureResourceUsage
      def initialize(app, options = {})
        @app = app
        @debug, @logger, @statsd, @sample_rate = options.values_at(:debug, :logger, :statsd, :sample_rate)
        @sample_rate ||= 0.1
      end

      def call(env)
        response = nil
        instrument(env) { response = @app.call(env) }
        response
      end

      def instrument(env, &block)
        change = ResourceUsage.instrument(&block)
        usage  = ResourceUsage.gauge
        record env, change, usage
      end

      def record(env, change, usage)
        record_env    env, change, usage
        record_logger env, change, usage if @logger
        record_statsd env, change, usage if @statsd
      end

      def record_env(env, change, usage)
        env['trashed.change'] = change
        env['trashed.usage']  = usage
      end

      def record_logger(env, change, usage)
        @logger.info "Rack handled in %dms (GC runs: %d)" % change.values_at('Time.wall', 'GC.count')
        record_debug_logger env, change, usage if @debug
      end

      def record_debug_logger(env, change, usage)
        @logger.debug "Changes: #{change.to_yaml}"
        @logger.debug "Usage: #{usage.to_yaml}"
      end

      def record_statsd(env, change, usage)
        record_statsd_timing change, :Rack
        record_statsd_timing usage
      end

      def record_statsd_timing(data, namespace = nil)
        data.each do |name, value|
          name = "#{namespace}.#{name}" if namespace
          @statsd.timing name, value, @sample_rate
        end
      end
    end
  end
end<commit_msg>Nest performance measurements under Rack, so per-request measurements are BCX.production.Performance.Rack.Request<commit_after>module Trashed
  module Rack
    class MeasureResourceUsage
      def initialize(app, options = {})
        @app = app
        @debug, @logger, @statsd, @sample_rate = options.values_at(:debug, :logger, :statsd, :sample_rate)
        @sample_rate ||= 0.1
      end

      def call(env)
        response = nil
        instrument(env) { response = @app.call(env) }
        response
      end

      def instrument(env, &block)
        change = ResourceUsage.instrument(&block)
        usage  = ResourceUsage.gauge
        record env, change, usage
      end

      def record(env, change, usage)
        record_env    env, change, usage
        record_logger env, change, usage if @logger
        record_statsd env, change, usage if @statsd
      end

      def record_env(env, change, usage)
        env['trashed.change'] = change
        env['trashed.usage']  = usage
      end

      def record_logger(env, change, usage)
        @logger.info "Rack handled in %dms (GC runs: %d)" % change.values_at('Time.wall', 'GC.count')
        record_debug_logger env, change, usage if @debug
      end

      def record_debug_logger(env, change, usage)
        @logger.debug "Changes: #{change.to_yaml}"
        @logger.debug "Usage: #{usage.to_yaml}"
      end

      def record_statsd(env, change, usage)
        record_statsd_timing change, :'Rack.Request'
        record_statsd_timing usage,  :Rack
      end

      def record_statsd_timing(data, namespace = nil)
        data.each do |name, value|
          name = "#{namespace}.#{name}" if namespace
          @statsd.timing name, value, @sample_rate
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10005868544600939, 'fraction_numerical': 0.0011737089201877935, 'mean_word_length': 2.361932938856016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3784118', 'n_tokens_mistral': 1033, 'n_tokens_neox': 1015, 'n_words': 281}