<commit_before>require "uri"
begin
  require "statsd"
rescue LoadError
  raise 'Gem dogstatsd-ruby is required for logging metrics. Please add the gem "dogstatsd-ruby" to your Gemfile.'
end

# Example:
#   Sapience.add_appender(:datadog, {url: "udp://localhost:2222"})
#
module Sapience
  module Appender
    class Datadog < Sapience::Subscriber
      # Create Appender
      #
      # Parameters:
      #   level: :trace
      #   url: [String]
      #     Valid URL to post to.
      #     Example:
      #       udp://localhost:8125
      #     Example, send all metrics to a particular namespace:
      #       udp://localhost:8125/namespace
      #     Default: udp://localhost:8125
      #   tags: [String]
      #     Example:
      #       tag1:true
      # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity

      def initialize(options = {}, &block)
        fail("Options should be a Hash") unless options.is_a?(Hash)
        url   = options.delete(:url) || "udp://localhost:8125"
        @tags = options.delete(:tags)
        @uri  = URI.parse(url)
        fail('Statsd only supports udp. Example: "udp://localhost:8125"') if @uri.scheme != "udp"

        super(options, &block)
      end

      def provider
        @_provider ||= begin
          statsd           = ::Statsd.new(@uri.host, @uri.port, tags: @tags)
          path             = @uri.path.chomp("/")
          statsd.namespace = path.sub("/", "") if path != ""
          statsd
        end
      end

      # Send an error notification to sentry
      def log(log)
        metric = log.metric
        return false unless metric

        if log.duration
          timing(metric, log.duration)
        else
          amount = (log.metric_amount || 1).round
          if amount < 0
            decrement(metric, amount.abs)
          else
            increment(metric, amount)
          end
        end
        true
      end

      def timing(metric, duration)
        provider.timing(metric, duration)
      end

      def increment(metric, amount = 1)
        provider.batch do
          amount.times { provider.increment(metric) }
        end
      end

      def decrement(metric, amount = 1)
        provider.batch do
          amount.times { provider.decrement(metric) }
        end
      end

      def histogram(metric, amount)
        provider.histogram(metric, amount)
      end

      def gauge(metric, amount, hash)
        provider.gauge(metric, amount, hash)
      end

      def batch
        yield provider
      end

    end
  end
end<commit_msg>[+AM] Add missing event method to metrix<commit_after>require "uri"
begin
  require "statsd"
rescue LoadError
  raise 'Gem dogstatsd-ruby is required for logging metrics. Please add the gem "dogstatsd-ruby" to your Gemfile.'
end

# Example:
#   Sapience.add_appender(:datadog, {url: "udp://localhost:2222"})
#
module Sapience
  module Appender
    class Datadog < Sapience::Subscriber
      # Create Appender
      #
      # Parameters:
      #   level: :trace
      #   url: [String]
      #     Valid URL to post to.
      #     Example:
      #       udp://localhost:8125
      #     Example, send all metrics to a particular namespace:
      #       udp://localhost:8125/namespace
      #     Default: udp://localhost:8125
      #   tags: [String]
      #     Example:
      #       tag1:true
      # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity

      def initialize(options = {}, &block)
        fail("Options should be a Hash") unless options.is_a?(Hash)
        url   = options.delete(:url) || "udp://localhost:8125"
        @tags = options.delete(:tags)
        @uri  = URI.parse(url)
        fail('Statsd only supports udp. Example: "udp://localhost:8125"') if @uri.scheme != "udp"

        super(options, &block)
      end

      def provider
        @_provider ||= begin
          statsd           = ::Statsd.new(@uri.host, @uri.port, tags: @tags)
          path             = @uri.path.chomp("/")
          statsd.namespace = path.sub("/", "") if path != ""
          statsd
        end
      end

      # Send an error notification to sentry
      def log(log)
        metric = log.metric
        return false unless metric

        if log.duration
          timing(metric, log.duration)
        else
          amount = (log.metric_amount || 1).round
          if amount < 0
            decrement(metric, amount.abs)
          else
            increment(metric, amount)
          end
        end
        true
      end

      def timing(metric, duration)
        provider.timing(metric, duration)
      end

      def increment(metric, amount = 1)
        provider.batch do
          amount.times { provider.increment(metric) }
        end
      end

      def decrement(metric, amount = 1)
        provider.batch do
          amount.times { provider.decrement(metric) }
        end
      end

      def histogram(metric, amount)
        provider.histogram(metric, amount)
      end

      def gauge(metric, amount, hash)
        provider.gauge(metric, amount, hash)
      end

      def batch
        yield provider
      end

      def event(title, text, options = {})
        provider.event(title, text, options)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10070705140454805, 'fraction_numerical': 0.011083508503726351, 'mean_word_length': 1.8383947939262473, 'pattern_counts': {'":': 0, '<': 7, '<?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': '797067', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1497, 'n_words': 407}